baml.Float
A 64-bit IEEE-754 floating-point number.
Signature
class baml.FloatA 64-bit IEEE-754 floating-point number.
Arithmetic follows IEEE-754 throughout, so float never panics. Division
by zero has an answer: 1.0 / 0.0 is inf and 0.0 / 0.0 is NaN, as in
JavaScript. int and bigint have no value to give and panic instead.
inf and NaN also come from float.inf() and float.nan(), from
overflow (1e308 * 10.0 is inf), and from other invalid operations
((-1.0).sqrt() is NaN).
Neither has a JSON representation, so baml.json.from renders NaN and both
infinities as null.
Comparison departs from raw IEEE: == is reflexive and </<=/>/>=
are a total order, with NaN a single value ordered above every number
(-inf < …reals… < +inf < NaN) — see baml.ops.Compare for float.
Source:<builtin>/baml/float.bamlbytes 812–16575
Static methods
_unit_from_draw
(draw: int) -> float throws never(internal) Maps 53 bits of an int draw onto [0.0, 1.0).
e
() -> float throws neverReturns Euler's number e, the base of the natural logarithm.
golden_ratio
() -> float throws neverReturns the golden ratio φ = (1 + √5) / 2, about 1.618.
inf
() -> float throws neverReturns positive infinity. Negate it — -float.inf() — for negative
infinity.
nan
() -> float throws neverReturns a NaN value. Test for it with x.is_nan(), never with equality.
Parses text as a floating-point number.
Accepts decimal notation (1.5, -0.25), scientific notation (1e3,
-1.5E-3), and the case-insensitive tokens inf, infinity, and
nan, each with an optional leading + or -. No surrounding
whitespace, no underscore separators.
Parsing "nan" succeeds and yields a NaN value, so test the result with
is_nan() rather than equality.
Throws
baml.errors.ParseErroriftextcannot be parsed.
Examples
float.parse("1.5") // 1.5
float.parse("-0.25") // -0.25
float.parse("1e3") // 1000.0
float.parse("inf") // +infinity
float.parse("NaN") // NaN
float.parse("") // throws — empty
float.parse("hello") // throws — non-numeric
float.parse(" 1.0 ") // throws — whitespace; trim first
pi
() -> float throws neverReturns π, the ratio of a circle's circumference to its diameter, to float precision (about 15 decimal digits).
Returns a uniformly distributed random float in the half-open range
[0.0, 1.0), drawn from rng.
rng defaults to random.SystemRandom, the host's cryptographic entropy
source; pass a seeded generator to make the draw reproducible. Each call
consumes one random_int value and uses 53 of its bits.
Examples
float.random() // some x with 0 <= x < 1
float.random() * 100.0 // some x with 0 <= x < 100
// Reproducible: the same seed replays the same values.
let rng = baml.random.Xoshiro256PlusPlus.new(seed = my_seed);
float.random(rng = rng)
Instance methods
abs
(self) -> float throws neverReturns the absolute value of self. NaN stays NaN.
Examples
(-3.5).abs() // 3.5
(3.5).abs() // 3.5
(-0.0).abs() // 0.0
float.nan().abs() // NaN
acos
(self) -> float throws neverInverse cosine, in [0, π]. NaN if self is outside [-1, 1].
acosh
(self) -> float throws neverInverse hyperbolic cosine, defined on [1, ∞). NaN for self < 1.
asin
(self) -> float throws neverInverse sine, in [-π/2, π/2]. NaN if self is outside [-1, 1].
asinh
(self) -> float throws neverInverse hyperbolic sine, defined for every real.
atan
(self) -> float throws neverInverse tangent, in (-π/2, π/2).
atan2
(self, other: float) -> float throws neverReturns the angle in radians from the positive x-axis to the point
(x = other, y = self), in [-π, π].
Examples
(1.0).atan2(1.0) // π/4 (45°)
(1.0).atan2(0.0) // π/2 (90°, due north)
(0.0).atan2(-1.0) // π (180°, due west)
atanh
(self) -> float throws neverInverse hyperbolic tangent, defined on (-1, 1). ±∞ at the
endpoints, NaN outside them.
ceil
(self) -> float throws neverReturns the smallest integer greater than or equal to self, as a
float. The sign of ±0.0 is preserved; NaN and ±inf pass through.
Examples
(3.2).ceil() // 4.0
(-1.5).ceil() // -1.0
cos
(self) -> float throws neverCosine of self, in radians.
cosh
(self) -> float throws neverHyperbolic cosine.
floor
(self) -> float throws neverReturns the largest integer less than or equal to self, as a float.
The sign of ±0.0 is preserved; NaN and ±inf pass through.
Examples
(3.7).floor() // 3.0
(-1.5).floor() // -2.0
(3.0).floor() // 3.0
fract
(self) -> float throws neverReturns the fractional part of self, self - self.trunc().
Examples
(3.7).fract() // 0.7
(-3.7).fract() // -0.7
(5.0).fract() // 0.0
hypot
(self, other: float) -> float throws neverReturns the Euclidean distance sqrt(self² + other²), computed so that
large magnitudes do not overflow along the way.
Examples
(3.0).hypot(4.0) // 5.0
(0.0).hypot(0.0) // 0.0
Like ceil, but returns int.
Throws
baml.errors.InvalidArgumentifselfis NaN or±inf, or rounds to a value outside theintrange.
Like floor, but returns int.
Throws
baml.errors.InvalidArgumentifselfis NaN or±inf, or rounds to a value outside theintrange.
Like round, but returns int.
Throws
baml.errors.InvalidArgumentifselfis NaN or±inf, or rounds to a value outside theintrange.
is_finite
(self) -> bool throws neverReturns true if self is neither NaN nor infinite. Subnormals and
±0.0 count as finite.
Examples
(3.14).is_finite() // true
(0.0).is_finite() // true
float.inf().is_finite() // false
float.nan().is_finite() // false
is_infinite
(self) -> bool throws neverReturns true if self is positive or negative infinity.
Examples
float.inf().is_infinite() // true
(-float.inf()).is_infinite() // true
(1000000.0 * 1000000.0).is_infinite() // false (1e12, large but finite)
float.nan().is_infinite() // false
is_nan
(self) -> bool throws neverReturns true if self is NaN — the "not a number" sentinel invalid
operations produce, such as (-1.0).sqrt(), float.inf() - float.inf(),
or 0.0 / 0.0.
This predicate is the only way to test for NaN: BAML's == is
reflexive, so x != x is false for every float — including NaN, which
equals itself and every other NaN.
Examples
(0.0).is_nan() // false
float.nan().is_nan() // true
float.inf().is_nan() // false
Like trunc, but returns int.
Throws
baml.errors.InvalidArgumentifselfis NaN or±inf, or rounds to a value outside theintrange.
log
(self, base: float) -> float throws neverReturns the logarithm of self in the given base, computed as
ln(self) / ln(base).
Every boundary case falls out of that division:
self < 0gives NaN, sincelnof a negative is NaN.self == 0withbase > 1gives-inf:-inf / positive.self == 0with0 < base < 1gives+inf;ln(base)is negative, so the sign flips relative to the case above.self == 0withbase == 1gives-inf, sinceln(1)is+0.0.base < 0gives NaN.base == 0withself > 0gives±0, signed oppositeln(self), since±finite / -infis∓0.base == 1withself > 0andself != 1gives±inf, signed to matchln(self), since±finite / +0.0is±inf.base == 1withself == 1gives NaN:0 / 0.
Examples
(1000.0).log(10.0) // 3.0
(8.0).log(2.0) // 3.0
(-1.0).log(10.0) // NaN
(0.0).log(10.0) // -infinity
(0.0).log(0.5) // +infinity
(2.0).log(1.0) // +infinity
(1.0).log(1.0) // NaN
pow
(self, exp: float) -> float throws neverReturns self ** exp, computed in floating point.
Gives NaN wherever the mathematical value is not real, such as a negative base raised to a non-integer power.
Examples
(2.0).pow(10.0) // 1024.0
(2.0).pow(-1.0) // 0.5
(2.0).pow(0.5) // sqrt(2)
(-1.0).pow(0.5) // NaN
round
(self) -> float throws neverReturns the integer nearest to self, as a float, with ties rounded
away from zero: 0.5 to 1.0, -0.5 to -1.0, 1.5 to 2.0.
Examples
(1.5).round() // 2.0
(-1.5).round() // -2.0
(1.4).round() // 1.0
(1.6).round() // 2.0
sin
(self) -> float throws neverSine of self, in radians.
sinh
(self) -> float throws neverHyperbolic sine.
sqrt
(self) -> float throws neverReturns the square root of self, or NaN if self is negative.
Examples
(4.0).sqrt() // 2.0
(2.0).sqrt() // 1.4142...
(-1.0).sqrt() // NaN
(0.0).sqrt() // 0.0
tan
(self) -> float throws neverTangent of self, in radians. Very large near odd multiples of π/2.
tanh
(self) -> float throws neverHyperbolic tangent, bounded to (-1, 1).
to_degrees
(self) -> float throws neverConverts self from radians to degrees.
to_radians
(self) -> float throws neverConverts self from degrees to radians.
trunc
(self) -> float throws neverReturns the integer part of self, discarding the fraction — that is,
self rounded toward zero.
Examples
(3.7).trunc() // 3.0
(-3.7).trunc() // -3.0
Implementations
baml.Concrete for T
Source:<builtin>/baml/core.bamlbytes 749–781
baml.ops.Add for float
Output = floatInstance methods
add
(self, rhs: float) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 8570–8717
baml.ops.Add for float
Output = floatInstance methods
add
(self, rhs: int) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 9505–9648
baml.ops.Compare for float
A total order over every float, unlike IEEE 754, which leaves NaN unordered:
-inf < …negative reals… < 0.0 < …positive reals… < +inf < NaN
NaN is a single value here, greater than every number: its sign and payload
are not observable, because the NaN a hardware invalid operation produces
differs across CPU architectures and comparison must not. -0.0 and 0.0
compare Ordering.Equal, as under IEEE 754. Every other pair orders exactly
as IEEE 754 orders it.
This is the order <, <=, >, >= and float[].sort() all use.
Instance methods
clamp
(self, min: Self, max: Self) -> Self throws neverClamps self into the range [min, max].
Callers should pass min <= max; if min > max the result is always
min, because the lower clamp runs after the upper one. Written as the
min/max chain rather than a three-way match so it agrees with
int.clamp / float.clamp on that degenerate input too.
ge
(self, other: Self) -> bool throws neverWhether self orders after other or equals it. Defaults to
self.cmp(other) != Ordering.Less.
gt
(self, other: Self) -> bool throws neverWhether self orders strictly after other. Defaults to
self.cmp(other) == Ordering.Greater.
le
(self, other: Self) -> bool throws neverWhether self orders before other or equals it. Defaults to
self.cmp(other) != Ordering.Greater.
lt
(self, other: Self) -> bool throws neverWhether self orders strictly before other. Defaults to
self.cmp(other) == Ordering.Less.
max
(self, other: Self) -> Self throws neverTies return self, so max is stable when two values compare Equal
without being interchangeable.
min
(self, other: Self) -> Self throws neverTies return self, matching max.
Source:<builtin>/baml/ns_ops/comparison.bamlbytes 11507–11930
baml.ops.Divide for float
Output = floatInstance methods
div
(self, rhs: float) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 9027–9215
baml.ops.Divide for float
Output = floatInstance methods
div
(self, rhs: int) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 9950–10134
baml.ops.Equals for float
Reflexive, unlike IEEE 754: every NaN equals every other NaN (and so
itself), and -0.0 equals 0.0. Consistent with Compare for float below.
Instance methods
eq
(self, other: float) -> bool throws neverneq
(self, other: Self) -> bool throws neverWhether self and other differ. Defaults to !self.eq(other).
Override this only to compute the answer more directly; an override that
disagrees with !eq leaves the pair inconsistent. Equals for null
overrides it with a constant false, which is consistent because null
has exactly one value.
Source:<builtin>/baml/ns_ops/comparison.bamlbytes 10767–10884
baml.ops.Multiply for float
Output = floatInstance methods
mul
(self, rhs: float) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 8873–9025
baml.ops.Multiply for float
Output = floatInstance methods
mul
(self, rhs: int) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 9800–9948
baml.ops.Negate for float
Output = floatInstance methods
neg
(self) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 9372–9503
baml.ops.Remainder for float
Output = floatInstance methods
rem
(self, rhs: float) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 9217–9370
baml.ops.Remainder for float
Output = floatInstance methods
rem
(self, rhs: int) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 10136–10285
baml.ops.Subtract for float
Output = floatInstance methods
sub
(self, rhs: float) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 8719–8871
baml.ops.Subtract for float
Output = floatInstance methods
sub
(self, rhs: int) -> float throws neverSource:<builtin>/baml/ns_ops/math.bamlbytes 9650–9798
Related definitions
baml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Comparebaml.ops.Equalsbaml.Boolbaml.Concretebaml.errors.InvalidArgumentbaml.errors.ParseErrorbaml.Intbaml.ops.Addbaml.ops.Comparebaml.ops.Dividebaml.ops.Equalsbaml.ops.Multiplybaml.ops.Negatebaml.ops.Orderingbaml.ops.Remainderbaml.ops.Subtractbaml.random.Rngbaml.Stringbaml.json.from