Float value
Parameters
| value - a number, or a string holding a number |
Description
|
Converts a value to a float. Float is a prefix operator, so Float value and Float( value ) are the same, and it matches what Blitz3D+ already does when you assign an integer or a string to a # variable. The reason you reach for it explicitly is integer division. Blitz3D+ divides two integers as integers and throws the remainder away, so 12/5 is 2 even when you store it in a float. Writing Float( 12 )/5 - or, more usually, Float( a )/b - forces the calculation to happen in floating point and gives you 2.4. The same trap catches percentages, aspect ratios and any "how far along am I" progress value, which all silently collapse to 0 or 1 if both sides are integers. Applied to a string, Float reads as much of a valid number as it can and stops at the first character that cannot be part of one. Float( "10" ) is 10.0, Float( "3junk" ) is 3.0 and Float( "junk3" ) is 0.0. Remember that floats carry roughly seven significant digits, so they are fine for positions and timers but should not be used as exact counters. See also: Int, Str, Ceil, Floor. |
Example
; Float Example ; ------------- ; Float converts a value (or a string) to a floating point number. ; Its main use is forcing float arithmetic where Blitz would otherwise ; do whole-number integer division. Print "12/5 = "+(12/5)+" (integer division throws the fraction away)" Print "Float(12)/5 = "+(Float(12)/5) Print "" ; Applied to a string, Float converts as much of it as it can Print "Float of the string 3.25 = "+Float("3.25") Print "Float of the string 3junk = "+Float("3junk")+" (stops at the first non-numeric character)" Print "Float of the string junk3 = "+Float("junk3") Print "" Print "Press any key to close the example" WaitKey End
Index