f32 / floatstring question?
Dwayne Dibbley
Posts: 63
i am trying to get the floating point math to work but with the following i would have expected to return 100 :
f1 := f32.FFloat(50)
f1 := f32.FMul( f1,2 )
term.str( fs.FloatToString( f1 ) )
but instead it returns 3.11653e-36?
am i doing something wrong again
Thanks
f1 := f32.FFloat(50)
f1 := f32.FMul( f1,2 )
term.str( fs.FloatToString( f1 ) )
but instead it returns 3.11653e-36?
am i doing something wrong again
Thanks
Comments
Jonathan
f1 := f32.FFloat(50)
f1 := f32.FMul( f1,2.0 )
term.str( fs.FloatToString( f1 ) )
resulted in 100
OK how can i convert a string eg "12.1234" in to a floating point to use with the math functions as FFloat only works with whole numbers
Thanks
Jonathan
is this as expected?
Thanks
thanks,
Jonathan
if i input 23.3784 i get 23.3784 returned
lat1 := f32.FAdd( f32.FDiv( f32.atof(gps.s_latm),60.0 ),f32.atof(gps.s_latd) )
term.str( fs.FloatToString( lat1 )) 'lat1=50.38959
term.tx(" ")
term.str( fs.FloatToString( f32.ACOS(f32.COS(f32.RADIANS(90-lat1))))) 'returns 6.805647e+38
in excel with =ACOS(COS(RADIANS(90-50.38959))) results in 0.691332073
am i not using the f32 function calls correctly for this miss result?
Thanks
now comparing to excel
excel results
90-50.38959=39.61041
RADIANS(39.61041)=0.691332
COS(0.691332)=0.770397
ACOS(0.770397)=0.691332
f32 results
90-50.38959=39.61031
RADIANS(39.61031)=0.6913304
COS(0.6913304)=0.7703919
ACOS(0.7703919)=0.6913408
is that as close as i am going to get with the current f32?
Thanks
Jonathan
i have the following excel formula : =ACOS(F1*F2+F3*F4*F5) results in 0.065175656
which i have tried to make a f32 version with : f32.ACOS(f32.FMul(F32.FMul(f32.FAdd(f32.FMul(f1,f2),f3),f4),f5)) results in 0.7385263
but it gives a different result completely i would guess its must be down the the way i have used the f32 options ?
Thanks again
Edit: last one for the evening? It's lunchtime here...feel free to keep those questions coming!
i am hopefully creating an gps tripmeter with 0.00 precision so my idea was to read the current gps position once a second and calculate the distance between them to generate a value for the tripmeter.
Thanks
UPDATE: would the 32 bit floating point coprocessor from parallax give me the double point precision?
something like this:
repeat
dist := f32.FAdd(dist,f32.FMul(f32.FMul(f32.atof(string_with_gps_knots),0.868976242),0.0000277777778)) '0.868976242=knots to miles per hour & 0.0000277777778 =1/10 sec in hours
term.str(fs.FloatToString(dist))
would that work or should i multiply everything by 1_000_000 if too many decimal places first and devide the result I only need miles in 0.00 hundruths?
Thanks
Some possible solutions:
- using integers...you have to control the scaling, but you get more digits to play with (google "fixed-point")
- keep a variable "last_GPS", and keep checking the current position "current_GPS", and don't update the trip length until "dist_last_current" > threshold.
(and a much more complex extension of the idea above: keep N intermediate trip_length variables. Always update the smallest trip length variable...and if that goes over a threshold then add it to the one above (this operation ripples "up" the chain). The thresholds might be 1, 10, 100, 1000, etc.)
One other quick thing to note is multiplying a value by 2 consecutive constants can (should ;-) be merged, the compiler can do the math with constants. For example, "FMul( FMul( x, 0.1), 0.27 )" would be "FMul( x, 0.1 * 0.27 )".
Jonathan
so for the above example : dist := f32.FAdd(dist,f32.FMul(f32.FMul(f32.atof(string_wi th_gps_knots),0.868976242),0.0000277777778))
should be like this : dist := f32.FAdd(dist,f32.FMul(f32.atof(string_wi th_gps_knots) * 0.868976242),0.0000277777778))
and i can only use 7 sig-figs ie : 0.868976242 should be 0.868976 and i would be better multiplying 0.0000277777778 by 10_000 = 0.277777 so both values are in the same range? then dividing the result by something 10_000 ? to get back to the correct figure? and should generate better results ?
Thanks again
formula should be knots / 0.8689762 and not * !!
re ran a test run car odo read 11.2 Miles and propeller f32 read 11.13678 Miles with the following formula:
dist := f32.FAdd(dist,f32.FMul(f32.FDiv(f32.atof(gps.s_speedk),0.8689762),0.00002777777))