X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=src%2Ffloat.4th;h=e100b4682e03409e293906863bcc6312b686cce6;hb=631c05ef31816dfd2c7e8ea0f19a008a1e732605;hp=2305749a9a8a51fcfee643d5a38a521fba17ac7d;hpb=a7e883b206fc496d7c0faaf713bb7bdd91ff29d3;p=scheme.forth.jl.git diff --git a/src/float.4th b/src/float.4th index 2305749..e100b46 100644 --- a/src/float.4th +++ b/src/float.4th @@ -2,9 +2,9 @@ ( 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! ) + at all portable. That said, the year is 2016 and + I've only cheated for words that have corresponding + x87 FPU instructions, so I don't feel too bad! ) CODE f+ b = reinterpret(Float64, popPS()) @@ -36,6 +36,11 @@ CODE f^ pushPS(reinterpret(Int64, a^b)) END-CODE +CODE fsqrt + a = reinterpret(Float64, popPS()) + pushPS(reinterpret(Int64, sqrt(a))) +END-CODE + CODE f> b = reinterpret(Float64, popPS()) a = reinterpret(Float64, popPS()) @@ -88,6 +93,26 @@ CODE fexp pushPS(reinterpret(Int64, exp(a))) END-CODE +CODE fsin + a = reinterpret(Float64, popPS()) + pushPS(reinterpret(Int64, sin(a))) +END-CODE + +CODE fcos + a = reinterpret(Float64, popPS()) + pushPS(reinterpret(Int64, cos(a))) +END-CODE + +CODE ftan + a = reinterpret(Float64, popPS()) + pushPS(reinterpret(Int64, tan(a))) +END-CODE + +CODE fatan + a = reinterpret(Float64, popPS()) + pushPS(reinterpret(Int64, atan(a))) +END-CODE + CODE fnan? a = reinterpret(Float64, popPS()) if isnan(a) @@ -115,6 +140,11 @@ CODE f->i pushPS(Int64(round(a))) END-CODE +CODE fabs + a = reinterpret(Float64, popPS()) + pushPS(reinterpret(Int64, abs(a))) +END-CODE + : f/mod 2dup fmod -rot f/ ; @@ -133,12 +163,6 @@ END-CODE : flog10 flog [ 10 i->f flog ] literal f/ ; -: fabs - dup 0.0 f< if - -1.0 f* - then -; - : floor dup 0.0 f>= if dup 1.0 fmod f- @@ -151,6 +175,19 @@ END-CODE then ; +: fasin ( float -- res ) + dup + dup f* 1.0 swap f- fsqrt + f/ + + fatan +; + +: facos ( float -- res ) + dup f* 1.0 swap f/ 1.0 f- fsqrt + fatan +; + : fhead ( float -- ) floor f->i 0 .R ; @@ -210,12 +247,13 @@ variable precision ." e" f->i 0 .R ; -: f. ( float -- ) +: f.nospace ( float -- ) dup fabs dup 1000000 i->f f>= swap 1 i->f 10000 i->f f/ f< or if f.scientific else f.plain then - - space ; + +: f. ( float -- ) + f.nospace space ;