e-008 in floats
BlitzMax Forums/BlitzMax Programming/e-008 in floats
| ||
Hello, I noticed yesterday that BMax sometimes stores floats or doubles with an 'e' notation. How can I convert those into float or doubles that use digits only? Even casting to a string or a double retains the 'e' notation at the end. Is there a built in function to do this, or do I need to convert it myself? Rgds Reda |
| ||
stores floats or doubles with an 'e' notation No. It's only the conversion to a string that makes it appear that way. |
| ||
If it were me, I'd use this :SuperStrict Framework BaH.Format Import BRL.StandardIO Local formatter:TFormatter = TFormatter.Create("%.22f") Local f:Double = 33.0/4433300.44 Print "Default = " + String.FromDouble(f) Print "Formatted = " + formatter.DoubleArg(f).Format() ' Default = 7.4436643444641959e-006 ' Formatted = 0.0000074436643444641959 Other people will do their own thing, no doubt. |
| ||
The 'e' means *10^ As instance: Local v:Float = 0.00000000009 Print vwill output 9.00000005e-011 wich means: 9 * 10^(-11), or 0.00000000009. |
| ||
Brucey, Thank you very much. That seems to be the solution. |
| ||
Hey Reda, I think Bruce and Ziggy know more than me, and perhaps you know already about the issues involved in working with floats, but I'll bring it up anyway.. You may be asking too much of floating values if you expect to be able check them for reaching zero, or comparing them against eachother. The usual case is to check them to being close within a tolerance (as defined by your application / units). SuperStrict Function floatCompare:Int ( f1:Float, f2:Float, tol:Float = 0.001:Float ) Return (f1 + tol >= f2) And (f1 - tol <= f2 ) End Function Local f1:Float = 0.00010:Float Local f2:Float = 0.00008:float ' comparing two floats to different tolerances Print floatCompare( f1, f2, 0.00001:Float ) ' returns FALSE Print floatCompare( f1, f2 ) ' returns TRUE ' checking if a float "is approximately zero" Print floatCompare( f2, 0 ) ' returns TRUE Cheers Matt |
| ||
matibee I have indeed been thinking about this issue but with no elegant solution. Your code example has definitely put me on the right path. |