| 1 | (* Reals at their edges, as x86-64 gives them: a NaN equal to nothing, |
| 2 | a real out of an I64's range or a NaN converted to the smallest I64, |
| 3 | then cut; -0.0; F32's rounding; a U64 above high(I64); reals as |
| 4 | arguments past v7, results, in records, slices and registers *) |
| 5 | module Reals; |
| 6 | import Out, Fmt; |
| 7 | |
| 8 | type Pt = record x: F64; y: F32; n: INT; end; |
| 9 | |
| 10 | var zero, one: F64; |
| 11 | |
| 12 | proc real(x: F64); |
| 13 | var b: [Fmt.ShortMost]U8; |
| 14 | begin |
| 15 | Out.str(Fmt.short(x, b)); Out.str(" "); |
| 16 | end; |
| 17 | |
| 18 | proc bool(b: BOOL); |
| 19 | begin |
| 20 | if b then Out.str("T"); else Out.str("F"); end; |
| 21 | end; |
| 22 | |
| 23 | (* every comparison of a and b *) |
| 24 | proc compare(a, b: F64); |
| 25 | begin |
| 26 | bool(a = b); bool(a # b); bool(a < b); bool(a <= b); bool(a > b); bool(a >= b); |
| 27 | Out.str(" "); |
| 28 | end; |
| 29 | |
| 30 | proc compare32(a, b: F32); |
| 31 | begin |
| 32 | bool(a = b); bool(a # b); bool(a < b); bool(a <= b); bool(a > b); bool(a >= b); |
| 33 | Out.str(" "); |
| 34 | end; |
| 35 | |
| 36 | (* x converted to each integer width *) |
| 37 | proc toInt(x: F64); |
| 38 | begin |
| 39 | Out.int(I64(x)); Out.str(" "); Out.int(I64(I32(x))); Out.str(" "); |
| 40 | Out.int(I64(I16(x))); Out.str(" "); Out.int(I64(U8(x))); Out.str(" "); |
| 41 | Out.int(I64(F32(x))); Out.ln; |
| 42 | end; |
| 43 | |
| 44 | (* ten reals and ten integers: the ninth and tenth of each on the stack *) |
| 45 | proc many(a, b, c, d, e, f, g, h, i, j: F64; k, l, m, n, o, p, q, r: INT; s: F32; t: INT): F64; |
| 46 | begin |
| 47 | return a + b * 2.0 + c * 3.0 + d * 4.0 + e * 5.0 + f * 6.0 + g * 7.0 + h * 8.0 |
| 48 | + i * 9.0 + j * 10.0 + F64(k + l + m + n + o + p + q + r + t) + F64(s); |
| 49 | end; |
| 50 | |
| 51 | proc half(x: F32): F32; |
| 52 | begin |
| 53 | return x / 2.0; |
| 54 | end; |
| 55 | |
| 56 | proc swapped(p: Pt): (F32, F64); |
| 57 | begin |
| 58 | return p.y, p.x; |
| 59 | end; |
| 60 | |
| 61 | proc main*; |
| 62 | var |
| 63 | nan, inf, nz, x, sum: F64; |
| 64 | f, g: F32; |
| 65 | u: U64; |
| 66 | p: Pt; |
| 67 | a: [5]F64; |
| 68 | s: []F64; |
| 69 | i: INT; |
| 70 | begin |
| 71 | nan := zero / zero; |
| 72 | inf := one / zero; |
| 73 | nz := -zero; |
| 74 | real(nan); real(inf); real(-inf); real(nz); real(ABS(nz)); real(-nz); real(one / nz); Out.ln; |
| 75 | compare(nan, nan); compare(nan, one); compare(one, nan); compare(one, one); compare(nz, zero); Out.ln; |
| 76 | f := F32(nan); g := 1.5; |
| 77 | compare32(f, g); compare32(g, f); compare32(g, g); Out.ln; |
| 78 | real(ABS(nan)); real(-nan); bool(ABS(nan) = ABS(nan)); Out.ln; |
| 79 | toInt(nan); toInt(inf); toInt(-inf); toInt(1.0E300); toInt(-1.0E300); |
| 80 | toInt(9.3E18); toInt(-9.3E18); toInt(9.2233720368547748E18); toInt(-9.2233720368547758E18); |
| 81 | toInt(2.75); toInt(-2.75); toInt(70000.9); toInt(-0.5); toInt(300.0); toInt(nz); |
| 82 | (* F32's rounding *) |
| 83 | f := F32(0.1); real(F64(f)); f := F32(16777217.0); real(F64(f)); |
| 84 | f := 0.1; g := 0.2; real(F64(f + g)); real(F64(f * g)); real(F64(f / g)); real(F64(f - g)); |
| 85 | f := F32(1.0E39); real(F64(f)); f := F32(1.0E-46); real(F64(f)); real(F64(F32(-1.0E-46))); |
| 86 | g := 3.4028234E38; real(F64(g * 2.0)); real(F64(half(3.0))); Out.ln; |
| 87 | (* integers to reals *) |
| 88 | u := MAX(U64); real(F64(u)); real(F64(F32(u))); u := 8000000000000401H; real(F64(u)); |
| 89 | real(F64(MIN(I64))); i := 16777217; real(F64(F32(i))); real(F64(I8(-7))); Out.ln; |
| 90 | (* calls, records, slices, registers *) |
| 91 | real(many(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 1, 2, 3, 4, 5, 6, 7, 8, 0.25, 9)); |
| 92 | p.x := 2.5; p.y := -1.25; p.n := 3; |
| 93 | f, x := swapped(p); real(F64(f)); real(x); |
| 94 | for i := 0 to 4 do a[i] := F64(i) * 0.5; end; |
| 95 | s := a[1..5]; |
| 96 | FILL(s[2..4], -3.5); |
| 97 | sum := 0.0; |
| 98 | for i := 0 to LEN(s) - 1 do sum := sum + s[i] * F64(i + 1); end; |
| 99 | real(sum); real(a[3]); real(a[0]); Out.ln; |
| 100 | end; |
| 101 | |
| 102 | begin |
| 103 | one := 1.0; |
| 104 | end Reals. |