Simple float question
turbosupra
Posts: 1,088
I'm trying to get a simple floatmath calculation working that I can baseline off of and build on ... what am I doing wrong here?
Thanks for reading! The attachment has the spin file and the necessary math/debug spin files.
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR
long numerator
long denominator
long answer
OBJ
debug : "FullDuplexSerial"
f : "FloatMath"
PUB main | freq
debug.Start(31,30,0,115200) 'start cog with serial driver
waitcnt((clkfreq * 3) + cnt) ' makes life easier when starting to debug with a 2 second pause to get the serial viewer active and receiving
debug.Tx(13)
Debug.Tx(13)
Debug.Tx(13)
Debug.Str(string(" */\*/\*/\*/\*/\*/\*/\* START */\*/\*/\*/\*/\*/\*/\* About to enter main pub")) ' for debug purposes
Debug.Tx(13)
Debug.Tx(13)
Debug.Tx(13)
numerator := f.ffloat(22)
denominator := f.ffloat(7)
repeat
f.ffloat(answer)
answer := f.fdiv(numerator,denominator)
debug.str(string("The answer equals "))
debug.dec(f.ffloat(answer))
debug.tx(13)
waitcnt((clkfreq) + cnt)
Thanks for reading! The attachment has the spin file and the necessary math/debug spin files.
Comments
instead. ffloat converts int to float. As answer is a float you want to convert it to an int (fround) to be displayed by debug.dec().
Thanks!
I would like it displayed as a decimal though, is that possible? Are you saying debug.dec is not able to display a decimal?
debug.dec does display a decimal number (an int in fact). If you want this in the form of 3.14 then have a look at the FloatString object (available in the prop tool library). Something along the lines of
should do the trick.
Now if I can just get the rest of the calcs to work with this I will be set
numerator/denominator = 16.666667 (numerator is 1000, denominator is 60)
but
(((clkfreq/2)/66656) /36) should also = 1000, but instead equals 1.345247e-42, see the quoted lines below, what would cause the difference?
On serial out I see
Your calculation:
is being done as normal signed integer. So you have an integer result of 1000 which you should print as
I found that sometimes I have to use these objects
object fullduplexserial = debug
object floatstring = fstring
object floatmath = f
and then f.div(variable1, variable2)
or f.div(variable1, variable2.0) as it wanted a .0 to properly display and calculate
and then debug.str(fstring.floattostring(variablename))
or debug.dec(variablename)
There are also numbers too big, the float object cannot accept 40,000,000 but can accept 40,000 ? So some scaling might be necessary.