Floats
Float is the default floating-point type for fractional numeric work.
`Float`
Float supports ordinary arithmetic, ordering, and string conversion.
ratio: Float = 0.75
-- ratio == 0.75
Sized Floats
Use Float32 or Float16 conversions when an interface or data layout needs a smaller representation.
func main(args: List[String]) -> Void:
sample: Float32 = to_float32(0.5)
compact: Float16 = to_float16(0.5)
-- sample == 0.5
-- compact == 0.5
Formatting
format gives a stable number of decimal places for output.
import:
float: format
func main(args: List[String]) -> Void:
label: String = format(3.14159, 2) -- "3.14"
-- label == "3.14"
Math Helpers
float includes helpers such as sqrt, pow, floor, ceil, and trigonometric functions.
import:
float: pow, sqrt
func main(args: List[String]) -> Void:
hypotenuse: Float = sqrt(pow(3.0, 2.0) + pow(4.0, 2.0))
-- hypotenuse == 5.0
Approximate Comparison
Use approx_eq or almost_equal when exact floating-point equality is the wrong model.
import:
float: approx_eq
func main(args: List[String]) -> Void:
same_enough: Bool = approx_eq(0.1 + 0.2, 0.3)
-- same_enough == True
Example
floats.brp
import:
float: format, sqrt
func main(args: List[String]) -> Void:
ratio: Float = 3.0 / 4.0
diagonal: Float = sqrt(9.0)
third: Float = 1.0 / 3.0
print(format(ratio, 2)) -- prints: 0.75
print(format(diagonal, 0)) -- prints: 3
print(format(third, 3)) -- prints: 0.333
Try it
terminal
blorp run examples/docs/core-data/floats/floats.brp