Floating point math and strange results in the terminal
turbosupra
Posts: 1,088
Parallax terminal reads: wrote:additionAnswer is: 2
subtractionAnswer is: -2
multiplicationAnswer is: 16
divisionAnswer is: 3
serialDataExample1 is: 1
serialDataExample2 is: 1
I'm unsure as to why I'm receiving "-2" as the result for the subtractionAnswer variable, and why I'm required to put pst.Str(fs.FloatToString(additionAnswer)) and pst.Str(fs.FloatToString(subtractionAnswer)) but then for division and multiplication I have to put pst.dec(multiplicationAnswer) and pst.dec(divisionAnswer) ? I'm also unsure as to why I cannot get the serialDataExample1 and serialDataExample2 to divide correctly? I can upload the objects later if someone would like them, just not from work because of the firewall.
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 VAR long numerator long denominator long constantMultiplier long additionAnswer long subtractionAnswer long multiplicationAnswer long divisionAnswer OBJ pst : "Parallax Serial Terminal" strings : "Strings" f : "Float32" fm : "FloatMath" fs : "FloatString" PUB Main {Addition: result = a + b Subtraction: result = a - b Multiplication: result = a * b Division: result = a / b} 'numerator := 1640.0 'denominator := 2.0 'constantMultiplier := 1.221 pst.Start(115_200) ' starts with passed on variable of the baud rate pst.Clear f.start pst.Str(String("Start" )) pst.Str(String(pst#NL)) repeat 'x := f.FAdd(x, y) is correct - from the propeller floating point math pdf file additionAnswer := f.FAdd(2.0, 4) subtractionAnswer := f.FSub(4, 2.0) multiplicationAnswer := f.FMul(8, 2.0) divisionAnswer := f.Fdiv(6, 2.0) serialDataExample1 := (f.FMul(820, 0.001221)) serialDataExample2 := ((f.FMul(1638, 1.221)/1000)) pst.Str(String("additionAnswer is: " )) pst.Str(fs.FloatToString(additionAnswer)) pst.Str(String(pst#NL)) pst.Str(String("subtractionAnswer is: " )) pst.Str(fs.FloatToString(subtractionAnswer)) pst.Str(String(pst#NL)) pst.Str(String("multiplicationAnswer is: " )) pst.dec(multiplicationAnswer) pst.Str(String(pst#NL)) pst.Str(String("divisionAnswer is: " )) pst.dec(divisionAnswer) pst.Str(String(pst#NL)) pst.Str(String("serialDataExample1 is: " )) pst.Dec((serialDataExample1)) pst.Str(String(pst#NL)) pst.Str(String("serialDataExample2 is: " )) pst.Dec((serialDataExample2)) pst.Str(String(pst#NL)) WaitCnt((clkfreq) + cnt)
Comments
divisionAnswer := f.Fdiv(6, 2.0)
be written as divisionAnswer := f.Fdiv(6.0, 2.0)
????
Beside the "6" just mentioned, I also see a "4" without a ".0" on it. (And a 1638, 820 etc.)
I put this line right under f.start
fs.SetPrecision(4)
and still nothing. I tried f.FRound in a few places as well without luck? Am I putting the fs.SetPrecision(4) in the correct location? If not, can you quote my code and show me where please?
I'm usually not worried about the formatting of my floating point projects.
You could use the "round" method and then output with a method like this:
You could add the method to your serial object. Just make sure and change all the "tx" calls to what ever the method is called in your serial object. For PST, all the tx calls will need to be changed to char calls.
So if you have a variable "x" with a number you want to display to one digit past the decimal, do this.
This assumes you've added the DecPoint method to PST.
I think finding a floating point formatting method would be less awkward though.
Oh, lose the "Float32" and "FloatMath". Those objects are so last decade! F32 is the way to go. It's faster and fits in one cog. You shouldn't need to modify your code to use it.
The latest version of F32 is here. The OBEX version has a bug.
Edit (July 12, 2012): I fixed a bug the DecPoint method above. Thank you Gunstar1 for pointing it out.
I don't know why, but I cannot get this to work, it seems like it should be a very simple thing too. I did as you suggested with modifying the pst object, without luck and I cannot for the life of me get it to round up??? Below is the current status, I appreciate you sticking with me on this.
Instead of "0.001221" use 1221 and just remember it has been multiplied by 1,000,000 and compensate later when you do other calculations on it.
You would use DecPoint to display it with 1,000,000 as the denominator.
F.ffloat method is correct. You can throw in to a equation like. Sum := f.fadd(f.ffloat(adc1),2.0) or just float them as you read the value in.
Do you have any other 3208 interface tips, so I can save myself the headache?
I'm hoping this post will come up in a search to save future users the headache of banging their head through this process like I did. It's a lot easier to work backwards from a working set of code, then to work through all of the issues to get to that working set of code.