dymokomi/luce-std
Source of release 0.7.1
0.7.1 / tests / math32 / math.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
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(floor(2.7) == 2.0 and ceil(2.1) == 3.0 and trunc(-2.7) == -2.0 and round(2.5) == 3.0)
assert(sqrt(16.0) == 4.0 and hypot(3.0, 4.0) == 5.0)
assert(pow(2.0, 10.0) == 1024.0 and exp2(3.0) == 8.0 and log2(8.0) == 3.0)
let (mantissa, exponent) = frexp(8.0)
assert(mantissa == 0.5 and exponent == 4)
assert(scalbn(0.5, 4) == 8.0 and fma(2.0, 3.0, 1.0) == 7.0)
assert(infinity > 3.0e38 and nan != nan)
## Within `ulps` units in the last place of `expected`, both finite and of one sign.
func near(actual: f64, expected: f64, ulps: u64) -> bool:
if math.signbit(actual) != math.signbit(expected):
return false
let a = math.abs(actual).bits()
let b = math.abs(expected).bits()
return (a - b if a >= b else b - a) <= ulps
test "the error function against Python's math.erf and math.erfc":
# the values Python 3 prints for math.erf(x) and math.erfc(x)
assert(near(math.erf(0.5), 0.5204998778130465, 2) and near(math.erfc(0.5), 0.4795001221869535, 2))
assert(near(math.erf(1.0), 0.8427007929497148, 2) and near(math.erfc(1.0), 0.15729920705028516, 2))
assert(near(math.erf(-2.0), -0.9953222650189527, 2) and near(math.erfc(-2.0), 1.9953222650189528, 2))
assert(near(math.erf(3.0), 0.9999779095030015, 2) and near(math.erfc(3.0), 2.2090496998585438e-05, 2))
assert(near(math.erfc(10.0), 2.0884875837625446e-45, 2) and near(math.erfc(26.0), 5.663192408856143e-296, 2))
assert(near(math.erfc(-1.0), 1.8427007929497148, 2))
# the odd symmetry, signed zero, the limits and NaN
assert(math.erf(0.0) == 0.0 and not math.signbit(math.erf(0.0)) and math.signbit(math.erf(-0.0)))
assert(math.erfc(0.0) == 1.0 and math.erfc(-0.0) == 1.0)
assert(math.erf(math.infinity) == 1.0 and math.erf(-math.infinity) == -1.0 and math.erf(7.0) == 1.0)
assert(math.erfc(math.infinity) == 0.0 and math.erfc(-math.infinity) == 2.0 and math.erfc(30.0) == 0.0)
assert(math.is_nan(math.erf(math.nan)) and math.is_nan(math.erfc(math.nan)))
for step in 0..<600:
let x = (f64)step / 100.0
assert(math.erf(-x) == -math.erf(x))
assert(math.abs(math.erf(x) + math.erfc(x) - 1.0) <= 2.3e-16)
# single precision rounds the f64 result once
assert(erf(1.0) == (f32)0.8427007929497148 and erfc(3.0) == (f32)2.2090496998585438e-05)
assert(signbit(erf(-0.0)) and erfc(infinity) == 0.0 and is_nan(erf(nan)))