Trouble building a calculator with the FloatMath and FloatString objects
mneuert
Posts: 9
Hello,
Thanks in advance to anyone willing to help me out. My issue seems fairly trivial, but I appreciate all help nonetheless.
I've been playing around with the "Parallax Serial Terminal.spin" object and am trying to make a simple calculator. Here is my code so far:
When I run my code, I get the following output:
The sample calculations from the first part of my code seem to work out just fine (that's the 10.2, -1.5, etc., from the first part of the output), but when I try to take input from they keyboard (that's 8.0 and 4.5), the output is not at all what I intend; somehow, both 8.0 and 4.5 are being read as 1.121039e-43, which don't even seem to be added together properly (1.121039e-43 + 1.121039e-43 != 1.751623e-43).
Could someone please point out why my code isn't adding 8.0 and 4.5? Am I using the correct methods from FloatMath and FloatString?
One other thing is that if the numbers that I use in the fist part of my code are not floats, then the first calculations don't work either. In other words, if I try and write
Thanks again for all your help - it's very much appreciated!
-MN
Thanks in advance to anyone willing to help me out. My issue seems fairly trivial, but I appreciate all help nonetheless.
I've been playing around with the "Parallax Serial Terminal.spin" object and am trying to make a simple calculator. Here is my code so far:
CON _xinfreq=5_000_000 _clkmode=xtal1+pll16x OBJ pst : "Parallax Serial Terminal" f : "FloatMath" fs : "FloatString" PUB Calculator | A,B,operation pst.start(115200) pst.str(fs.FloatToString(f.FAdd(5.0,5.2))) pst.NewLine pst.str(fs.FloatToString(f.FSub(2.0,3.5))) pst.NewLine pst.str(fs.FloatToString(f.FMul(3.0,4.0))) pst.NewLine pst.str(fs.FloatToString(f.FDiv(5.5,11.0))) pst.NewLine pst.NewLine repeat pst.str(string("Enter the first number ")) 'A:=pst.GetFloat A:=pst.DecIn pst.str(fs.FloatToString(A)) pst.NewLine pst.NewLine pst.str(string("Enter the second number ")) 'B:=pst.GetFloat B:=pst.DecIn pst.str(fs.FloatToString(A)) pst.NewLine pst.NewLine pst.str(string("Select an operation (+ - * /) ")) 'operation:=pst.rx operation:=pst.CharIn 'pst.NewLines(2) pst.NewLine pst.str(string("The solution is ")) case operation "+" : pst.str(fs.FloatToString(f.FAdd(A,B))) "-" : pst.str(fs.FloatToString(f.FSub(A,B))) "*" : pst.str(fs.FloatToString(f.FMul(A,B))) "/" : pst.str(fs.FloatToString(f.FDiv(A,B))) other : pst.str(string("ERROR!")) 'pst.NewLines(3) pst.NewLine pst.NewLine pst.NewLine
When I run my code, I get the following output:
The sample calculations from the first part of my code seem to work out just fine (that's the 10.2, -1.5, etc., from the first part of the output), but when I try to take input from they keyboard (that's 8.0 and 4.5), the output is not at all what I intend; somehow, both 8.0 and 4.5 are being read as 1.121039e-43, which don't even seem to be added together properly (1.121039e-43 + 1.121039e-43 != 1.751623e-43).
Could someone please point out why my code isn't adding 8.0 and 4.5? Am I using the correct methods from FloatMath and FloatString?
One other thing is that if the numbers that I use in the fist part of my code are not floats, then the first calculations don't work either. In other words, if I try and write
pst.str(fs.FloatToString(f.FMul(3,4.0)))at the beginning instead of using 3.0 and 4.0, the output becomes 1.681558e-44 instead of 12. Is this happening for the same reason that my calculator isn't working?
Thanks again for all your help - it's very much appreciated!
-MN
Comments
One quick question - is there a reason you added the comments {0} after the pointer to one and two? I assume those are comments...
-MN
There was a recent discussion about converting a float back to an integer while preserving precision of the fractional portion by using "pseudo real" numbers.