-
Notifications
You must be signed in to change notification settings - Fork 8
/
decimal.ex
220 lines (172 loc) · 5.97 KB
/
decimal.ex
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
defmodule RDF.XSD.Decimal do
@moduledoc """
`RDF.XSD.Datatype` for XSD decimals.
"""
@type valid_value :: Decimal.t()
use RDF.XSD.Datatype.Primitive,
name: "decimal",
id: RDF.Utils.Bootstrapping.xsd_iri("decimal")
alias RDF.XSD
alias Elixir.Decimal, as: D
def_applicable_facet XSD.Facets.MinInclusive
def_applicable_facet XSD.Facets.MaxInclusive
def_applicable_facet XSD.Facets.MinExclusive
def_applicable_facet XSD.Facets.MaxExclusive
def_applicable_facet XSD.Facets.TotalDigits
def_applicable_facet XSD.Facets.FractionDigits
def_applicable_facet XSD.Facets.Pattern
@doc false
def min_inclusive_conform?(min_inclusive, value, _lexical) do
not (D.cmp(value, D.new(min_inclusive)) == :lt)
end
@doc false
def max_inclusive_conform?(max_inclusive, value, _lexical) do
not (D.cmp(value, D.new(max_inclusive)) == :gt)
end
@doc false
def min_exclusive_conform?(min_exclusive, value, _lexical) do
D.cmp(value, D.new(min_exclusive)) == :gt
end
@doc false
def max_exclusive_conform?(max_exclusive, value, _lexical) do
D.cmp(value, D.new(max_exclusive)) == :lt
end
@doc false
def total_digits_conform?(total_digits, value, _lexical) do
do_digit_count(to_string(value)) <= total_digits
end
@doc false
def fraction_digits_conform?(fraction_digits, value, _lexical) do
do_fraction_digit_count(to_string(value)) <= fraction_digits
end
@doc false
def pattern_conform?(pattern, _value, lexical) do
XSD.Facets.Pattern.conform?(pattern, lexical)
end
@impl XSD.Datatype
def lexical_mapping(lexical, opts) do
if String.contains?(lexical, ~w[e E]) do
@invalid_value
else
case D.parse(lexical) do
{:ok, decimal} -> elixir_mapping(decimal, opts)
:error -> @invalid_value
end
end
end
@impl XSD.Datatype
@spec elixir_mapping(valid_value | integer | float | any, Keyword.t()) :: value
def elixir_mapping(value, _)
def elixir_mapping(%D{coef: coef}, _) when coef in ~w[qNaN sNaN inf]a,
do: @invalid_value
def elixir_mapping(%D{} = decimal, _),
do: canonical_decimal(decimal)
def elixir_mapping(value, opts) when is_integer(value),
do: value |> D.new() |> elixir_mapping(opts)
def elixir_mapping(value, opts) when is_float(value),
do: value |> D.from_float() |> elixir_mapping(opts)
def elixir_mapping(_, _), do: @invalid_value
@doc false
@spec canonical_decimal(valid_value) :: valid_value
def canonical_decimal(decimal)
def canonical_decimal(%D{coef: 0} = decimal),
do: %{decimal | exp: -1}
def canonical_decimal(%D{coef: coef, exp: 0} = decimal),
do: %{decimal | coef: coef * 10, exp: -1}
def canonical_decimal(%D{coef: coef, exp: exp} = decimal)
when exp > 0,
do: canonical_decimal(%{decimal | coef: coef * 10, exp: exp - 1})
def canonical_decimal(%D{coef: coef} = decimal)
when Kernel.rem(coef, 10) != 0,
do: decimal
def canonical_decimal(%D{coef: coef, exp: exp} = decimal),
do: canonical_decimal(%{decimal | coef: Kernel.div(coef, 10), exp: exp + 1})
@impl XSD.Datatype
@spec canonical_mapping(valid_value) :: String.t()
def canonical_mapping(value)
def canonical_mapping(%D{sign: sign, coef: :qNaN}),
do: if(sign == 1, do: "NaN", else: "-NaN")
def canonical_mapping(%D{sign: sign, coef: :sNaN}),
do: if(sign == 1, do: "sNaN", else: "-sNaN")
def canonical_mapping(%D{sign: sign, coef: :inf}),
do: if(sign == 1, do: "Infinity", else: "-Infinity")
def canonical_mapping(%D{} = decimal),
do: D.to_string(decimal, :normal)
@impl RDF.Literal.Datatype
def do_cast(value)
def do_cast(%XSD.String{} = xsd_string) do
xsd_string.value |> new() |> canonical()
end
def do_cast(literal) do
cond do
XSD.Boolean.datatype?(literal) ->
case literal.value do
false -> new(0.0)
true -> new(1.0)
end
XSD.Integer.datatype?(literal) ->
new(literal.value)
# we're catching XSD.Floats with this too
is_float(literal.value) and XSD.Double.datatype?(literal) ->
new(literal.value)
true ->
super(literal)
end
end
@impl RDF.Literal.Datatype
def do_equal_value_same_or_derived_datatypes?(left, right),
do: XSD.Numeric.do_equal_value?(left, right)
@impl RDF.Literal.Datatype
def do_equal_value_different_datatypes?(left, right),
do: XSD.Numeric.do_equal_value?(left, right)
@impl RDF.Literal.Datatype
def do_compare(left, right), do: XSD.Numeric.do_compare(left, right)
@doc """
The number of digits in the XML Schema canonical form of the literal value.
"""
@spec digit_count(RDF.Literal.t()) :: non_neg_integer | nil
def digit_count(literal)
def digit_count(%RDF.Literal{literal: datatype_literal}), do: digit_count(datatype_literal)
def digit_count(%datatype{} = literal) do
cond do
XSD.Integer.datatype?(datatype) ->
XSD.Integer.digit_count(literal)
datatype?(datatype) and datatype.valid?(literal) ->
literal
|> datatype.canonical_lexical()
|> do_digit_count()
true ->
nil
end
end
@doc false
def do_digit_count(decimal_string) do
decimal_string
|> String.replace(".", "")
|> String.replace("-", "")
|> String.length()
end
@doc """
The number of digits to the right of the decimal point in the XML Schema canonical form of the literal value.
"""
@spec fraction_digit_count(RDF.Literal.t()) :: non_neg_integer | nil
def fraction_digit_count(%RDF.Literal{literal: datatype_literal}),
do: fraction_digit_count(datatype_literal)
def fraction_digit_count(%datatype{} = literal) do
cond do
XSD.Integer.datatype?(literal) ->
0
datatype?(literal) and datatype.valid?(literal) ->
literal
|> datatype.canonical_lexical()
|> do_fraction_digit_count()
true ->
nil
end
end
@doc false
def do_fraction_digit_count(decimal_string) do
[_, fraction] = String.split(decimal_string, ".")
String.length(fraction)
end
end