summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/Intl/MathematicalValue.cpp
blob: b14859ceee4c2b05e9b14f476563ea0f366e6c84 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/MathematicalValue.h>
#include <math.h>

namespace JS::Intl {

bool MathematicalValue::is_number() const
{
    return m_value.has<double>();
}

double MathematicalValue::as_number() const
{
    VERIFY(is_number());
    return m_value.get<double>();
}

bool MathematicalValue::is_bigint() const
{
    return m_value.has<Crypto::SignedBigInteger>();
}

Crypto::SignedBigInteger const& MathematicalValue::as_bigint() const
{
    VERIFY(is_bigint());
    return m_value.get<Crypto::SignedBigInteger>();
}

bool MathematicalValue::is_mathematical_value() const
{
    return is_number() || is_bigint();
}

bool MathematicalValue::is_positive_infinity() const
{
    if (is_mathematical_value())
        return false;
    return m_value.get<Symbol>() == Symbol::PositiveInfinity;
}

bool MathematicalValue::is_negative_infinity() const
{
    if (is_mathematical_value())
        return false;
    return m_value.get<Symbol>() == Symbol::NegativeInfinity;
}

bool MathematicalValue::is_negative_zero() const
{
    if (is_mathematical_value())
        return false;
    return m_value.get<Symbol>() == Symbol::NegativeZero;
}

bool MathematicalValue::is_nan() const
{
    if (is_mathematical_value())
        return false;
    return m_value.get<Symbol>() == Symbol::NotANumber;
}

void MathematicalValue::negate()
{
    m_value.visit(
        [](double& value) {
            VERIFY(value != 0.0);
            value *= -1.0;
        },
        [](Crypto::SignedBigInteger& value) { value.negate(); },
        [](auto) { VERIFY_NOT_REACHED(); });
}

MathematicalValue MathematicalValue::plus(Checked<i32> addition) const
{
    return m_value.visit(
        [&](double value) {
            return MathematicalValue { value + addition.value() };
        },
        [&](Crypto::SignedBigInteger const& value) {
            return MathematicalValue { value.plus(Crypto::SignedBigInteger { addition.value() }) };
        },
        [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
}

MathematicalValue MathematicalValue::plus(MathematicalValue const& addition) const
{
    return m_value.visit(
        [&](double value) {
            return MathematicalValue { value + addition.as_number() };
        },
        [&](Crypto::SignedBigInteger const& value) {
            return MathematicalValue { value.plus(addition.as_bigint()) };
        },
        [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
}

MathematicalValue MathematicalValue::minus(Checked<i32> subtraction) const
{
    return m_value.visit(
        [&](double value) {
            return MathematicalValue { value - subtraction.value() };
        },
        [&](Crypto::SignedBigInteger const& value) {
            return MathematicalValue { value.minus(Crypto::SignedBigInteger { subtraction.value() }) };
        },
        [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
}

MathematicalValue MathematicalValue::minus(MathematicalValue const& subtraction) const
{
    return m_value.visit(
        [&](double value) {
            return MathematicalValue { value - subtraction.as_number() };
        },
        [&](Crypto::SignedBigInteger const& value) {
            return MathematicalValue { value.minus(subtraction.as_bigint()) };
        },
        [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
}

MathematicalValue MathematicalValue::multiplied_by(Checked<i32> multiplier) const
{
    return m_value.visit(
        [&](double value) {
            return MathematicalValue { value * multiplier.value() };
        },
        [&](Crypto::SignedBigInteger const& value) {
            return MathematicalValue { value.multiplied_by(Crypto::SignedBigInteger { multiplier.value() }) };
        },
        [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
}

MathematicalValue MathematicalValue::multiplied_by(MathematicalValue const& multiplier) const
{
    return m_value.visit(
        [&](double value) {
            return MathematicalValue { value * multiplier.as_number() };
        },
        [&](Crypto::SignedBigInteger const& value) {
            return MathematicalValue { value.multiplied_by(multiplier.as_bigint()) };
        },
        [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
}

MathematicalValue MathematicalValue::divided_by(Checked<i32> divisor) const
{
    return m_value.visit(
        [&](double value) {
            return MathematicalValue { value / divisor.value() };
        },
        [&](Crypto::SignedBigInteger const& value) {
            return MathematicalValue { value.divided_by(Crypto::SignedBigInteger { divisor.value() }).quotient };
        },
        [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
}

MathematicalValue MathematicalValue::divided_by(MathematicalValue const& divisor) const
{
    return m_value.visit(
        [&](double value) {
            return MathematicalValue { value / divisor.as_number() };
        },
        [&](Crypto::SignedBigInteger const& value) {
            return MathematicalValue { value.divided_by(divisor.as_bigint()).quotient };
        },
        [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
}

static Crypto::SignedBigInteger bigint_power(Checked<i32> exponent)
{
    VERIFY(exponent >= 0);

    static auto base = Crypto::SignedBigInteger { 10 };
    auto result = Crypto::SignedBigInteger { 1 };

    for (i32 i = 0; i < exponent; ++i)
        result = result.multiplied_by(base);

    return result;
}

MathematicalValue MathematicalValue::multiplied_by_power(Checked<i32> exponent) const
{
    return m_value.visit(
        [&](double value) {
            return MathematicalValue { value * pow(10, exponent.value()) };
        },
        [&](Crypto::SignedBigInteger const& value) {
            if (exponent < 0)
                return MathematicalValue { value.divided_by(bigint_power(-exponent.value())).quotient };
            return MathematicalValue { value.multiplied_by(bigint_power(exponent)) };
        },
        [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
}

MathematicalValue MathematicalValue::divided_by_power(Checked<i32> exponent) const
{
    return m_value.visit(
        [&](double value) {
            if (exponent < 0)
                return MathematicalValue { value * pow(10, -exponent.value()) };
            return MathematicalValue { value / pow(10, exponent.value()) };
        },
        [&](Crypto::SignedBigInteger const& value) {
            if (exponent < 0)
                return MathematicalValue { value.multiplied_by(bigint_power(-exponent.value())) };
            return MathematicalValue { value.divided_by(bigint_power(exponent)).quotient };
        },
        [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
}

bool MathematicalValue::modulo_is_zero(Checked<i32> mod) const
{
    return m_value.visit(
        [&](double value) {
            auto result = MathematicalValue { modulo(value, mod.value()) };
            return result.is_equal_to(MathematicalValue { 0.0 });
        },
        [&](Crypto::SignedBigInteger const& value) {
            return modulo(value, Crypto::SignedBigInteger { mod.value() }).is_zero();
        },
        [](auto) -> bool { VERIFY_NOT_REACHED(); });
}

ThrowCompletionOr<int> MathematicalValue::logarithmic_floor(VM& vm) const
{
    return m_value.visit(
        [](double value) -> ThrowCompletionOr<int> {
            return static_cast<int>(floor(log10(value)));
        },
        [&](Crypto::SignedBigInteger const& value) -> ThrowCompletionOr<int> {
            // FIXME: Can we do this without string conversion?
            auto value_as_string = TRY_OR_THROW_OOM(vm, value.to_base(10));
            return static_cast<int>(value_as_string.bytes_as_string_view().length() - 1);
        },
        [](auto) -> ThrowCompletionOr<int> { VERIFY_NOT_REACHED(); });
}

bool MathematicalValue::is_equal_to(MathematicalValue const& other) const
{
    return m_value.visit(
        [&](double value) {
            static constexpr double epsilon = 5e-14;
            return fabs(value - other.as_number()) < epsilon;
        },
        [&](Crypto::SignedBigInteger const& value) {
            return value == other.as_bigint();
        },
        [](auto) -> bool { VERIFY_NOT_REACHED(); });
}

bool MathematicalValue::is_less_than(MathematicalValue const& other) const
{
    return m_value.visit(
        [&](double value) {
            if (is_equal_to(other))
                return false;
            return value < other.as_number();
        },
        [&](Crypto::SignedBigInteger const& value) {
            return value < other.as_bigint();
        },
        [](auto) -> bool { VERIFY_NOT_REACHED(); });
}

bool MathematicalValue::is_negative() const
{
    return m_value.visit(
        [](double value) { return value < 0.0; },
        [](Crypto::SignedBigInteger const& value) { return value.is_negative(); },
        [](Symbol symbol) { return symbol == Symbol::NegativeInfinity; });
}

bool MathematicalValue::is_positive() const
{
    return m_value.visit(
        [](double value) { return value > 0.0; },
        [](Crypto::SignedBigInteger const& value) { return !value.is_zero() && !value.is_negative(); },
        [](Symbol symbol) { return symbol == Symbol::PositiveInfinity; });
}

bool MathematicalValue::is_zero() const
{
    return m_value.visit(
        [&](double value) { return value == 0.0; },
        [](Crypto::SignedBigInteger const& value) { return value.is_zero(); },
        [](auto) { return false; });
}

ThrowCompletionOr<String> MathematicalValue::to_string(VM& vm) const
{
    return m_value.visit(
        [&](double value) -> ThrowCompletionOr<String> {
            return TRY_OR_THROW_OOM(vm, number_to_string(value, NumberToStringMode::WithoutExponent));
        },
        [&](Crypto::SignedBigInteger const& value) -> ThrowCompletionOr<String> {
            return TRY_OR_THROW_OOM(vm, value.to_base(10));
        },
        [&](auto) -> ThrowCompletionOr<String> { VERIFY_NOT_REACHED(); });
}

Value MathematicalValue::to_value(VM& vm) const
{
    return m_value.visit(
        [](double value) {
            return Value(value);
        },
        [&](Crypto::SignedBigInteger const& value) {
            return Value(BigInt::create(vm, value));
        },
        [](auto symbol) {
            switch (symbol) {
            case Symbol::PositiveInfinity:
                return js_infinity();
            case Symbol::NegativeInfinity:
                return js_negative_infinity();
            case Symbol::NegativeZero:
                return Value(-0.0);
            case Symbol::NotANumber:
                return js_nan();
            }

            VERIFY_NOT_REACHED();
        });
}

MathematicalValue::ValueType MathematicalValue::value_from_number(double number)
{
    Value value(number);

    if (value.is_positive_infinity())
        return Symbol::PositiveInfinity;
    if (value.is_negative_infinity())
        return Symbol::NegativeInfinity;
    if (value.is_negative_zero())
        return Symbol::NegativeZero;
    if (value.is_nan())
        return Symbol::NotANumber;
    return number;
}

}