baml.Float

A 64-bit IEEE-754 floating-point number.

Reference version

Signature

class baml.Float

A 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

function

_unit_from_draw

(draw: int) -> float throws never

(internal) Maps 53 bits of an int draw onto [0.0, 1.0).

function

e

() -> float throws never

Returns Euler's number e, the base of the natural logarithm.

function

golden_ratio

() -> float throws never

Returns the golden ratio φ = (1 + √5) / 2, about 1.618.

function

inf

() -> float throws never

Returns positive infinity. Negate it — -float.inf() — for negative infinity.

function

nan

() -> float throws never

Returns a NaN value. Test for it with x.is_nan(), never with equality.

function

parse

(text: string) -> float throws baml.errors.ParseError

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.ParseError if text cannot 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
function

pi

() -> float throws never

Returns π, the ratio of a circle's circumference to its diameter, to float precision (about 15 decimal digits).

function

random

(rng: baml.random.Rng = …) -> float throws never

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

function

abs

(self) -> float throws never

Returns 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
function

acos

(self) -> float throws never

Inverse cosine, in [0, π]. NaN if self is outside [-1, 1].

function

acosh

(self) -> float throws never

Inverse hyperbolic cosine, defined on [1, ∞). NaN for self < 1.

function

asin

(self) -> float throws never

Inverse sine, in [-π/2, π/2]. NaN if self is outside [-1, 1].

function

asinh

(self) -> float throws never

Inverse hyperbolic sine, defined for every real.

function

atan

(self) -> float throws never

Inverse tangent, in (-π/2, π/2).

function

atan2

(self, other: float) -> float throws never

Returns 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)
function

atanh

(self) -> float throws never

Inverse hyperbolic tangent, defined on (-1, 1). ±∞ at the endpoints, NaN outside them.

function

ceil

(self) -> float throws never

Returns 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
function

cos

(self) -> float throws never

Cosine of self, in radians.

function

cosh

(self) -> float throws never

Hyperbolic cosine.

function

floor

(self) -> float throws never

Returns 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
function

fract

(self) -> float throws never

Returns the fractional part of self, self - self.trunc().

Examples

(3.7).fract()    // 0.7
(-3.7).fract()   // -0.7
(5.0).fract()    // 0.0
function

hypot

(self, other: float) -> float throws never

Returns 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
function

iceil

(self) -> int throws baml.errors.InvalidArgument

Like ceil, but returns int.

Throws

  • baml.errors.InvalidArgument if self is NaN or ±inf, or rounds to a value outside the int range.
function

ifloor

(self) -> int throws baml.errors.InvalidArgument

Like floor, but returns int.

Throws

  • baml.errors.InvalidArgument if self is NaN or ±inf, or rounds to a value outside the int range.
function

iround

(self) -> int throws baml.errors.InvalidArgument

Like round, but returns int.

Throws

  • baml.errors.InvalidArgument if self is NaN or ±inf, or rounds to a value outside the int range.
function

is_finite

(self) -> bool throws never

Returns 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
function

is_infinite

(self) -> bool throws never

Returns 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
function

is_nan

(self) -> bool throws never

Returns 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
function

itrunc

(self) -> int throws baml.errors.InvalidArgument

Like trunc, but returns int.

Throws

  • baml.errors.InvalidArgument if self is NaN or ±inf, or rounds to a value outside the int range.
function

log

(self, base: float) -> float throws never

Returns the logarithm of self in the given base, computed as ln(self) / ln(base).

Every boundary case falls out of that division:

  • self < 0 gives NaN, since ln of a negative is NaN.
  • self == 0 with base > 1 gives -inf: -inf / positive.
  • self == 0 with 0 < base < 1 gives +inf; ln(base) is negative, so the sign flips relative to the case above.
  • self == 0 with base == 1 gives -inf, since ln(1) is +0.0.
  • base < 0 gives NaN.
  • base == 0 with self > 0 gives ±0, signed opposite ln(self), since ±finite / -inf is ∓0.
  • base == 1 with self > 0 and self != 1 gives ±inf, signed to match ln(self), since ±finite / +0.0 is ±inf.
  • base == 1 with self == 1 gives 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
function

pow

(self, exp: float) -> float throws never

Returns 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
function

round

(self) -> float throws never

Returns 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
function

sin

(self) -> float throws never

Sine of self, in radians.

function

sinh

(self) -> float throws never

Hyperbolic sine.

function

sqrt

(self) -> float throws never

Returns 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
function

tan

(self) -> float throws never

Tangent of self, in radians. Very large near odd multiples of π/2.

function

tanh

(self) -> float throws never

Hyperbolic tangent, bounded to (-1, 1).

function

to_degrees

(self) -> float throws never

Converts self from radians to degrees.

function

to_radians

(self) -> float throws never

Converts self from degrees to radians.

function

trunc

(self) -> float throws never

Returns 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 = float

Instance methods

function

add

(self, rhs: float) -> float throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 8570–8717

baml.ops.Add for float

Output = float

Instance methods

function

add

(self, rhs: int) -> float throws never

Source:<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

function

clamp

(self, min: Self, max: Self) -> Self throws never

Clamps 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.

function

ge

(self, other: Self) -> bool throws never

Whether self orders after other or equals it. Defaults to self.cmp(other) != Ordering.Less.

function

gt

(self, other: Self) -> bool throws never

Whether self orders strictly after other. Defaults to self.cmp(other) == Ordering.Greater.

function

le

(self, other: Self) -> bool throws never

Whether self orders before other or equals it. Defaults to self.cmp(other) != Ordering.Greater.

function

lt

(self, other: Self) -> bool throws never

Whether self orders strictly before other. Defaults to self.cmp(other) == Ordering.Less.

function

max

(self, other: Self) -> Self throws never

Ties return self, so max is stable when two values compare Equal without being interchangeable.

function

min

(self, other: Self) -> Self throws never

Ties return self, matching max.

Source:<builtin>/baml/ns_ops/comparison.bamlbytes 11507–11930

baml.ops.Divide for float

Output = float

Instance methods

function

div

(self, rhs: float) -> float throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 9027–9215

baml.ops.Divide for float

Output = float

Instance methods

function

div

(self, rhs: int) -> float throws never

Source:<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

function

eq

(self, other: float) -> bool throws never
function

neq

(self, other: Self) -> bool throws never

Whether 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 = float

Instance methods

function

mul

(self, rhs: float) -> float throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 8873–9025

baml.ops.Multiply for float

Output = float

Instance methods

function

mul

(self, rhs: int) -> float throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 9800–9948

baml.ops.Negate for float

Output = float

Instance methods

function

neg

(self) -> float throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 9372–9503

baml.ops.Remainder for float

Output = float

Instance methods

function

rem

(self, rhs: float) -> float throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 9217–9370

baml.ops.Remainder for float

Output = float

Instance methods

function

rem

(self, rhs: int) -> float throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 10136–10285

baml.ops.Subtract for float

Output = float

Instance methods

function

sub

(self, rhs: float) -> float throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 8719–8871

baml.ops.Subtract for float

Output = float

Instance methods

function

sub

(self, rhs: int) -> float throws never

Source:<builtin>/baml/ns_ops/math.bamlbytes 9650–9798