Spaces:
Running
Running
File size: 7,686 Bytes
b200bda |
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
"""
Test bit-level integer and mpf operations
"""
from mpmath import *
from mpmath.libmp import *
def test_bitcount():
assert bitcount(0) == 0
assert bitcount(1) == 1
assert bitcount(7) == 3
assert bitcount(8) == 4
assert bitcount(2**100) == 101
assert bitcount(2**100-1) == 100
def test_trailing():
assert trailing(0) == 0
assert trailing(1) == 0
assert trailing(2) == 1
assert trailing(7) == 0
assert trailing(8) == 3
assert trailing(2**100) == 100
assert trailing(2**100-1) == 0
def test_round_down():
assert from_man_exp(0, -4, 4, round_down)[:3] == (0, 0, 0)
assert from_man_exp(0xf0, -4, 4, round_down)[:3] == (0, 15, 0)
assert from_man_exp(0xf1, -4, 4, round_down)[:3] == (0, 15, 0)
assert from_man_exp(0xff, -4, 4, round_down)[:3] == (0, 15, 0)
assert from_man_exp(-0xf0, -4, 4, round_down)[:3] == (1, 15, 0)
assert from_man_exp(-0xf1, -4, 4, round_down)[:3] == (1, 15, 0)
assert from_man_exp(-0xff, -4, 4, round_down)[:3] == (1, 15, 0)
def test_round_up():
assert from_man_exp(0, -4, 4, round_up)[:3] == (0, 0, 0)
assert from_man_exp(0xf0, -4, 4, round_up)[:3] == (0, 15, 0)
assert from_man_exp(0xf1, -4, 4, round_up)[:3] == (0, 1, 4)
assert from_man_exp(0xff, -4, 4, round_up)[:3] == (0, 1, 4)
assert from_man_exp(-0xf0, -4, 4, round_up)[:3] == (1, 15, 0)
assert from_man_exp(-0xf1, -4, 4, round_up)[:3] == (1, 1, 4)
assert from_man_exp(-0xff, -4, 4, round_up)[:3] == (1, 1, 4)
def test_round_floor():
assert from_man_exp(0, -4, 4, round_floor)[:3] == (0, 0, 0)
assert from_man_exp(0xf0, -4, 4, round_floor)[:3] == (0, 15, 0)
assert from_man_exp(0xf1, -4, 4, round_floor)[:3] == (0, 15, 0)
assert from_man_exp(0xff, -4, 4, round_floor)[:3] == (0, 15, 0)
assert from_man_exp(-0xf0, -4, 4, round_floor)[:3] == (1, 15, 0)
assert from_man_exp(-0xf1, -4, 4, round_floor)[:3] == (1, 1, 4)
assert from_man_exp(-0xff, -4, 4, round_floor)[:3] == (1, 1, 4)
def test_round_ceiling():
assert from_man_exp(0, -4, 4, round_ceiling)[:3] == (0, 0, 0)
assert from_man_exp(0xf0, -4, 4, round_ceiling)[:3] == (0, 15, 0)
assert from_man_exp(0xf1, -4, 4, round_ceiling)[:3] == (0, 1, 4)
assert from_man_exp(0xff, -4, 4, round_ceiling)[:3] == (0, 1, 4)
assert from_man_exp(-0xf0, -4, 4, round_ceiling)[:3] == (1, 15, 0)
assert from_man_exp(-0xf1, -4, 4, round_ceiling)[:3] == (1, 15, 0)
assert from_man_exp(-0xff, -4, 4, round_ceiling)[:3] == (1, 15, 0)
def test_round_nearest():
assert from_man_exp(0, -4, 4, round_nearest)[:3] == (0, 0, 0)
assert from_man_exp(0xf0, -4, 4, round_nearest)[:3] == (0, 15, 0)
assert from_man_exp(0xf7, -4, 4, round_nearest)[:3] == (0, 15, 0)
assert from_man_exp(0xf8, -4, 4, round_nearest)[:3] == (0, 1, 4) # 1111.1000 -> 10000.0
assert from_man_exp(0xf9, -4, 4, round_nearest)[:3] == (0, 1, 4) # 1111.1001 -> 10000.0
assert from_man_exp(0xe8, -4, 4, round_nearest)[:3] == (0, 7, 1) # 1110.1000 -> 1110.0
assert from_man_exp(0xe9, -4, 4, round_nearest)[:3] == (0, 15, 0) # 1110.1001 -> 1111.0
assert from_man_exp(-0xf0, -4, 4, round_nearest)[:3] == (1, 15, 0)
assert from_man_exp(-0xf7, -4, 4, round_nearest)[:3] == (1, 15, 0)
assert from_man_exp(-0xf8, -4, 4, round_nearest)[:3] == (1, 1, 4)
assert from_man_exp(-0xf9, -4, 4, round_nearest)[:3] == (1, 1, 4)
assert from_man_exp(-0xe8, -4, 4, round_nearest)[:3] == (1, 7, 1)
assert from_man_exp(-0xe9, -4, 4, round_nearest)[:3] == (1, 15, 0)
def test_rounding_bugs():
# 1 less than power-of-two cases
assert from_man_exp(72057594037927935, -56, 53, round_up) == (0, 1, 0, 1)
assert from_man_exp(73786976294838205979, -65, 53, round_nearest) == (0, 1, 1, 1)
assert from_man_exp(31, 0, 4, round_up) == (0, 1, 5, 1)
assert from_man_exp(-31, 0, 4, round_floor) == (1, 1, 5, 1)
assert from_man_exp(255, 0, 7, round_up) == (0, 1, 8, 1)
assert from_man_exp(-255, 0, 7, round_floor) == (1, 1, 8, 1)
def test_rounding_issue_200():
a = from_man_exp(9867,-100)
b = from_man_exp(9867,-200)
c = from_man_exp(-1,0)
z = (1, 1023, -10, 10)
assert mpf_add(a, c, 10, 'd') == z
assert mpf_add(b, c, 10, 'd') == z
assert mpf_add(c, a, 10, 'd') == z
assert mpf_add(c, b, 10, 'd') == z
def test_perturb():
a = fone
b = from_float(0.99999999999999989)
c = from_float(1.0000000000000002)
assert mpf_perturb(a, 0, 53, round_nearest) == a
assert mpf_perturb(a, 1, 53, round_nearest) == a
assert mpf_perturb(a, 0, 53, round_up) == c
assert mpf_perturb(a, 0, 53, round_ceiling) == c
assert mpf_perturb(a, 0, 53, round_down) == a
assert mpf_perturb(a, 0, 53, round_floor) == a
assert mpf_perturb(a, 1, 53, round_up) == a
assert mpf_perturb(a, 1, 53, round_ceiling) == a
assert mpf_perturb(a, 1, 53, round_down) == b
assert mpf_perturb(a, 1, 53, round_floor) == b
a = mpf_neg(a)
b = mpf_neg(b)
c = mpf_neg(c)
assert mpf_perturb(a, 0, 53, round_nearest) == a
assert mpf_perturb(a, 1, 53, round_nearest) == a
assert mpf_perturb(a, 0, 53, round_up) == a
assert mpf_perturb(a, 0, 53, round_floor) == a
assert mpf_perturb(a, 0, 53, round_down) == b
assert mpf_perturb(a, 0, 53, round_ceiling) == b
assert mpf_perturb(a, 1, 53, round_up) == c
assert mpf_perturb(a, 1, 53, round_floor) == c
assert mpf_perturb(a, 1, 53, round_down) == a
assert mpf_perturb(a, 1, 53, round_ceiling) == a
def test_add_exact():
ff = from_float
assert mpf_add(ff(3.0), ff(2.5)) == ff(5.5)
assert mpf_add(ff(3.0), ff(-2.5)) == ff(0.5)
assert mpf_add(ff(-3.0), ff(2.5)) == ff(-0.5)
assert mpf_add(ff(-3.0), ff(-2.5)) == ff(-5.5)
assert mpf_sub(mpf_add(fone, ff(1e-100)), fone) == ff(1e-100)
assert mpf_sub(mpf_add(ff(1e-100), fone), fone) == ff(1e-100)
assert mpf_sub(mpf_add(fone, ff(-1e-100)), fone) == ff(-1e-100)
assert mpf_sub(mpf_add(ff(-1e-100), fone), fone) == ff(-1e-100)
assert mpf_add(fone, fzero) == fone
assert mpf_add(fzero, fone) == fone
assert mpf_add(fzero, fzero) == fzero
def test_long_exponent_shifts():
mp.dps = 15
# Check for possible bugs due to exponent arithmetic overflow
# in a C implementation
x = mpf(1)
for p in [32, 64]:
a = ldexp(1,2**(p-1))
b = ldexp(1,2**p)
c = ldexp(1,2**(p+1))
d = ldexp(1,-2**(p-1))
e = ldexp(1,-2**p)
f = ldexp(1,-2**(p+1))
assert (x+a) == a
assert (x+b) == b
assert (x+c) == c
assert (x+d) == x
assert (x+e) == x
assert (x+f) == x
assert (a+x) == a
assert (b+x) == b
assert (c+x) == c
assert (d+x) == x
assert (e+x) == x
assert (f+x) == x
assert (x-a) == -a
assert (x-b) == -b
assert (x-c) == -c
assert (x-d) == x
assert (x-e) == x
assert (x-f) == x
assert (a-x) == a
assert (b-x) == b
assert (c-x) == c
assert (d-x) == -x
assert (e-x) == -x
assert (f-x) == -x
def test_float_rounding():
mp.prec = 64
for x in [mpf(1), mpf(1)+eps, mpf(1)-eps, -mpf(1)+eps, -mpf(1)-eps]:
fa = float(x)
fb = float(fadd(x,0,prec=53,rounding='n'))
assert fa == fb
z = mpc(x,x)
ca = complex(z)
cb = complex(fadd(z,0,prec=53,rounding='n'))
assert ca == cb
for rnd in ['n', 'd', 'u', 'f', 'c']:
fa = to_float(x._mpf_, rnd=rnd)
fb = to_float(fadd(x,0,prec=53,rounding=rnd)._mpf_, rnd=rnd)
assert fa == fb
mp.prec = 53
|