TLang.Float / Double
TLang.Float and TLang.Double both work with IEEE 754 double-precision floating-point numbers. They are functionally identical — Double is the preferred name in most contexts.
use TLang.Float
use TLang.Double
Parsing and Conversion
let f = Float.parse("3.14") // String → Float
let s = Float.toString(3.14) // Float → String
let fi = Float.fromInt(42) // Int → Float
let i = Float.toInt(3.9) // Float → Int (truncated, not rounded)
Rounding
let r = Float.round(3.7) // 4.0
let f = Float.floor(3.7) // 3.0
let c = Float.ceil(3.2) // 4.0
Numeric Operations
let a = Float.abs(-2.5) // 2.5
let mn = Float.min(1.5, 2.5) // 1.5
let mx = Float.max(1.5, 2.5) // 2.5
let sq = Float.sqrt(16.0) // 4.0
let pw = Float.pow(2.0, 10.0) // 1024.0
Precise Arithmetic Functions
For contexts where operator precedence could cause ambiguity, named arithmetic functions are available.
let sum = Float.add(0.1, 0.2)
let diff = Float.sub(1.0, 0.3)
let prod = Float.mul(2.5, 4.0)
let quot = Float.div(10.0, 3.0)
Testing Values
let nan = Float.isNaN(0.0 / 0.0) // true
let inf = Float.isInfinite(1.0 / 0.0) // true
Constants
let pi = Float.pi() // 3.141592653589793
let e = Float.e() // 2.718281828459045
let inf = Float.infinity() // positive infinity
let nan = Float.nan() // NaN
Double
Double has the same API. Use Double when the intent is to emphasise double precision.
use TLang.Double
let pi = Double.pi()
let root = Double.sqrt(2.0)
let r = Double.round(Double.pi()) // 3.0
Example — Percentage Formatter
use TLang.Float
use TLang.Int
func percent(value: Float, total: Float): String {
let ratio = Float.div(value, total)
let pct = Float.mul(ratio, 100.0)
let pct = Float.round(pct)
return Int.toString(Float.toInt(pct)) + "%"
}
func main(): String {
let p = percent(37.0, 100.0)
Terminal.println(p) // "37%"
return "done"
}