summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testFractionToDouble.cpp
blob: c3e6842863dda990f0abe775f4b45603d936c8d9 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifdef JS_HAS_TEMPORAL_API

#  include <cmath>
#  include <stdint.h>

#  include "builtin/temporal/Int128.h"
#  include "builtin/temporal/Temporal.h"
#  include "jsapi-tests/tests.h"

using namespace js::temporal;

// Simple test using numerators and denominators where the result can be
// computed through standard double division.
BEGIN_TEST(testFraction_simple) {
  int64_t numerators[] = {
      0, 1, 2, 10, 100, INT32_MIN, INT32_MAX,
  };

  int64_t denominators[] = {
      1, 2, 3, 10, 100, 1000,
  };

  for (auto numerator : numerators) {
    for (auto denominator : denominators) {
      double result = double(numerator) / double(denominator);

      CHECK_EQUAL(FractionToDouble(numerator, denominator), result);
      CHECK_EQUAL(FractionToDouble(Int128{numerator}, Int128{denominator}),
                  result);

      CHECK_EQUAL(FractionToDouble(-numerator, denominator),
                  std::copysign(result, -numerator));
      CHECK_EQUAL(FractionToDouble(-Int128{numerator}, Int128{denominator}),
                  std::copysign(result, -numerator));
    }
  }

  return true;
}
END_TEST(testFraction_simple)

// Complex test with values exceeding Number.MAX_SAFE_INTEGER.
BEGIN_TEST(testFraction_complex) {
  struct {
    int64_t numerator;
    int64_t denominator;
    double result;
  } values[] = {
      // Number.MAX_SAFE_INTEGER
      {9007199254740991, 2, 4503599627370495.5},
      {9007199254740992, 2, 4503599627370496},
      {9007199254740993, 2, 4503599627370496.5},

      {INT64_MAX, 2, 4611686018427387903.5},
      {INT64_MIN, 2, -4611686018427387904.0},
  };

  for (auto [numerator, denominator, result] : values) {
    CHECK_EQUAL(FractionToDouble(numerator, denominator), result);
    CHECK_EQUAL(FractionToDouble(Int128{numerator}, Int128{denominator}),
                result);
  }

  return true;
}
END_TEST(testFraction_complex)

// Complex test with Int128 values exceeding Number.MAX_SAFE_INTEGER.
BEGIN_TEST(testFraction_complex_int128) {
  struct {
    Int128 numerator;
    Int128 denominator;
    double result;
  } values[] = {
      // Divide 1 by a growing divisor.
      {Int128{1}, Int128{1'000}, 0.001},
      {Int128{1}, Int128{1'000'000}, 0.000'001},
      {Int128{1}, Int128{1'000'000'000}, 0.000'000'001},
      {Int128{1}, Int128{1'000'000'000'000}, 0.000'000'000'001},
      {Int128{1}, Int128{1'000'000'000'000'000}, 0.000'000'000'000'001},
      {Int128{1}, Int128{1'000'000'000'000'000'000}, 0.000'000'000'000'000'001},
      {Int128{1}, Int128{1'000'000'000'000'000'000} * Int128{1'000},
       0.000'000'000'000'000'000'001},
      {Int128{1}, Int128{1'000'000'000'000'000'000} * Int128{1'000'000},
       0.000'000'000'000'000'000'000'001},
      {Int128{1}, Int128{1'000'000'000'000'000'000} * Int128{1'000'000'000},
       0.000'000'000'000'000'000'000'000'001},
      {Int128{1}, Int128{1'000'000'000'000'000'000} * Int128{1'000'000'000'000},
       0.000'000'000'000'000'000'000'000'000'001},
      {Int128{1},
       Int128{1'000'000'000'000'000'000} * Int128{1'000'000'000'000'000},
       0.000'000'000'000'000'000'000'000'000'000'001},
      {Int128{1},
       Int128{1'000'000'000'000'000'000} * Int128{1'000'000'000'000'000'000},
       0.000'000'000'000'000'000'000'000'000'000'000'001},

      // Divide a number not representable as an int64.
      {Int128{0x8000'0000} << 64, Int128{1'000},
       39614081257132168796771975.1680},
      {Int128{0x8000'0000} << 64, Int128{1'000'000},
       39614081257132168796771.9751680},
      {Int128{0x8000'0000} << 64, Int128{1'000'000'000},
       39614081257132168796.7719751680},
      {Int128{0x8000'0000} << 64, Int128{1'000'000'000'000},
       39614081257132168.7967719751680},
      {Int128{0x8000'0000} << 64, Int128{1'000'000'000'000'000},
       39614081257132.1687967719751680},
      {Int128{0x8000'0000} << 64, Int128{1'000'000'000'000'000'000},
       39614081257.1321687967719751680},
      {Int128{0x8000'0000} << 64,
       Int128{1'000'000'000'000'000'000} * Int128{1'000},
       39614081.2571321687967719751680},
      {Int128{0x8000'0000} << 64,
       Int128{1'000'000'000'000'000'000} * Int128{1'000'000},
       39614.0812571321687967719751680},
      {Int128{0x8000'0000} << 64,
       Int128{1'000'000'000'000'000'000} * Int128{1'000'000'000},
       39.6140812571321687967719751680},
      {Int128{0x8000'0000} << 64,
       Int128{1'000'000'000'000'000'000} * Int128{1'000'000'000'000},
       0.0396140812571321687967719751680},
      {Int128{0x8000'0000} << 64,
       Int128{1'000'000'000'000'000'000} * Int128{1'000'000'000'000'000},
       0.0000396140812571321687967719751680},
      {Int128{0x8000'0000} << 64,
       Int128{1'000'000'000'000'000'000} * Int128{1'000'000'000'000'000'000},
       0.0000000396140812571321687967719751680},

      // Test divisor which isn't a multiple of ten.
      {Int128{0x8000'0000} << 64, Int128{2}, 19807040628566084398385987584.0},
      {Int128{0x8000'0000} << 64, Int128{3}, 13204693752377389598923991722.666},
      {Int128{0x8000'0000} << 64, Int128{3'333},
       11885412918431493788410433.5937},
      {Int128{0x8000'0000} << 64, Int128{3'333'333},
       11884225565562207195252.3120756},
      {Int128{0x8000'0000} << 64, Int128{3'333'333'333},
       11884224378328073076.864399858},
      {Int128{0x8000'0000} << 64, Int128{3'333'333'333'333},
       11884224377140839.0614693066343},
      {Int128{0x8000'0000} << 64, Int128{3'333'333'333'333'333},
       11884224377139.6518274540302643},
      {Int128{0x8000'0000} << 64, Int128{3'333'333'333'333'333'333},
       11884224377.1396506402200149881},
      {Int128{0x8000'0000} << 64,
       (Int128{3'333'333'333'333'333'333} * Int128{1'000}) + Int128{333},
       11884224.3771396506390327809728},
      {Int128{0x8000'0000} << 64,
       (Int128{3'333'333'333'333'333'333} * Int128{1'000'000}) +
           Int128{333'333},
       11884.2243771396506390315937388},
      {Int128{0x8000'0000} << 64,
       (Int128{3'333'333'333'333'333'333} * Int128{1'000'000'000}) +
           Int128{333'333'333},
       11.884224377139650639031592551588422437713965063903159255158842243},
      {Int128{0x8000'0000} << 64,
       (Int128{3'333'333'333'333'333'333} * Int128{1'000'000'000'000}) +
           Int128{333'333'333'333},
       0.0118842243771396506390315925504},
      {Int128{0x8000'0000} << 64,
       (Int128{3'333'333'333'333'333'333} * Int128{1'000'000'000'000'000}) +
           Int128{333'333'333'333'333},
       0.0000118842243771396506390315925504},
      {Int128{0x8000'0000} << 64,
       (Int128{3'333'333'333'333'333'333} * Int128{1'000'000'000'000'000'000}) +
           Int128{333'333'333'333'333'333},
       0.0000000118842243771396506390315925504},
  };

  for (auto [numerator, denominator, result] : values) {
    CHECK_EQUAL(FractionToDouble(numerator, denominator), result);
  }

  return true;
}
END_TEST(testFraction_complex_int128)

#endif