MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1import { test, testApprox, summary } from './helpers.js';
2
3console.log('Math Tests\n');
4
5testApprox('Math.E', Math.E, 2.718281828459045);
6testApprox('Math.PI', Math.PI, 3.141592653589793);
7testApprox('Math.LN10', Math.LN10, 2.302585092994046);
8testApprox('Math.LN2', Math.LN2, 0.6931471805599453);
9testApprox('Math.LOG10E', Math.LOG10E, 0.4342944819032518);
10testApprox('Math.LOG2E', Math.LOG2E, 1.4426950408889634);
11testApprox('Math.SQRT2', Math.SQRT2, 1.4142135623730951);
12testApprox('Math.SQRT1_2', Math.SQRT1_2, 0.7071067811865476);
13
14test('Math.abs(5)', Math.abs(5), 5);
15test('Math.abs(-5)', Math.abs(-5), 5);
16test('Math.abs(0)', Math.abs(0), 0);
17
18testApprox('Math.acos(1)', Math.acos(1), 0);
19testApprox('Math.acos(0)', Math.acos(0), Math.PI / 2);
20testApprox('Math.acos(-1)', Math.acos(-1), Math.PI);
21test('Math.acos(2) is NaN', Number.isNaN(Math.acos(2)), true);
22
23testApprox('Math.asin(0)', Math.asin(0), 0);
24testApprox('Math.asin(1)', Math.asin(1), Math.PI / 2);
25
26testApprox('Math.atan(0)', Math.atan(0), 0);
27testApprox('Math.atan(1)', Math.atan(1), Math.PI / 4);
28
29testApprox('Math.atan2(1, 1)', Math.atan2(1, 1), Math.PI / 4);
30testApprox('Math.atan2(1, 0)', Math.atan2(1, 0), Math.PI / 2);
31
32testApprox('Math.cbrt(8)', Math.cbrt(8), 2);
33testApprox('Math.cbrt(-8)', Math.cbrt(-8), -2);
34testApprox('Math.cbrt(27)', Math.cbrt(27), 3);
35
36test('Math.ceil(0.5)', Math.ceil(0.5), 1);
37test('Math.ceil(-0.5)', Math.ceil(-0.5), 0);
38test('Math.ceil(4.7)', Math.ceil(4.7), 5);
39
40test('Math.floor(0.5)', Math.floor(0.5), 0);
41test('Math.floor(-0.5)', Math.floor(-0.5), -1);
42test('Math.floor(4.7)', Math.floor(4.7), 4);
43
44test('Math.round(0.5)', Math.round(0.5), 1);
45test('Math.round(0.49)', Math.round(0.49), 0);
46test('Math.round(4.7)', Math.round(4.7), 5);
47
48test('Math.trunc(4.7)', Math.trunc(4.7), 4);
49test('Math.trunc(-4.7)', Math.trunc(-4.7), -4);
50
51testApprox('Math.cos(0)', Math.cos(0), 1);
52testApprox('Math.cos(Math.PI)', Math.cos(Math.PI), -1);
53
54testApprox('Math.sin(0)', Math.sin(0), 0);
55testApprox('Math.sin(Math.PI/2)', Math.sin(Math.PI / 2), 1);
56
57testApprox('Math.tan(0)', Math.tan(0), 0);
58testApprox('Math.tan(Math.PI/4)', Math.tan(Math.PI / 4), 1);
59
60testApprox('Math.exp(0)', Math.exp(0), 1);
61testApprox('Math.exp(1)', Math.exp(1), Math.E);
62
63testApprox('Math.log(1)', Math.log(1), 0);
64testApprox('Math.log(Math.E)', Math.log(Math.E), 1);
65
66testApprox('Math.log10(1)', Math.log10(1), 0);
67testApprox('Math.log10(10)', Math.log10(10), 1);
68testApprox('Math.log10(100)', Math.log10(100), 2);
69
70testApprox('Math.log2(1)', Math.log2(1), 0);
71testApprox('Math.log2(2)', Math.log2(2), 1);
72testApprox('Math.log2(8)', Math.log2(8), 3);
73
74test('Math.max(1, 2, 3)', Math.max(1, 2, 3), 3);
75test('Math.max(-1, -2, -3)', Math.max(-1, -2, -3), -1);
76test('Math.max()', Math.max(), -Infinity);
77
78test('Math.min(1, 2, 3)', Math.min(1, 2, 3), 1);
79test('Math.min(-1, -2, -3)', Math.min(-1, -2, -3), -3);
80test('Math.min()', Math.min(), Infinity);
81
82test('Math.pow(2, 3)', Math.pow(2, 3), 8);
83test('Math.pow(2, 10)', Math.pow(2, 10), 1024);
84test('Math.pow(10, 0)', Math.pow(10, 0), 1);
85testApprox('Math.pow(4, 0.5)', Math.pow(4, 0.5), 2);
86
87let r = Math.random();
88test('Math.random() >= 0', r >= 0, true);
89test('Math.random() < 1', r < 1, true);
90
91test('Math.sign(5)', Math.sign(5), 1);
92test('Math.sign(-5)', Math.sign(-5), -1);
93test('Math.sign(0)', Math.sign(0), 0);
94
95testApprox('Math.sqrt(4)', Math.sqrt(4), 2);
96testApprox('Math.sqrt(9)', Math.sqrt(9), 3);
97test('Math.sqrt(-1) is NaN', Number.isNaN(Math.sqrt(-1)), true);
98
99testApprox('Math.hypot(3, 4)', Math.hypot(3, 4), 5);
100testApprox('Math.hypot(5, 12)', Math.hypot(5, 12), 13);
101
102test('Math.clz32(1)', Math.clz32(1), 31);
103test('Math.clz32(0)', Math.clz32(0), 32);
104
105test('Math.imul(2, 4)', Math.imul(2, 4), 8);
106test('Math.imul(-1, 8)', Math.imul(-1, 8), -8);
107
108testApprox('Math.cosh(0)', Math.cosh(0), 1);
109testApprox('Math.sinh(0)', Math.sinh(0), 0);
110testApprox('Math.tanh(0)', Math.tanh(0), 0);
111
112testApprox('Math.acosh(1)', Math.acosh(1), 0);
113testApprox('Math.asinh(0)', Math.asinh(0), 0);
114testApprox('Math.atanh(0)', Math.atanh(0), 0);
115
116testApprox('Math.expm1(0)', Math.expm1(0), 0);
117testApprox('Math.log1p(0)', Math.log1p(0), 0);
118
119test('Math.abs(NaN) is NaN', Number.isNaN(Math.abs(NaN)), true);
120test('Math.abs(-Infinity)', Math.abs(-Infinity), Infinity);
121test('Math.log(0)', Math.log(0), -Infinity);
122
123summary();