Grav v1.14.0

Float

import "std/types" (included in import "std") · namespace std.types

Float holds what floating-point numbers can do beyond arithmetic, as methods of any float (x.round(2) is Float.round(x, 2)), with the special values named: NaN and Infinity (also Float.NAN, Float.INFINITY, Float.NEGATIVE_INFINITY), which print as NaN, Infinity and -Infinity.

grav
import "std"

fn main() {
    let x = 3.14159
    print("${x.round(2)} ${x.toFixed(4)} ${2.75.floor()} ${0.256.toPercent(1)}")
    print("${Float.parse("2.5e3")} ${0.1.toDecimal()} ${7.0.isWhole()}")
    let r = 0.0 / 0.0
    print("${r} ${r.isNaN()} ${r == NaN} ${1.0 / 0.0 == Infinity} ${-Infinity}")
}
Output
3.14 3.1416 2 25.6%
2500 0.1 true
NaN true false true -Infinity
  • NaN is not == to anything, itself included: test it with x.isNaN(). Float.compare (for sorting) puts it after every number and treats it as equal to itself, and Float.equals(NaN, NaN) is true.
  • round(x, digits) rounds half away from zero; toFixed gives text with exactly that many digits.

API

class Float

Floating-point numbers (see the top of this file).

Static members

  • static readonly MAX: float = 1.7976931348623157e308 — The largest finite float, the smallest positive normal one, and the gap between 1.0 and the next float.
  • static readonly MIN_POSITIVE: float = 2.2250738585072014e-308
  • static readonly EPSILON: float = 2.220446049250313e-16
  • static readonly NAN: float = 0.0 / 0.0 — Not-a-number (what 0.0 / 0.0 gives; it is not == to anything, itself included: test with isNaN), and the infinities. Also the globals NaN and Infinity.
  • static readonly INFINITY: float = 1.0 / 0.0
  • static readonly NEGATIVE_INFINITY: float = -1.0 / 0.0
  • static fn infinity() -> float
  • static fn nan() -> float
  • static fn parse(s: string) -> float — s as a float ("2.5", "-1e3", "inf"); ValueError when it is not one.
  • static fn parseOr(s: string, fallback: float) -> float — s as a float, or fallback when it is not one.
  • static fn tryParse(s: string) -> float? — s as a float, or null when it is not one.
  • static fn isValid(s: string) -> bool — Whether all of s is a number.
  • static fn isNaN(x: float) -> bool
  • static fn isInfinite(x: float) -> bool — Either infinity.
  • static fn isPositiveInfinity(x: float) -> bool
  • static fn isNegativeInfinity(x: float) -> bool
  • static fn isFinite(x: float) -> bool — Neither NaN nor infinite.
  • static fn isWhole(x: float) -> bool — Whether it has no fraction (3.0, not 3.5; NaN and infinities are not).
  • static fn approxEquals(a: float, b: float, tolerance: float) -> bool — Whether a and b differ by at most tolerance.
  • static fn round(x: float, digits: int) -> float — Rounded to digits after the point, half away from zero (3.14159 -> 3.14).
  • static fn round(x: float) -> float — Rounded to the nearest whole number, half away from zero.
  • static fn floor(x: float) -> float
  • static fn ceil(x: float) -> float
  • static fn trunc(x: float) -> float — Toward zero (-2.7 -> -2).
  • static fn fraction(x: float) -> float — The part after the point, with the sign (-2.75 -> -0.75).
  • static fn abs(x: float) -> float
  • static fn sign(x: float) -> int — -1, 0 or 1 (0 for NaN).
  • static fn min(a: float, b: float) -> float
  • static fn max(a: float, b: float) -> float
  • static fn clamp(x: float, lo: float, hi: float) -> float — x kept within lo..hi.
  • static fn between(x: float, lo: float, hi: float) -> bool — Whether lo <= x <= hi.
  • static fn sqrt(x: float) -> float
  • static fn pow(x: float, y: float) -> float — x to the power y.
  • static fn lerp(a: float, b: float, t: float) -> float — The value t of the way from a to b (t = 0 gives a, 1 gives b).
  • static fn compare(a: float, b: float) -> int — A comparator for sorting (xs.sortBy(Float.compare)): -1, 0 or 1, in a total order (-0.0 before 0.0, NaN after everything, equal to itself).
  • static fn descending(a: float, b: float) -> int — Largest first (NaN last).
  • static fn compareTo(a: float, b: float) -> int — a.compareTo(b): as compare.
  • static fn equals(a: float, b: float) -> bool — Whether both are the same number (NaN equals NaN here, unlike ==).
  • static fn toLong(x: float) -> long — The whole part as a long, toward zero; ArithmeticError for NaN, infinities and values past a long.
  • static fn roundToLong(x: float) -> long — Rounded to the nearest long (half away from zero); ArithmeticError as toLong.
  • static fn toDecimal(x: float) -> Decimal — The shortest exact Decimal that reads back as x (0.1 is 0.1).
  • static fn toString(x: float) -> string — The shortest text that reads back as x ("0.1", "2500", "nan").
  • static fn toFixed(x: float, digits: int) -> string — Exactly digits after the point ("3.10").
  • static fn toPercent(x: float, digits: int) -> string — As a percentage with digits after the point (0.256 -> "25.6%").

Edit this page on GitHub