/
float.cr
226 lines (188 loc) · 4.02 KB
/
float.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
require "c/stdio"
require "c/string"
require "./float/printer"
# Float is the base type of all floating point numbers.
#
# There are two floating point types, `Float32` and `Float64`,
# which correspond to the [binary32](http://en.wikipedia.org/wiki/Single_precision_floating-point_format)
# and [binary64](http://en.wikipedia.org/wiki/Double_precision_floating-point_format)
# types defined by IEEE.
#
# A floating point literal is an optional `+` or `-` sign, followed by
# a sequence of numbers or underscores, followed by a dot,
# followed by numbers or underscores, followed by an optional exponent suffix,
# followed by an optional type suffix. If no suffix is present, the literal's type is `Float64`.
#
# ```
# 1.0 # Float64
# 1.0_f32 # Float32
# 1_f32 # Float32
#
# 1e10 # Float64
# 1.5e10 # Float64
# 1.5e-7 # Float64
#
# +1.3 # Float64
# -0.5 # Float64
# ```
#
# The underscore `_` before the suffix is optional.
#
# Underscores can be used to make some numbers more readable:
#
# ```
# 1_000_000.111_111 # better than 1000000.111111
# ```
struct Float
alias Primitive = Float32 | Float64
def -
self.class.zero - self
end
def %(other)
modulo(other)
end
def nan?
!(self == self)
end
def infinite?
if nan? || self == 0 || self != 2 * self
nil
else
self > 0 ? 1 : -1
end
end
def finite?
!nan? && !infinite?
end
def fdiv(other)
self / other
end
def modulo(other)
if other == 0.0
raise DivisionByZeroError.new
else
self - other * self.fdiv(other).floor
end
end
def remainder(other)
if other == 0.0
raise DivisionByZeroError.new
else
mod = self % other
return self.class.zero if mod == 0.0
return mod if self > 0 && other > 0
return mod if self < 0 && other < 0
mod - other
end
end
# See `Object#hash(hasher)`
def hash(hasher)
hasher.float(self)
end
# Writes this float to the given *io* in the given *format*.
# See also: `IO#write_bytes`.
def to_io(io : IO, format : IO::ByteFormat)
format.encode(self, io)
end
# Reads a float from the given *io* in the given *format*.
# See also: `IO#read_bytes`.
def self.from_io(io : IO, format : IO::ByteFormat) : self
format.decode(self, io)
end
end
struct Float32
NAN = (0_f32 / 0_f32).as Float32
INFINITY = (1_f32 / 0_f32).as Float32
MIN = (-INFINITY).as Float32
MAX = INFINITY.as Float32
# Returns a `Float32` by invoking `to_f32` on *value*.
def self.new(value)
value.to_f32
end
def ceil
LibM.ceil_f32(self)
end
def floor
LibM.floor_f32(self)
end
def round
LibM.round_f32(self)
end
def trunc
LibM.trunc_f32(self)
end
def **(other : Int32)
{% if flag?(:win32) %}
self ** other.to_f32
{% else %}
LibM.powi_f32(self, other)
{% end %}
end
def **(other : Float32)
LibM.pow_f32(self, other)
end
def **(other)
self ** other.to_f32
end
def to_s
String.build(22) do |buffer|
Printer.print(self, buffer)
end
end
def to_s(io : IO)
Printer.print(self, io)
end
def inspect(io)
to_s(io)
io << "_f32"
end
def clone
self
end
end
struct Float64
NAN = (0_f64 / 0_f64).as Float64
INFINITY = (1_f64 / 0_f64).as Float64
MIN = (-INFINITY).as Float64
MAX = INFINITY.as Float64
# Returns a `Float64` by invoking `to_f64` on *value*.
def Float64.new(value)
value.to_f64
end
def ceil
LibM.ceil_f64(self)
end
def floor
LibM.floor_f64(self)
end
def round
LibM.round_f64(self)
end
def trunc
LibM.trunc_f64(self)
end
def **(other : Int32)
{% if flag?(:win32) %}
self ** other.to_f64
{% else %}
LibM.powi_f64(self, other)
{% end %}
end
def **(other : Float64)
LibM.pow_f64(self, other)
end
def **(other)
self ** other.to_f64
end
def to_s
String.build(22) do |buffer|
Printer.print(self, buffer)
end
end
def to_s(io : IO)
Printer.print(self, io)
end
def clone
self
end
end