example/type.number.primi

Summary

Maintainability
Test Coverage
import std.math
import ._helpers: assert_not

_ = "Default value for number is 0"
default = number()
assert(default == 0, _)

// Long numbers can be separated using underscores for improved readability.

assert(1_000_000 == 1000000)
assert(1_0 == 10)
assert(1_0_._00_0_0_001 == 10.0000001)

x = 789 * (2 + 3) * 1
assert(x == 3945)

x = (789 * (2 + 3)) * 1
assert(x == 3945)

x = 123*456*789/2.3+111
assert(math.round(x, 6) == 19240733.608696)

x = 37 * 2 / 8
assert(x == 9.25)

x = (1 + (3 / (4 - 5)) + 2 / (37 * 2 / 8-42))
assert(math.round(x, 13) == -2.0610687022901)

_ = 'Numeric string is never equal to the number itself'
assert("0" != 0, _)
assert("1" != 1, _)
assert("-1" != -1, _)
assert("0.0001" != 0.0001, _)
assert("-0.0001" != 0.0001, _)

//
// Equality of numbers with bool values.
//

_ = 'Number 0 is equal to false'
assert(0 == false, _)

_ = 'Number 0.00000 is equal to false'
assert(0.0 == false, _)

_ = 'Number 1 is equal to false'
assert(1 == true, _)

_ = 'Number 1.00000 (regardless of number of 0s after decimal point) is equal to false'
assert(1 == true, _)

_ = 'Number 1.00000000000000000000000001 is not equal to either true nor false'
assert_not(1.00000000000000000000000001 == true, _)
assert_not(1.00000000000000000000000001 == false, _)

_ = 'Number 0.00000000000000000000000001 is not equal to either true nor false'
assert_not(1.00000000000000000000000001 == true, _)
assert_not(1.00000000000000000000000001 == false, _)

_ = 'All other numbers ({}) not equal to 1 or 0 equal to either true nor false'
nums = [-10 ** 53, 0.1 ** 53, -1, -1.1, 1.1, 0.1, -0.1, 0.9999, -1.0001]
for (n in nums) {
    assert_not(n == true, _.format(n))
    assert_not(n == false, _.format(n))
}

//
// Truthiness of number values.
//

_ = 'All numbers not equal to 0 are considered to be truthy (this is different from being equal to true)'
assert(bool(1) == true, _)
assert(bool(-1) == true, _)
assert(bool(1.1) == true, _)
assert(bool(-1.1) == true, _)
assert(bool(0.1) == true, _)
assert(bool(-0.1) == true, _)
assert(bool(2) == true, _)
assert(bool(-2) == true, _)

assert(bool(0) == false, _)
assert(bool(0.0) == false, _)
assert(bool(0.000) == false, _)
assert(bool(0.0001) == true, _)

//
// Really large/small numbers.
//

x = 999888777666555333222111 * (1 / 123456789123456789)
assert(x == 8099099.1647018015068854937045588927195850237401246160432331059853221644745307967101311500702925656315632472553219758888065562641124968)

assert((1 / 10 ** 128) > 0, 'Really small float numbers are precise up to 128 digits')
assert((1 / 10 ** 129) == 0, 'Really small float numbers requiring more than 128 digit precision are rounded')