1
+ import { agm } from '../ArithmeticGeometricMean.js'
2
+
3
+ describe ( 'Tests for AGM' , ( ) => {
4
+ it ( 'should be a function' , ( ) => {
5
+ expect ( typeof agm ) . toEqual ( 'function' )
6
+ } )
7
+
8
+ it ( 'number of parameters should be 2' , ( ) => {
9
+ expect ( agm . length ) . toEqual ( 2 )
10
+ } )
11
+
12
+ const m = 0x100 ; //scale for rand
13
+
14
+ it ( 'should return NaN if any or all params has a negative argument' , ( ) => {
15
+ //I multiplied by minus one, because the sign inversion is more clearly visible
16
+ expect ( agm ( - 1 * Math . random ( ) * m , Math . random ( ) * m ) ) . toBe ( NaN )
17
+ expect ( agm ( Math . random ( ) * m , - 1 * Math . random ( ) * m ) ) . toBe ( NaN )
18
+ expect ( agm ( - 1 * Math . random ( ) * m , - 1 * Math . random ( ) * m ) ) . toBe ( NaN )
19
+ } )
20
+
21
+ it ( 'should return Infinity if any arg is Infinity and the other is not 0' , ( ) => {
22
+ expect ( agm ( Math . random ( ) * m + 1 , Infinity ) ) . toEqual ( Infinity )
23
+ expect ( agm ( Infinity , Math . random ( ) * m + 1 ) ) . toEqual ( Infinity )
24
+ expect ( agm ( Infinity , Infinity ) . toEqual ( Infinity ) )
25
+ } )
26
+
27
+ it ( 'should return NaN if some arg is Infinity and the other is 0' , ( ) => {
28
+ expect ( agm ( 0 , Infinity ) . toBe ( NaN ) )
29
+ expect ( agm ( Infinity , 0 ) . toBe ( NaN ) )
30
+ } )
31
+
32
+ it ( 'should return +0 if any or all args are +0 or -0, and return -0 if all are -0' , ( ) => {
33
+ expect ( agm ( Math . random ( ) * m , - 0 ) ) . toBe ( 0 )
34
+ expect ( agm ( 0 , Math . random ( ) * m ) ) . toBe ( 0 )
35
+ expect ( agm ( 0 , - 0 ) . toBe ( 0 ) )
36
+ expect ( agm ( - 0 , 0 ) . toBe ( 0 ) )
37
+ expect ( agm ( - 0 , - 0 ) . toBe ( - 0 ) )
38
+ } )
39
+
40
+ it ( 'should return NaN if any or all args are NaN' , ( ) => {
41
+ expect ( agm ( Math . random ( ) * m , NaN ) ) . toBe ( NaN )
42
+ expect ( agm ( NaN , Math . random ( ) * m ) ) . toBe ( NaN )
43
+ expect ( agm ( NaN , NaN ) . toBe ( NaN ) )
44
+ } )
45
+
46
+ it ( 'should return an accurate approximation of the AGM between 2 valid input args' , ( ) => {
47
+ //all the constants are provided by WolframAlpha
48
+ expect ( agm ( 1 , 2 ) ) . toBeCloseTo ( 1.4567910310469068691864323832650819749738639432213055907941723832 )
49
+ expect ( agm ( 2 , 256 ) ) . toBeCloseTo ( 64.4594071943866695986665434983250898480227029006463800294306182 )
50
+ expect ( agm ( 55555 , 34 ) ) . toBeCloseTo ( 9933.40472395519953051873484897963370925448369441581220656590 )
51
+ //test "unsafe" numbers
52
+ expect ( agm ( 2 ** 48 , 3 ** 27 ) . toBeCloseTo ( 88506556379265.712723873253375677194780 ) )
53
+ } )
54
+ } )
0 commit comments