/
number.cr
180 lines (160 loc) · 5.41 KB
/
number.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
struct BigInt
Number.expand_div [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128], BigFloat
Number.expand_div [Float32, Float64], BigFloat
end
struct BigFloat
def fdiv(other : Number::Primitive) : self
self.class.new(self / other)
end
def /(other : Int::Primitive) : BigFloat
# Division by 0 in BigFloat is not allowed, there is no BigFloat::Infinity
raise DivisionByZeroError.new if other == 0
Int.primitive_ui_check(other) do |ui, neg_ui, _|
{
ui: BigFloat.new { |mpf| LibGMP.mpf_div_ui(mpf, self, {{ ui }}) },
neg_ui: BigFloat.new { |mpf| LibGMP.mpf_div_ui(mpf, self, {{ neg_ui }}); LibGMP.mpf_neg(mpf, mpf) },
big_i: BigFloat.new { |mpf| LibGMP.mpf_div(mpf, self, BigFloat.new(other)) },
}
end
end
Number.expand_div [Float32, Float64], BigFloat
end
struct BigDecimal
Number.expand_div [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128], BigDecimal
Number.expand_div [Float32, Float64], BigDecimal
end
struct BigRational
Number.expand_div [Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128], BigRational
Number.expand_div [Float32, Float64], BigRational
end
struct Int
# :nodoc:
# Yields 3 expressions: `Call`s nodes that convert *var* into a `LibGMP::SI`,
# a `LibGMP::UI`, and a `BigInt` respectively. These expressions are not
# evaluated unless they are interpolated in *block*.
#
# *block* should return a named tuple: the value for `:si` is returned by the
# macro if *var* fits into a `LibGMP::SI`, the value for `:ui` returned if
# *var* fits into a `LibGMP::UI`, and the value for `:big_i` otherwise.
macro primitive_si_ui_check(var, &block)
{%
exps = yield(
"::LibGMP::SI.new!(#{var.id})".id,
"::LibGMP::UI.new!(#{var.id})".id,
"::BigInt.new(#{var.id})".id,
)
%}
if ::LibGMP::SI::MIN <= {{ var }} <= ::LibGMP::UI::MAX
if {{ var }} <= ::LibGMP::SI::MAX
{{ exps[:si] }}
else
{{ exps[:ui] }}
end
else
{{ exps[:big_i] }}
end
end
# :nodoc:
# Yields 3 expressions: `Call`s nodes that convert *var* into a `LibGMP::UI`,
# the negative of *var* into a `LibGMP::UI`, and *var* into a `BigInt`,
# respectively. These expressions are not evaluated unless they are
# interpolated in *block*.
#
# *block* should return a named tuple: the value for `:ui` is returned by the
# macro if *var* fits into a `LibGMP::UI`, the value for `:neg_ui` returned if
# the negative of *var* fits into a `LibGMP::UI`, and the value for `:big_i`
# otherwise.
macro primitive_ui_check(var, &block)
{%
exps = yield(
"::LibGMP::UI.new!(#{var.id})".id,
"::LibGMP::UI.new!((#{var.id}).abs_unsigned)".id,
"::BigInt.new(#{var.id})".id,
)
%}
if ::LibGMP::UI::MIN <= {{ var }} <= ::LibGMP::UI::MAX
{{ exps[:ui] }}
elsif {{ var }}.responds_to?(:abs_unsigned) && {{ var }}.abs_unsigned <= ::LibGMP::UI::MAX
{{ exps[:neg_ui] }}
else
{{ exps[:big_i] }}
end
end
end
struct Int8
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end
struct Int16
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end
struct Int32
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end
struct Int64
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end
struct Int128
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end
struct UInt8
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end
struct UInt16
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end
struct UInt32
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end
struct UInt64
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end
struct UInt128
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end
struct Float
def fdiv(other : BigInt | BigFloat | BigDecimal | BigRational) : self
self.class.new(self / other)
end
end
struct Float32
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end
struct Float64
Number.expand_div [BigInt], BigFloat
Number.expand_div [BigFloat], BigFloat
Number.expand_div [BigDecimal], BigDecimal
Number.expand_div [BigRational], BigRational
end