Added draft readflonum.
[scheme.forth.jl.git] / src / float.4th
1 \ Floating point arithmetic
2
3 ( Cheating for now by using forth.jl CODE/END-CODE to
4   access Julia's floating point support.  This isn't
5   at all portable.  That said, the year is 2016 and most
6   CPUs implement these operations - even the trig functions,
7   so I don't feel too bad! )
8
9 CODE f+
10     b = reinterpret(Float64, popPS())
11     a = reinterpret(Float64, popPS())
12     pushPS(reinterpret(Int64, a+b))
13 END-CODE
14
15 CODE f-
16     b = reinterpret(Float64, popPS())
17     a = reinterpret(Float64, popPS())
18     pushPS(reinterpret(Int64, a-b))
19 END-CODE
20
21 CODE f*
22     b = reinterpret(Float64, popPS())
23     a = reinterpret(Float64, popPS())
24     pushPS(reinterpret(Int64, a*b))
25 END-CODE
26
27 CODE f/
28     b = reinterpret(Float64, popPS())
29     a = reinterpret(Float64, popPS())
30     pushPS(reinterpret(Int64, a/b))
31 END-CODE
32
33 CODE f^
34     b = reinterpret(Float64, popPS())
35     a = reinterpret(Float64, popPS())
36     pushPS(reinterpret(Int64, a^b))
37 END-CODE
38
39 CODE f>
40     b = reinterpret(Float64, popPS())
41     a = reinterpret(Float64, popPS())
42     pushPS(reinterpret(Int64, a>b))
43 END-CODE
44
45 CODE f<
46     b = reinterpret(Float64, popPS())
47     a = reinterpret(Float64, popPS())
48     pushPS(reinterpret(Int64, a<b))
49 END-CODE
50
51 CODE f=
52     b = reinterpret(Float64, popPS())
53     a = reinterpret(Float64, popPS())
54     pushPS(reinterpret(Int64, a=b))
55 END-CODE
56
57 CODE f<=
58     b = reinterpret(Float64, popPS())
59     a = reinterpret(Float64, popPS())
60     pushPS(reinterpret(Int64, a<=b))
61 END-CODE
62
63 CODE f>=
64     b = reinterpret(Float64, popPS())
65     a = reinterpret(Float64, popPS())
66     pushPS(reinterpret(Int64, a>=b))
67 END-CODE
68
69 CODE flog
70     b = reinterpret(Float64, popPS())
71     pushPS(reinterpret(Int64, log(a)))
72 END-CODE
73
74 CODE fexp
75     b = reinterpret(Float64, popPS())
76     pushPS(reinterpret(Int64, exp(a)))
77 END-CODE
78
79 CODE i->f
80     pushPS(reinterpret(Int64, Float64(popPS())))
81 END-CODE
82
83 : f.scientific ( float -- )
84 ;
85
86 : f.plain ( float -- )
87
88 ;
89
90 : f. ( float -- )
91     dup dup 1000000 i->f f>= swap 1 i->f 10000 i->f f/ f< or if
92         f.scientific
93     else
94         f.plain
95     then
96 ;