Download
45 lines 1.4 KiBTextModified 2022-07-29 raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445
a := 3_000 // Underscores can separate chunks
b := 2
x := 3.5
y := 2e3 // Scientific notation
say "Addition: $a+$b=$(a+b) $x+$y=$(x+y)"
say "Subtraction: $a-$b=$(a-b) $x-$y=$(x-y)"
say "Multiplication: $a*$b=$(a*b) $x*$y=$(x*y)"
say "Division: $a/$b=$(a/b) $x/$y=$(x/y)"
say "Modulus: $a mod $b=$(a mod b) $x mod 1=$(x mod 1.)"
say "Negative num modulus: -$a mod $b=$(-a mod b) -$x mod 1=$(-x mod 1.)"
say "Negative modulus: $a mod -$b=$(a mod -b) $x mod -1=$(x mod -1.)"
say "Exponentiation: $a^$b=$(a^b) $x^2.5=$(x^2.5)"

long := 1 + 2 - 3 - 4 + 5 - 6 * 7 * 8 / 2
fail unless long == -167
say "Long chain: $long"

f := 23.5
say "$f casts to int: $(f:Int)"
fail unless f:Int == 23
say "$f reinterpreted as int: $(f:Value:Int)"

i := 123
say "$i casts to float: $(i:Num)"
fail unless i:Num == 123.0
say "$i reinterpreted as float: $(i:Value:Num)"

fail unless 23.5:Int8 == 23:Int8
fail "23:Num32 = $(23:Num32)" unless 23:Num32 == 23.0:Num32

def fast_inverse_sqrt(number:Num):Num
    threehalfs := 1.5
    x2 := number * 0.5
    y := number
    i := number:Value:Int
    i = 0x5FE6EB50C7B537A9 - (i / 2) // what the fuck? 
    y = i:Value:Num
    y *= (threehalfs - (x2 * y * y)) // 1st iteration
    y *= (threehalfs - (x2 * y * y)) // 2nd iteration, this can be removed
    y *= (threehalfs - (x2 * y * y)) // 3rd iteration, this can be removed
    return y

test := 16.
say "fast_inverse_sqrt($test) = $(fast_inverse_sqrt test)"
fail unless "$(fast_inverse_sqrt(16.))" == "0.25"