summaryrefslogtreecommitdiff
path: root/lib/math/test/poly-impl.myr
blob: 565a658bee5046a65839d0936e887e731b98b461 (plain)
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
use std
use math
use testr

const main = {
	testr.run([
		[.name="horner-01", .fn = horner01],
		[.name="horner-02", .fn = horner02],
	][:])
}

const horner01 = {c
	var inputs : (uint32, uint32[:], uint32)[:] = [
		(0x00000000, [][:], 0x00000000),
		(0xbf800000, [0x3f715545, 0x3f715546, 0x3f715544, 0x3f715545][:], 0xb4000000),
		(0xbf800001, [0x3f715545, 0x3f715546, 0x3f715544, 0x3f715545][:], 0xb4bc5552),
	][:]

	for (f, a, r) : inputs
		var f2 = std.flt32frombits(f)
		var a2 = [][:]
		for aa : a
			std.slpush(&a2, std.flt32frombits(aa))
		;;
		var sf = math.horner_poly(f2, a2)
		var s : uint32 = std.flt32bits(sf)
		testr.eq(c, s, r)
	;;
}

const horner02 = {c
	var inputs : (uint64, uint64[:], uint64)[:] = [
		(0x0000000000000000, [][:], 0x0000000000000000),
		(0xc0003d3368ee1111, [
			0x3ff0000000000000,
			0x3fe0000000000000,
			0x3fc5555555555555,
			0x3fa5555555555555,
			0x3f81a7b9611a7b96,
			0x3f59c2d14ee4a102,
			0x3f3136f054ff42a4,
			0x3f05902ed525b6ee
		][:], 0x3fdb64730ab8fa29),
		(0x40003d3368ee1111, [
			0x3ff0000000000000,
			0x3fe0000000000000,
			0x3fc5555555555555,
			0x3fa5555555555555,
			0x3f81a7b9611a7b96,
			0x3f59c2d14ee4a102,
			0x3f3136f054ff42a4,
			0x3f05902ed525b6ee
		][:], 0x400a331575ee40db),
	][:]

	for (f, a, r) : inputs
		var f2 = std.flt64frombits(f)
		var a2 = [][:]
		for aa : a
			std.slpush(&a2, std.flt64frombits(aa))
		;;
		var sf = math.horner_poly(f2, a2)
		var s : uint64 = std.flt64bits(sf)
		testr.eq(c, s, r)
	;;
}