Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-std

Source of release 0.7.0

0.7.0 / tests / unit / math_test.lucb

## The `math` and `math32` modules against their archive: rounding, roots, powers,
## logarithms and the decompositions.
# valgrind: skip -- valgrind does not reproduce floating-point results bit for bit
import math
import math32

test "rounding, roots, powers and logarithms":
    assert(math.floor(2.7) == 2.0 and math.ceil(2.1) == 3.0 and math.trunc(-2.7) == -2.0 and math.round(2.5) == 3.0)
    assert(math.sqrt(16.0) == 4.0 and math.hypot(3.0, 4.0) == 5.0)
    # a C library may round the cube root by a unit in the last place
    let root = math.cbrt(27.0)
    assert(root > 2.999999999999 and root < 3.000000000001)
    assert(math.pow(2.0, 10.0) == 1024.0 and math.exp2(3.0) == 8.0 and math.log2(8.0) == 3.0 and math.log10(1000.0) == 3.0)
    assert(math.mod(7.5, 2.0) == 1.5)
    let (mantissa, exponent) = math.frexp(8.0)
    assert(mantissa == 0.5 and exponent == 4)
    assert(math.scalbn(0.5, 4) == 8.0)
    let (fraction, whole) = math.modf(2.75)
    assert(whole == 2.0 and fraction == 0.75)
    assert(math.next_up(1.0) > 1.0 and math.next_down(1.0) < 1.0 and math.nextafter(1.0, 2.0) == math.next_up(1.0))
    assert(math.fma(2.0, 3.0, 1.0) == 7.0)
    assert(math.infinity > 1.0e308 and math.nan != math.nan)

test "single precision agrees":
    assert(math32.floor(2.7) == 2.0 and math32.ceil(2.1) == 3.0 and math32.trunc(-2.7) == -2.0 and math32.round(2.5) == 3.0)
    assert(math32.sqrt(16.0) == 4.0 and math32.hypot(3.0, 4.0) == 5.0)
    assert(math32.pow(2.0, 10.0) == 1024.0 and math32.exp2(3.0) == 8.0 and math32.log2(8.0) == 3.0)
    let (mantissa, exponent) = math32.frexp(8.0)
    assert(mantissa == 0.5 and exponent == 4)
    assert(math32.scalbn(0.5, 4) == 8.0 and math32.fma(2.0, 3.0, 1.0) == 7.0)
    assert(math32.infinity > 3.0e38 and math32.nan != math32.nan)