Problems with numbers
Hi all
The following code seems to have a problem. If Fout is an integer, all is well, however changing it to a decimal, even going from 7 to 7.0 causes the code to give odd results. I have tried using the F32 object to round, truc and other things but that gives yet more confusing results. There must be something really obvious that I am missing - but I have no idea what.
Any ideas?
Thanks
Richard
The following code seems to have a problem. If Fout is an integer, all is well, however changing it to a decimal, even going from 7 to 7.0 causes the code to give odd results. I have tried using the F32 object to round, truc and other things but that gives yet more confusing results. There must be something really obvious that I am missing - but I have no idea what.
OBJ
pst : "Parallax Serial Terminal"
fpm : "F32" 'Load Float Routines
Var
Long Fout, Fclock, f, fw
Con
Divisor=34359738.4
PUB Main | value, base, width, offset
pst.Start(115_200)
'__________________ Test my code
Fout := 7 'Test data
Fclock := 125 'Test data
f := 0 'Hard clear f
fpm.start
'Calculate the frequency word required
Fout <<= 1
repeat 32 'perform long division of (Fout / Fclock) * 2^32
f <<= 1
if Fout => Fclock
Fout -= Fclock
f++
Fout <<= 1
f:=f+1
fw:=f
waitcnt(clkfreq*5+cnt)
pst.Clear
pst.Chars(pst#NL, 1)
pst.Dec(f)
pst.Chars(pst#NL, 2)
pst.Dec(fw)
pst.Chars(pst#NL, 1)
pst.Hex(fw, 8)
pst.Chars(pst#NL, 1)
pst.Bin(fw,32)
pst.Chars(pst#NL, 1)
Pst.dec(cnt)
Any ideas?
Thanks
Richard
Comments
Why not post the code that doesn't work and explain exactly what you expect it to do ?
I did post the code in my first post.
Fout = 7 should give 240518169 (it does)
However Fout = 7.0 gives 2118123644
Look at this object and this one. Note that in the 2nd object, you should change the reference to the FloatMath object to use F32 instead which is a faster drop-in replacement.
if you want to do calculations with numbers stored as floating point you have to use the methods for "plus", "minus", "multiply" and "divide" that are inside
the floatingpoint-object F32.
the propeller can handle only 32bit integers. Even a floating-point value is stored in a 32bit integer. But the meaning of the bits is different to the meaning
of bits in a long. So first of all a long-value has to be transformed into a floating-point value and after that all mathematical operations must be done with floating-point methods
see the following methods inside the file F32.spin
PUB FAdd(a, b) PUB FSub(a, b) PUB FMul(a, b) PUB FDiv(a, b) PUB FFloat(n)
best regardsStefan
Con _CLKMODE = XTAL1 + PLL16X _XINFREQ = 5_000_000 Var Obj pst : "parallax serial terminal" fm : "floatmath" fs : "floatstring" Pub main | a, b, c pst.start(115200) a := 7.0 b := 34359738.4 c := fm.fdiv(a, b) 'divide a by b pst.str(fs.floattostring(c)) 'display floating point #
Edit: integers must be turned into floats to do math with other floats by using the .ffloat method in floatmath
Thanks. That's the solution I needed. I am making slow but steady progress - this is new to me so the patient help is appreciated!
Cheers.