diff options
author | S. Gilles <sgilles@math.umd.edu> | 2018-03-12 04:24:33 -0400 |
---|---|---|
committer | S. Gilles <sgilles@math.umd.edu> | 2018-03-12 23:43:42 -0400 |
commit | 007b768f79b8a45846062086451484a32c1e30fd (patch) | |
tree | 03494b0e21bdccf32efef19534abcf31185861fb /lib/std/test | |
parent | 373facebac71ac1e6743d30152543627ad115cf7 (diff) | |
download | mc-007b768f79b8a45846062086451484a32c1e30fd.tar.gz |
Make fltXYexplode and fltXYassem inverses of each other
The exponent and significand (mantissa) returned from fltXYexplode
are the numerical values, not just the bit patterns. Informally,
flt64explode(1.23 x 10^101)
would return (false, 123000..., 101), where the significand has
precisely 52 bits after the intial `1'.
Diffstat (limited to 'lib/std/test')
-rw-r--r-- | lib/std/test/fltbits.myr | 63 | ||||
-rw-r--r-- | lib/std/test/fmt.myr | 1 |
2 files changed, 64 insertions, 0 deletions
diff --git a/lib/std/test/fltbits.myr b/lib/std/test/fltbits.myr index 6e3b7c3..1354c70 100644 --- a/lib/std/test/fltbits.myr +++ b/lib/std/test/fltbits.myr @@ -9,6 +9,8 @@ const main = { [.name = "bits-roundtrip-64", .fn = bitsround64], [.name = "flt32bits", .fn = flt32bits], [.name = "flt64bits", .fn = flt64bits], + [.name = "explode-roundtrip-32", .fn = exploderound32], + [.name = "explode-roundtrip-64", .fn = exploderound64], ][:]) } @@ -96,3 +98,64 @@ const flt64bits = {c testr.check(c, u == uprime, "flt64bits wrong for {}: 0x{x} != 0x{x}", f, u, uprime) ;; } + +const exploderound32 = {c + for f : [1.0, 0.00001, 123.45, 1111111111111111.2, -1.9, -0.0001, std.flt32nan()][:] + var n, e, s + (n, e, s) = std.flt32explode(f) + var g = std.flt32assem(n, e, s) + testr.check(c, f == g, "assem o explode non-identity: {} != {}", f, g) + ;; + + /* + The exponents and significands need to be rather specific + in order for flt32assem to work as expected + */ + for (n, e, s) : [ + (false, 0, -127), + (true, 0, -127), + (false, 0x399, -127), + (true, 0x23, -127), + (false, (1 << 23) | 0x23, 45), + (true, (1 << 23) | 0x3a2, -12), + (true, (1 << 23) | 0x3a1, -126), + ][:] + var m, f, t + (m, f, t) = std.flt32explode(std.flt32assem(n, e, s)) + testr.check(c, n == m, "explode o assem non-identity: {} != {}", (n, e, s), (m, f, t)) + testr.check(c, e == f, "explode o assem non-identity: {} != {}", (n, e, s), (m, f, t)) + testr.check(c, s == t, "explode o assem non-identity: {} != {}", (n, e, s), (m, f, t)) + ;; +} + +const exploderound64 = {c + for f : [1.0, 0.00001, 123.45, 1111111111111e+309, -1.9, -0.0001, std.flt64nan()][:] + var n, e, s + (n, e, s) = std.flt64explode(f) + var g = std.flt64assem(n, e, s) + testr.check(c, f == g, "assem o explode non-identity: {} != {}", f, g) + ;; + + /* + The exponents and significands need to be rather specific + in order for flt32assem to work as expected + */ + for (n, e, s) : [ + (false, 0, -1023), + (true, 0, -1023), + (false, 0x399, -1023), + (true, 0x23, -1023), + (false, (1 << 52) | 0xa33bc, 45), + (true, (1 << 52) | 0x3, -12), + (true, (1 << 52) | 0x11aabbcc, -200), + (true, (1 << 52) | 0x3a1, 543), + (true, (1 << 52) | 0x99aa228, 1001), + ][:] + var m, f, t + (m, f, t) = std.flt64explode(std.flt64assem(n, e, s)) + testr.check(c, n == m, "explode o assem non-identity: {} != {}", (n, e, s), (m, f, t)) + testr.check(c, e == f, "explode o assem non-identity: {} != {}", (n, e, s), (m, f, t)) + testr.check(c, s == t, "explode o assem non-identity: {} != {}", (n, e, s), (m, f, t)) + ;; +} + diff --git a/lib/std/test/fmt.myr b/lib/std/test/fmt.myr index da1b76a..acf499f 100644 --- a/lib/std/test/fmt.myr +++ b/lib/std/test/fmt.myr @@ -66,6 +66,7 @@ const builtins = { check("7b", "{x}", 123) check("0x7b", "0x{x}", 123) check("0.0", "{}", 0.0) + check("-0.0", "{}", -0.0) check("0.3", "{}", 0.3) check("0.3", "{}", (0.3 : flt32)) check("1.0", "{}", 1.0) |