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}")
}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 withx.isNaN().Float.compare(for sorting) puts it after every number and treats it as equal to itself, andFloat.equals(NaN, NaN)is true. round(x, digits)rounds half away from zero;toFixedgives 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-308static readonly EPSILON: float = 2.220446049250313e-16static 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.0static readonly NEGATIVE_INFINITY: float = -1.0 / 0.0static fn infinity() -> floatstatic fn nan() -> floatstatic fn parse(s: string) -> float—sas a float ("2.5", "-1e3", "inf"); ValueError when it is not one.static fn parseOr(s: string, fallback: float) -> float—sas a float, orfallbackwhen it is not one.static fn tryParse(s: string) -> float?—sas a float, or null when it is not one.static fn isValid(s: string) -> bool— Whether all ofsis a number.static fn isNaN(x: float) -> boolstatic fn isInfinite(x: float) -> bool— Either infinity.static fn isPositiveInfinity(x: float) -> boolstatic fn isNegativeInfinity(x: float) -> boolstatic 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— Whetheraandbdiffer by at mosttolerance.static fn round(x: float, digits: int) -> float— Rounded todigitsafter 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) -> floatstatic fn ceil(x: float) -> floatstatic 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) -> floatstatic fn sign(x: float) -> int— -1, 0 or 1 (0 for NaN).static fn min(a: float, b: float) -> floatstatic fn max(a: float, b: float) -> floatstatic fn clamp(x: float, lo: float, hi: float) -> float—xkept within lo..hi.static fn between(x: float, lo: float, hi: float) -> bool— Whether lo <= x <= hi.static fn sqrt(x: float) -> floatstatic fn pow(x: float, y: float) -> float—xto the powery.static fn lerp(a: float, b: float, t: float) -> float— The value t of the way fromatob(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 asx(0.1 is 0.1).static fn toString(x: float) -> string— The shortest text that reads back asx("0.1", "2500", "nan").static fn toFixed(x: float, digits: int) -> string— Exactlydigitsafter the point ("3.10").static fn toPercent(x: float, digits: int) -> string— As a percentage withdigitsafter the point (0.256 -> "25.6%").