.nib std
nib 0.3.1-pre

Pre-release documentation. This describes nib 0.3.1-pre, which is on main and has no release yet: what it documents beyond 0.3.0 may change before it ships.

module always in scope

Float

Crossing between Float and Int, and the rounding and roots an operator cannot spell. The arithmetic itself is the dotted operators +. -. *. /..

Float is a 64-bit IEEE 754 number. Its arithmetic is written with dotted operators, as in OCaml: +., -., *., /., and -. in front of a value to negate it. + and friends are Int-only, so the operator says which number you mean and inference never has to guess: func mean :: a, b = (a +. b) /. 2.0 is Float, Float -> Float with nothing annotated. A minus written directly on a literal is part of it, so -1.5 is a Float.

let r = 2.0;
3.14159 *. r *. r
12.56636

Division by zero is not a fault: 1.0 /. 0.0 is infinity and 0.0 /. 0.0 is NaN, as IEEE says. There is no % on Floats. <, <=, > and >= order Floats as they do Ints.

This namespace is what is left: conversions, rounding and a root. Nothing converts between Int and Float implicitly.

Functions

funcof_int
func of_int :: Int -> Float

The Float with the same value as an Int.

Float.of_int(7) /. 2.0
3.5
functo_int
func to_int :: Float -> Int raises Err.OutOfRange

The Int part, truncated toward zero. A Float with no Int value (NaN, an infinity, or one past the Int range) raises Err.OutOfRange, as an Int / by zero raises Err.DivisionByZero; ask is_finite first when the value may be one. To round to the nearest Int, round first:

(Float.to_int(-2.9), Float.to_int(Float.round(2.5)))
(-2, 3)
try Float.to_int(0.0 /. 0.0) rescue Err.OutOfRange(_) => -1 end
-1
func is_finite :: Float -> Bool

true unless the value is NaN or an infinity.

(Float.is_finite(1.5), Float.is_finite(1.0 /. 0.0))
(true, false)
funcround
func round :: Float -> Float

The nearest whole number, as a Float. A half rounds away from zero, so round(-2.5) is -3.0.

(Float.round(2.5), Float.round(-2.5))
(3.0, -3.0)
funcfloor
func floor :: Float -> Float

The largest whole number not above the value, as a Float.

Float.floor(-2.5)
-3.0
funcceil
func ceil :: Float -> Float

The smallest whole number not below the value, as a Float.

Float.ceil(2.1)
3.0
funcabs
func abs :: Float -> Float

The value without its sign.

Float.abs(-4.25)
4.25
funcsqrt
func sqrt :: Float -> Float

The square root. A negative argument answers NaN.

Float.sqrt(16.0)
4.0