X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=float.4th;h=95aaa3b1f42ea2d6f3596fb5e691fa84be048c30;hb=069f0a8c965a6b7277f5ba8a3f939c69d787838e;hp=4b7382324c8e11cc2e4b3e31f1a333fbab7d3cdc;hpb=7c4b983db5c13783ed581b302becb7e8419123dc;p=scheme.forth.jl.git diff --git a/float.4th b/float.4th index 4b73823..95aaa3b 100644 --- a/float.4th +++ b/float.4th @@ -1,26 +1,44 @@ \ Floating point arithmetic -: lshift 2* ; -: rshift 2/ ; +( Cheating for now by using forth.jl CODE/END-CODE to + access Julia's floating point support. This isn't + at all portable. That said, the year is 2016 and most + CPUs implement these operations - even the trig functions, + so I don't feel too bad! ) -: nlshift 0 do lshift loop ; -: nrshift 0 do rshift loop ; +CODE f+ + b = reinterpret(Float64, popPS()) + a = reinterpret(Float64, popPS()) + pushPS(reinterpret(Int64, a+b)) +END-CODE -1 52 nlshift 1- constant frac-mask -1 11 nlshift 1- 52 nlshift constant exp-mask +CODE f- + b = reinterpret(Float64, popPS()) + a = reinterpret(Float64, popPS()) + pushPS(reinterpret(Int64, a-b)) +END-CODE -: fraction - frac-mask and ; +CODE f* + b = reinterpret(Float64, popPS()) + a = reinterpret(Float64, popPS()) + pushPS(reinterpret(Int64, a*b)) +END-CODE -: exponent - exp-mask and 52 nrshift ; +CODE f/ + b = reinterpret(Float64, popPS()) + a = reinterpret(Float64, popPS()) + pushPS(reinterpret(Int64, a/b)) +END-CODE -: sign ( float -- sign ) - 0> ; +( addr len -- float ) +CODE float-parse + len = popPS() + addr = popPS() + val = parse(Float64, getString(addr, len)) + pushPS(reinterpret(Int64, val)) +END-CODE -: make-float ( sign exponent fraction -- float ) - swap 52 nlshift or - swap false = if - negate - then -; +( float -- ) +CODE float-print + print(reinterpret(Float64, popPS())) +END-CODE \ No newline at end of file