Floating Point calculation
Hello,
I have a question on floating point,
I using floating point,
tMath : "FloatMath"
fString : "FloatString"
and performing this calculation
(0.1 * 13) / 13 which should give me 0.1 as output
but I am getting 0.07692308
Hz: 13 GPM: 0.07692308
I attached the code:
regards
Jeff
I have a question on floating point,
I using floating point,
tMath : "FloatMath"
fString : "FloatString"
and performing this calculation
(0.1 * 13) / 13 which should give me 0.1 as output
but I am getting 0.07692308
Hz: 13 GPM: 0.07692308
I attached the code:
{Object_Title_and_Purpose}
CON
_clkmode = xtal1 + pll16x 'Standard clock mode * crystal frequency = 80 MHz
_xinfreq = 5_000_000
VAR
long Transition_counter
long pin_state
long previous_pin_state
long GPM
long GPM_temp
OBJ
term : "FullDuplexSerial"
Math : "FloatMath"
' fMath : "Float32full"
String : "FloatString"
PUB Main | t
term.start(31, 30, 0, 115200)
dira[8]~ ' set to input := 0
'Hz is frequeny per second
' count the number of transisions from high->low->high
repeat
term.tx(13)
term.str(string("Hz: "))
transition_counter := 13
term.dec(transition_counter)
'GPM := (0.1 * transition_counter) / 13
fmath.FFloat(transition_counter)
GPM_temp := fmath.FMul(0.10, transition_counter)
GPM := fmath.FDiv(GPM_temp,13)
term.str(string(" GPM: "))
term.str(fstring.FloatToString(GPM))
GPM_temp := 0
GPM := 0
Transition_counter := 0 ' reset Transision counter
t := clkfreq + cnt ' get next second
repeat while cnt < t ' loop until second is up
waitcnt(clkfreq/1000 + cnt) ' wait one millisecond
pin_state := ina[8] ' check high or low
if pin_state == previous_pin_state
'do nothing
else
previous_pin_state := pin_state ' make new pin state
transition_counter++ ' and count the transision
regards
Jeff

Comments
Jeff
I think the line
should be
Bean
that yields
Hz: 13 GPM: 5.444519e+38
regards
Jeff
Oh, I see. I think this needs changed too. to
Basically you can't use integers ANYWHERE you are doing floating point stuff. Without using FFloat.
I'm not sure, but you might be able to just use 13.0 too.
Bean
P.S. Yes, I tried it. using 13.0 will work.
anyway, the changes
'GPM := (0.1 * transition_counter) / 13 transition_counter := fmath.FFloat(transition_counter) GPM_temp := fmath.FMul(0.10, transition_counter) GPM := fmath.FDiv(GPM_temp ,13.0) term.str(string(" GPM: ")) term.str(fstring.FloatToString(GPM))output is now correct
Hz: 13 GPM: 0.1
thanks for the help
regards
Jeff
The latest version (i think it's 1.6) is in the OBEX. F32 is also included in the archive attached to my integer vs floating point trig speed tests.