Repository navigation
Expand file tree
/
Copy pathsin.test.js
More file actions
126 lines (106 loc) · 5.95 KB
/
Copy pathsin.test.js
File metadata and controls
126 lines (106 loc) · 5.95 KB
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
import assert from 'assert'
import math from '../../../../src/defaultInstance.js'
import { approxEqual, approxDeepEqual } from '../../../../tools/approx.js'
const EPSILON = 1e-13
const pi = math.pi
const complex = math.complex
const matrix = math.matrix
const unit = math.unit
const sin = math.sin
const bigmath = math.create({ precision: 242 })
describe('sin', function () {
it('should return the sine of a boolean', function () {
approxEqual(sin(true), 0.841470984807897)
approxEqual(sin(false), 0)
})
it('should return the sine of a number', function () {
approxEqual(sin(0), 0, EPSILON)
approxEqual(sin(pi / 8), 0.382683432365090, EPSILON)
approxEqual(sin(pi / 4), Math.SQRT1_2, EPSILON)
approxEqual(sin(pi / 2), 1, EPSILON)
approxEqual(sin(pi * 3 / 4), 0.707106781186548, EPSILON)
approxEqual(sin(pi), 0, EPSILON)
approxEqual(sin(pi * 5 / 4), -0.707106781186548, EPSILON)
approxEqual(sin(pi * 3 / 2), -1, EPSILON)
approxEqual(sin(pi * 7 / 4), -0.707106781186548, EPSILON)
approxEqual(sin(pi * 2), 0, EPSILON)
})
it('should return the sine of a bignumber', function () {
const Big = bigmath.bignumber
assert.deepStrictEqual(bigmath.sin(Big(0)), Big(0))
// 103.64 % tau = 3.109... <- pretty close to the pi boundary
let resultVal = bigmath.sin(Big(103.64))
assert.deepStrictEqual(resultVal, Big('0.0325518169566161584427313159942672130512044591216893328934710' +
'307148043832988055013958395123418887322610809247793661058554' +
'935758353628919004205593985094895305777198408601067175226892' +
'4960612126026291341865833521451170868744460464214033460336158'))
const arg = Big(-103.64)
resultVal = bigmath.sin(arg)
assert.deepStrictEqual(arg, Big(-103.64)) // Make sure arg wasn't changed
assert.deepStrictEqual(resultVal, Big('-0.0325518169566161584427313159942672130512044591216893328934710' +
'3071480438329880550139583951234188873226108092477936610585549' +
'3575835362891900420559398509489530577719840860106717522689249' +
'60612126026291341865833521451170868744460464214033460336158'))
const bigmath2 = bigmath.create({ number: 'BigNumber', precision: 15 })
// we've had a bug in reducing the period, affecting integer values around multiples of tau (like 6, 7)
for (let x = -20; x < 20; x += 1) {
approxEqual(bigmath2.sin(bigmath2.bignumber(x)).toNumber(), Math.sin(x), EPSILON)
}
const bigPi = bigmath2.pi
assert.deepStrictEqual(bigmath2.sin(bigPi.div(8)).toString(), '0.38268343236509')
assert.deepStrictEqual(bigmath2.sin(bigPi.div(4)).toString(), '0.707106781186547')
assert.deepStrictEqual(bigmath2.sin(bigPi.div(2)).toString(), '1')
assert.deepStrictEqual(bigmath2.sin(bigPi.times(3).div(4)).toString(), '0.707106781186551')
assert.ok(bigmath2.sin(bigPi).lt(1e-14))
assert.deepStrictEqual(bigmath2.sin(bigPi.times(5).div(4)).toString(), '-0.707106781186554')
assert.deepStrictEqual(bigmath2.sin(bigPi.times(3).div(2)).toString(), '-1')
assert.deepStrictEqual(bigmath2.sin(bigPi.times(7).div(4)).toString(), '-0.707106781186553')
assert.ok(bigmath2.sin(bigPi.times(2)).lt(1e-13))
assert.ok(bigmath2.sin(bigmath2.tau).lt(1e-14))
assert.ok(bigmath2.sin(bigmath2.tau.times(2)).lt(1e-13))
const bigmath61 = bigmath.create({ number: 'BigNumber', precision: 61 })
assert.deepStrictEqual(bigmath61.sin(bigmath61.bignumber(-2)).toString(), '-0.909297426825681695396019865911744842702254971447890268378973')
})
it('should return the sine of a complex number', function () {
const re = 9.15449914691143
const im = 4.16890695996656
approxDeepEqual(sin(complex('2+3i')), complex(re, -im), EPSILON)
approxDeepEqual(sin(complex('2-3i')), complex(re, im), EPSILON)
approxDeepEqual(sin(complex('-2+3i')), complex(-re, -im), EPSILON)
approxDeepEqual(sin(complex('-2-3i')), complex(-re, im), EPSILON)
approxDeepEqual(sin(complex('i')), complex(0, 1.175201193643801), EPSILON)
approxDeepEqual(sin(complex('1')), complex(0.841470984807897, 0), EPSILON)
approxDeepEqual(sin(complex('1+i')), complex(1.298457581415977, 0.634963914784736), EPSILON)
approxDeepEqual(sin(complex('1e-10i')), complex('1e-10i'), EPSILON)
})
it('should return the sine of an angle', function () {
approxEqual(sin(unit('45deg')), 0.707106781186548, EPSILON)
approxEqual(sin(unit('-45deg')), -0.707106781186548, EPSILON)
assert(math.isBigNumber(sin(unit(math.bignumber(45), 'deg'))))
approxEqual(sin(unit(math.bignumber(45), 'deg')).toNumber(), 0.707106781186548, EPSILON)
approxDeepEqual(sin(unit(complex('1+i'), 'rad')), complex(1.298457581415977, 0.634963914784736), EPSILON)
})
it('should throw an error if called with an invalid unit', function () {
assert.throws(function () { sin(unit('5 celsius')) }, /Unit in function sin is no angle/)
})
it('should throw an error if called with a string', function () {
assert.throws(function () { sin('string') })
})
const sin123 = [0.84147098480789, 0.909297426825682, 0.141120008059867]
it('should not operate on an array', function () {
assert.throws(() => sin([1, 2, 3]), TypeError)
approxDeepEqual(math.map([1, 2, 3], sin), sin123, EPSILON)
})
it('should not operate on a matrix', function () {
assert.throws(() => sin(matrix([1, 2, 3])), TypeError)
approxDeepEqual(math.map(matrix([1, 2, 3]), sin), matrix(sin123), EPSILON)
})
it('should throw an error in case of invalid number of arguments', function () {
assert.throws(function () { sin() }, /TypeError: Too few arguments/)
assert.throws(function () { sin(1, 2) }, /TypeError: Too many arguments/)
})
it('should LaTeX sin', function () {
const expression = math.parse('sin(0.5)')
assert.strictEqual(expression.toTex(), '\\sin\\left(0.5\\right)')
})
})