OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <limits> | 5 #include <limits> |
6 | 6 |
7 #include "src/base/ieee754.h" | 7 #include "src/base/ieee754.h" |
8 #include "src/base/macros.h" | 8 #include "src/base/macros.h" |
9 #include "testing/gmock-support.h" | 9 #include "testing/gmock-support.h" |
10 #include "testing/gtest-support.h" | 10 #include "testing/gtest-support.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 | 56 |
57 TEST(Ieee754, Atanh) { | 57 TEST(Ieee754, Atanh) { |
58 EXPECT_THAT(atanh(std::numeric_limits<double>::quiet_NaN()), IsNaN()); | 58 EXPECT_THAT(atanh(std::numeric_limits<double>::quiet_NaN()), IsNaN()); |
59 EXPECT_THAT(atanh(std::numeric_limits<double>::signaling_NaN()), IsNaN()); | 59 EXPECT_THAT(atanh(std::numeric_limits<double>::signaling_NaN()), IsNaN()); |
60 EXPECT_THAT(atanh(std::numeric_limits<double>::infinity()), IsNaN()); | 60 EXPECT_THAT(atanh(std::numeric_limits<double>::infinity()), IsNaN()); |
61 EXPECT_EQ(std::numeric_limits<double>::infinity(), atanh(1)); | 61 EXPECT_EQ(std::numeric_limits<double>::infinity(), atanh(1)); |
62 EXPECT_EQ(-std::numeric_limits<double>::infinity(), atanh(-1)); | 62 EXPECT_EQ(-std::numeric_limits<double>::infinity(), atanh(-1)); |
63 EXPECT_DOUBLE_EQ(0.54930614433405478, atanh(0.5)); | 63 EXPECT_DOUBLE_EQ(0.54930614433405478, atanh(0.5)); |
64 } | 64 } |
65 | 65 |
| 66 TEST(Ieee754, Cos) { |
| 67 // Test values mentioned in the EcmaScript spec. |
| 68 EXPECT_THAT(cos(std::numeric_limits<double>::quiet_NaN()), IsNaN()); |
| 69 EXPECT_THAT(cos(std::numeric_limits<double>::signaling_NaN()), IsNaN()); |
| 70 EXPECT_THAT(cos(std::numeric_limits<double>::infinity()), IsNaN()); |
| 71 EXPECT_THAT(cos(-std::numeric_limits<double>::infinity()), IsNaN()); |
| 72 |
| 73 // Tests for cos for |x| < pi/4 |
| 74 EXPECT_EQ(1.0, 1 / cos(-0.0)); |
| 75 EXPECT_EQ(1.0, 1 / cos(0.0)); |
| 76 // cos(x) = 1 for |x| < 2^-27 |
| 77 EXPECT_EQ(1, cos(2.3283064365386963e-10)); |
| 78 EXPECT_EQ(1, cos(-2.3283064365386963e-10)); |
| 79 // Test KERNELCOS for |x| < 0.3. |
| 80 // cos(pi/20) = sqrt(sqrt(2)*sqrt(sqrt(5)+5)+4)/2^(3/2) |
| 81 EXPECT_EQ(0.9876883405951378, cos(0.15707963267948966)); |
| 82 // Test KERNELCOS for x ~= 0.78125 |
| 83 EXPECT_EQ(0.7100335477927638, cos(0.7812504768371582)); |
| 84 EXPECT_EQ(0.7100338835660797, cos(0.78125)); |
| 85 // Test KERNELCOS for |x| > 0.3. |
| 86 // cos(pi/8) = sqrt(sqrt(2)+1)/2^(3/4) |
| 87 EXPECT_EQ(0.9238795325112867, cos(0.39269908169872414)); |
| 88 // Test KERNELTAN for |x| < 0.67434. |
| 89 EXPECT_EQ(0.9238795325112867, cos(-0.39269908169872414)); |
| 90 |
| 91 // Tests for cos. |
| 92 EXPECT_EQ(1, cos(3.725290298461914e-9)); |
| 93 // Cover different code paths in KERNELCOS. |
| 94 EXPECT_EQ(0.9689124217106447, cos(0.25)); |
| 95 EXPECT_EQ(0.8775825618903728, cos(0.5)); |
| 96 EXPECT_EQ(0.7073882691671998, cos(0.785)); |
| 97 // Test that cos(Math.PI/2) != 0 since Math.PI is not exact. |
| 98 EXPECT_EQ(6.123233995736766e-17, cos(1.5707963267948966)); |
| 99 // Test cos for various phases. |
| 100 EXPECT_EQ(0.7071067811865474, cos(7.0 / 4 * 3.141592653589793)); |
| 101 EXPECT_EQ(0.7071067811865477, cos(9.0 / 4 * 3.141592653589793)); |
| 102 EXPECT_EQ(-0.7071067811865467, cos(11.0 / 4 * 3.141592653589793)); |
| 103 EXPECT_EQ(-0.7071067811865471, cos(13.0 / 4 * 3.141592653589793)); |
| 104 EXPECT_EQ(0.9367521275331447, cos(1000000.0)); |
| 105 EXPECT_EQ(-3.435757038074824e-12, cos(1048575.0 / 2 * 3.141592653589793)); |
| 106 } |
| 107 |
66 TEST(Ieee754, Exp) { | 108 TEST(Ieee754, Exp) { |
67 EXPECT_THAT(exp(std::numeric_limits<double>::quiet_NaN()), IsNaN()); | 109 EXPECT_THAT(exp(std::numeric_limits<double>::quiet_NaN()), IsNaN()); |
68 EXPECT_THAT(exp(std::numeric_limits<double>::signaling_NaN()), IsNaN()); | 110 EXPECT_THAT(exp(std::numeric_limits<double>::signaling_NaN()), IsNaN()); |
69 EXPECT_EQ(0.0, exp(-std::numeric_limits<double>::infinity())); | 111 EXPECT_EQ(0.0, exp(-std::numeric_limits<double>::infinity())); |
70 EXPECT_EQ(0.0, exp(-1000)); | 112 EXPECT_EQ(0.0, exp(-1000)); |
71 EXPECT_EQ(0.0, exp(-745.1332191019412)); | 113 EXPECT_EQ(0.0, exp(-745.1332191019412)); |
72 EXPECT_EQ(2.2250738585072626e-308, exp(-708.39641853226408)); | 114 EXPECT_EQ(2.2250738585072626e-308, exp(-708.39641853226408)); |
73 EXPECT_EQ(3.307553003638408e-308, exp(-708.0)); | 115 EXPECT_EQ(3.307553003638408e-308, exp(-708.0)); |
74 EXPECT_EQ(4.9406564584124654e-324, exp(-7.45133219101941108420e+02)); | 116 EXPECT_EQ(4.9406564584124654e-324, exp(-7.45133219101941108420e+02)); |
75 EXPECT_EQ(0.36787944117144233, exp(-1.0)); | 117 EXPECT_EQ(0.36787944117144233, exp(-1.0)); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 EXPECT_EQ(std::numeric_limits<double>::infinity(), | 205 EXPECT_EQ(std::numeric_limits<double>::infinity(), |
164 log10(std::numeric_limits<double>::infinity())); | 206 log10(std::numeric_limits<double>::infinity())); |
165 EXPECT_EQ(3.0, log10(1000.0)); | 207 EXPECT_EQ(3.0, log10(1000.0)); |
166 EXPECT_EQ(14.0, log10(100000000000000)); // log10(10 ^ 14) | 208 EXPECT_EQ(14.0, log10(100000000000000)); // log10(10 ^ 14) |
167 EXPECT_EQ(3.7389561269540406, log10(5482.2158)); | 209 EXPECT_EQ(3.7389561269540406, log10(5482.2158)); |
168 EXPECT_EQ(14.661551142893833, log10(458723662312872.125782332587)); | 210 EXPECT_EQ(14.661551142893833, log10(458723662312872.125782332587)); |
169 EXPECT_EQ(-0.9083828622192334, log10(0.12348583358871)); | 211 EXPECT_EQ(-0.9083828622192334, log10(0.12348583358871)); |
170 EXPECT_EQ(5.0, log10(100000.0)); | 212 EXPECT_EQ(5.0, log10(100000.0)); |
171 } | 213 } |
172 | 214 |
173 TEST(Ieee754, cbrt) { | 215 TEST(Ieee754, Cbrt) { |
174 EXPECT_THAT(cbrt(std::numeric_limits<double>::quiet_NaN()), IsNaN()); | 216 EXPECT_THAT(cbrt(std::numeric_limits<double>::quiet_NaN()), IsNaN()); |
175 EXPECT_THAT(cbrt(std::numeric_limits<double>::signaling_NaN()), IsNaN()); | 217 EXPECT_THAT(cbrt(std::numeric_limits<double>::signaling_NaN()), IsNaN()); |
176 EXPECT_EQ(std::numeric_limits<double>::infinity(), | 218 EXPECT_EQ(std::numeric_limits<double>::infinity(), |
177 cbrt(std::numeric_limits<double>::infinity())); | 219 cbrt(std::numeric_limits<double>::infinity())); |
178 EXPECT_EQ(-std::numeric_limits<double>::infinity(), | 220 EXPECT_EQ(-std::numeric_limits<double>::infinity(), |
179 cbrt(-std::numeric_limits<double>::infinity())); | 221 cbrt(-std::numeric_limits<double>::infinity())); |
180 EXPECT_EQ(1.4422495703074083, cbrt(3)); | 222 EXPECT_EQ(1.4422495703074083, cbrt(3)); |
181 EXPECT_EQ(100, cbrt(100 * 100 * 100)); | 223 EXPECT_EQ(100, cbrt(100 * 100 * 100)); |
182 EXPECT_EQ(46.415888336127786, cbrt(100000)); | 224 EXPECT_EQ(46.415888336127786, cbrt(100000)); |
183 } | 225 } |
184 | 226 |
| 227 TEST(Ieee754, Sin) { |
| 228 // Test values mentioned in the EcmaScript spec. |
| 229 EXPECT_THAT(sin(std::numeric_limits<double>::quiet_NaN()), IsNaN()); |
| 230 EXPECT_THAT(sin(std::numeric_limits<double>::signaling_NaN()), IsNaN()); |
| 231 EXPECT_THAT(sin(std::numeric_limits<double>::infinity()), IsNaN()); |
| 232 EXPECT_THAT(sin(-std::numeric_limits<double>::infinity()), IsNaN()); |
| 233 |
| 234 // Tests for sin for |x| < pi/4 |
| 235 EXPECT_EQ(-std::numeric_limits<double>::infinity(), 1 / sin(-0.0)); |
| 236 EXPECT_EQ(std::numeric_limits<double>::infinity(), 1 / sin(0.0)); |
| 237 // sin(x) = x for x < 2^-27 |
| 238 EXPECT_EQ(2.3283064365386963e-10, sin(2.3283064365386963e-10)); |
| 239 EXPECT_EQ(-2.3283064365386963e-10, sin(-2.3283064365386963e-10)); |
| 240 // sin(pi/8) = sqrt(sqrt(2)-1)/2^(3/4) |
| 241 EXPECT_EQ(0.3826834323650898, sin(0.39269908169872414)); |
| 242 EXPECT_EQ(-0.3826834323650898, sin(-0.39269908169872414)); |
| 243 |
| 244 // Tests for sin. |
| 245 EXPECT_EQ(0.479425538604203, sin(0.5)); |
| 246 EXPECT_EQ(-0.479425538604203, sin(-0.5)); |
| 247 EXPECT_EQ(1, sin(1.5707963267948966)); |
| 248 EXPECT_EQ(-1, sin(-1.5707963267948966)); |
| 249 // Test that sin(Math.PI) != 0 since Math.PI is not exact. |
| 250 EXPECT_EQ(1.2246467991473532e-16, sin(3.141592653589793)); |
| 251 EXPECT_EQ(-7.047032979958965e-14, sin(2200 * 3.141592653589793)); |
| 252 // Test sin for various phases. |
| 253 EXPECT_EQ(-0.7071067811865477, sin(7.0 / 4 * 3.141592653589793)); |
| 254 EXPECT_EQ(0.7071067811865474, sin(9.0 / 4 * 3.141592653589793)); |
| 255 EXPECT_EQ(0.7071067811865483, sin(11.0 / 4 * 3.141592653589793)); |
| 256 EXPECT_EQ(-0.7071067811865479, sin(13.0 / 4 * 3.141592653589793)); |
| 257 EXPECT_EQ(-3.2103381051568376e-11, sin(1048576.0 / 4 * 3.141592653589793)); |
| 258 } |
| 259 |
185 } // namespace ieee754 | 260 } // namespace ieee754 |
186 } // namespace base | 261 } // namespace base |
187 } // namespace v8 | 262 } // namespace v8 |
OLD | NEW |