|
| 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(Math.random() * m, -0)).toBe(0) |
| 36 | + expect(agm(-0, Math.random() * m)).toBe(0) |
| 37 | + expect(agm(0, -0)).toBe(0) |
| 38 | + expect(agm(-0, 0)).toBe(0) |
| 39 | + expect(agm(-0, -0)).toBe(-0) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should return NaN if any or all args are NaN', () => { |
| 43 | + expect(agm(Math.random() * m, NaN)).toBe(NaN) |
| 44 | + expect(agm(NaN, Math.random() * m)).toBe(NaN) |
| 45 | + expect(agm(NaN, NaN)).toBe(NaN) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should return an accurate approximation of the AGM between 2 valid input args', () => { |
| 49 | + // all the constants are provided by WolframAlpha |
| 50 | + expect(agm(1, 2)).toBeCloseTo(1.4567910310469068) |
| 51 | + expect(agm(2, 256)).toBeCloseTo(64.45940719438667) |
| 52 | + expect(agm(55555, 34)).toBeCloseTo(9933.4047239552) |
| 53 | + // test "unsafe" numbers |
| 54 | + expect(agm(2 ** 48, 3 ** 27)).toBeCloseTo(88506556379265.7) |
| 55 | + }) |
| 56 | +}) |
0 commit comments