| 1 |
import sys, pytest, math |
| 2 |
|
| 3 |
from rpython.rlib.rfloat import float_as_rbigint_ratio |
| 4 |
from rpython.rlib.rfloat import round_away |
| 5 |
from rpython.rlib.rfloat import round_double |
| 6 |
from rpython.rlib.rfloat import erf, erfc, gamma, lgamma |
| 7 |
from rpython.rlib.rfloat import ulps_check, acc_check |
| 8 |
from rpython.rlib.rfloat import string_to_float, exp2, cbrt |
| 9 |
from rpython.rlib.rbigint import rbigint |
| 10 |
|
| 11 |
def test_round_away(): |
| 12 |
assert round_away(.1) == 0. |
| 13 |
assert round_away(.5) == 1. |
| 14 |
assert round_away(.7) == 1. |
| 15 |
assert round_away(1.) == 1. |
| 16 |
assert round_away(-.5) == -1. |
| 17 |
assert round_away(-.1) == 0. |
| 18 |
assert round_away(-.7) == -1. |
| 19 |
assert round_away(0.) == 0. |
| 20 |
|
| 21 |
def test_round_double(): |
| 22 |
def almost_equal(x, y): |
| 23 |
assert abs(x-y) < 1e-7 |
| 24 |
|
| 25 |
almost_equal(round_double(0.125, 2), 0.13) |
| 26 |
almost_equal(round_double(0.375, 2), 0.38) |
| 27 |
almost_equal(round_double(0.625, 2), 0.63) |
| 28 |
almost_equal(round_double(0.875, 2), 0.88) |
| 29 |
almost_equal(round_double(-0.125, 2), -0.13) |
| 30 |
almost_equal(round_double(-0.375, 2), -0.38) |
| 31 |
almost_equal(round_double(-0.625, 2), -0.63) |
| 32 |
almost_equal(round_double(-0.875, 2), -0.88) |
| 33 |
|
| 34 |
almost_equal(round_double(0.25, 1), 0.3) |
| 35 |
almost_equal(round_double(0.75, 1), 0.8) |
| 36 |
almost_equal(round_double(-0.25, 1), -0.3) |
| 37 |
almost_equal(round_double(-0.75, 1), -0.8) |
| 38 |
|
| 39 |
assert round_double(-6.5, 0) == -7.0 |
| 40 |
assert round_double(-5.5, 0) == -6.0 |
| 41 |
assert round_double(-1.5, 0) == -2.0 |
| 42 |
assert round_double(-0.5, 0) == -1.0 |
| 43 |
assert round_double(0.5, 0) == 1.0 |
| 44 |
assert round_double(1.5, 0) == 2.0 |
| 45 |
assert round_double(2.5, 0) == 3.0 |
| 46 |
assert round_double(3.5, 0) == 4.0 |
| 47 |
assert round_double(4.5, 0) == 5.0 |
| 48 |
assert round_double(5.5, 0) == 6.0 |
| 49 |
assert round_double(6.5, 0) == 7.0 |
| 50 |
|
| 51 |
assert round_double(-25.0, -1) == -30.0 |
| 52 |
assert round_double(-15.0, -1) == -20.0 |
| 53 |
assert round_double(-5.0, -1) == -10.0 |
| 54 |
assert round_double(5.0, -1) == 10.0 |
| 55 |
assert round_double(15.0, -1) == 20.0 |
| 56 |
assert round_double(25.0, -1) == 30.0 |
| 57 |
assert round_double(35.0, -1) == 40.0 |
| 58 |
assert round_double(45.0, -1) == 50.0 |
| 59 |
assert round_double(55.0, -1) == 60.0 |
| 60 |
assert round_double(65.0, -1) == 70.0 |
| 61 |
assert round_double(75.0, -1) == 80.0 |
| 62 |
assert round_double(85.0, -1) == 90.0 |
| 63 |
assert round_double(95.0, -1) == 100.0 |
| 64 |
assert round_double(12325.0, -1) == 12330.0 |
| 65 |
|
| 66 |
assert round_double(350.0, -2) == 400.0 |
| 67 |
assert round_double(450.0, -2) == 500.0 |
| 68 |
|
| 69 |
almost_equal(round_double(0.5e21, -21), 1e21) |
| 70 |
almost_equal(round_double(1.5e21, -21), 2e21) |
| 71 |
almost_equal(round_double(2.5e21, -21), 3e21) |
| 72 |
almost_equal(round_double(5.5e21, -21), 6e21) |
| 73 |
almost_equal(round_double(8.5e21, -21), 9e21) |
| 74 |
|
| 75 |
almost_equal(round_double(-1.5e22, -22), -2e22) |
| 76 |
almost_equal(round_double(-0.5e22, -22), -1e22) |
| 77 |
almost_equal(round_double(0.5e22, -22), 1e22) |
| 78 |
almost_equal(round_double(1.5e22, -22), 2e22) |
| 79 |
|
| 80 |
exact_integral = 5e15 + 1 |
| 81 |
assert round_double(exact_integral, 0) == exact_integral |
| 82 |
assert round_double(exact_integral/2.0, 0) == 5e15/2.0 + 1.0 |
| 83 |
exact_integral = 5e15 - 1 |
| 84 |
assert round_double(exact_integral, 0) == exact_integral |
| 85 |
assert round_double(exact_integral/2.0, 0) == 5e15/2.0 |
| 86 |
|
| 87 |
def test_round_half_even(): |
| 88 |
from rpython.rlib import rfloat |
| 89 |
func = rfloat.round_double |
| 90 |
|
| 91 |
assert func(2.5, 0, False) == 3.0 |
| 92 |
|
| 93 |
assert func(2.5, 0, True) == 2.0 |
| 94 |
for i in range(-10, 10): |
| 95 |
assert func(i + 0.5, 0, True) == i + (i & 1) |
| 96 |
assert func(i * 10 + 5, -1, True) == (i + (i & 1)) * 10 |
| 97 |
exact_integral = 5e15 + 1 |
| 98 |
assert round_double(exact_integral, 0, True) == exact_integral |
| 99 |
assert round_double(exact_integral/2.0, 0, True) == 5e15/2.0 |
| 100 |
exact_integral = 5e15 - 1 |
| 101 |
assert round_double(exact_integral, 0, True) == exact_integral |
| 102 |
assert round_double(exact_integral/2.0, 0, True) == 5e15/2.0 |
| 103 |
|
| 104 |
def test_float_as_rbigint_ratio(): |
| 105 |
for f, ratio in [ |
| 106 |
(0.875, (7, 8)), |
| 107 |
(-0.875, (-7, 8)), |
| 108 |
(0.0, (0, 1)), |
| 109 |
(11.5, (23, 2)), |
| 110 |
]: |
| 111 |
num, den = float_as_rbigint_ratio(f) |
| 112 |
assert num.eq(rbigint.fromint(ratio[0])) |
| 113 |
assert den.eq(rbigint.fromint(ratio[1])) |
| 114 |
|
| 115 |
with pytest.raises(OverflowError): |
| 116 |
float_as_rbigint_ratio(float('inf')) |
| 117 |
with pytest.raises(OverflowError): |
| 118 |
float_as_rbigint_ratio(float('-inf')) |
| 119 |
with pytest.raises(ValueError): |
| 120 |
float_as_rbigint_ratio(float('nan')) |
| 121 |
|
| 122 |
def test_mtestfile(): |
| 123 |
from rpython.rlib import rfloat |
| 124 |
import zipfile |
| 125 |
import os |
| 126 |
def _parse_mtestfile(fname): |
| 127 |
"""Parse a file with test values |
| 128 |
|
| 129 |
-- starts a comment |
| 130 |
blank lines, or lines containing only a comment, are ignored |
| 131 |
other lines are expected to have the form |
| 132 |
id fn arg -> expected [flag]* |
| 133 |
|
| 134 |
""" |
| 135 |
with open(fname) as fp: |
| 136 |
for line in fp: |
| 137 |
|
| 138 |
if '--' in line: |
| 139 |
line = line[:line.index('--')] |
| 140 |
if not line.strip(): |
| 141 |
continue |
| 142 |
|
| 143 |
lhs, rhs = line.split('->') |
| 144 |
id, fn, arg = lhs.split() |
| 145 |
rhs_pieces = rhs.split() |
| 146 |
exp = rhs_pieces[0] |
| 147 |
flags = rhs_pieces[1:] |
| 148 |
|
| 149 |
yield (id, fn, float(arg), float(exp), flags) |
| 150 |
|
| 151 |
ALLOWED_ERROR = 20 |
| 152 |
fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}" |
| 153 |
|
| 154 |
failures = [] |
| 155 |
math_testcases = os.path.join(os.path.dirname(__file__), |
| 156 |
"math_testcases.txt") |
| 157 |
for id, fn, arg, expected, flags in _parse_mtestfile(math_testcases): |
| 158 |
func = getattr(rfloat, fn) |
| 159 |
|
| 160 |
if 'invalid' in flags or 'divide-by-zero' in flags: |
| 161 |
expected = 'ValueError' |
| 162 |
elif 'overflow' in flags: |
| 163 |
expected = 'OverflowError' |
| 164 |
|
| 165 |
try: |
| 166 |
got = func(arg) |
| 167 |
except ValueError: |
| 168 |
got = 'ValueError' |
| 169 |
except OverflowError: |
| 170 |
got = 'OverflowError' |
| 171 |
|
| 172 |
accuracy_failure = None |
| 173 |
if isinstance(got, float) and isinstance(expected, float): |
| 174 |
if math.isnan(expected) and math.isnan(got): |
| 175 |
continue |
| 176 |
if not math.isnan(expected) and not math.isnan(got): |
| 177 |
if fn == 'lgamma': |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
accuracy_failure = acc_check(expected, got, |
| 183 |
rel_err = 5e-15, |
| 184 |
abs_err = 5e-15) |
| 185 |
elif fn == 'erfc': |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
accuracy_failure = ulps_check(expected, got, 2000) |
| 193 |
|
| 194 |
else: |
| 195 |
accuracy_failure = ulps_check(expected, got, 20) |
| 196 |
if accuracy_failure is None: |
| 197 |
continue |
| 198 |
|
| 199 |
if isinstance(got, str) and isinstance(expected, str): |
| 200 |
if got == expected: |
| 201 |
continue |
| 202 |
|
| 203 |
fail_msg = fail_fmt.format(id, fn, arg, expected, got) |
| 204 |
if accuracy_failure is not None: |
| 205 |
fail_msg += ' ({})'.format(accuracy_failure) |
| 206 |
failures.append(fail_msg) |
| 207 |
assert not failures |
| 208 |
|
| 209 |
|
| 210 |
def test_gamma_overflow_translated(): |
| 211 |
from rpython.translator.c.test.test_genc import compile |
| 212 |
def wrapper(arg): |
| 213 |
try: |
| 214 |
return gamma(arg) |
| 215 |
except OverflowError: |
| 216 |
return -42 |
| 217 |
|
| 218 |
f = compile(wrapper, [float]) |
| 219 |
assert f(10.0) == 362880.0 |
| 220 |
assert f(1720.0) == -42 |
| 221 |
assert f(172.0) == -42 |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
def test_string_to_float(): |
| 226 |
from rpython.rlib.rstring import ParseStringError |
| 227 |
import random |
| 228 |
assert string_to_float('0') == 0.0 |
| 229 |
assert string_to_float('1') == 1.0 |
| 230 |
assert string_to_float('-1.5') == -1.5 |
| 231 |
assert string_to_float('1.5E2') == 150.0 |
| 232 |
assert string_to_float('2.5E-1') == 0.25 |
| 233 |
assert string_to_float('1e1111111111111') == float('1e1111111111111') |
| 234 |
assert string_to_float('1e-1111111111111') == float('1e-1111111111111') |
| 235 |
assert string_to_float('-1e1111111111111') == float('-1e1111111111111') |
| 236 |
assert string_to_float('-1e-1111111111111') == float('-1e-1111111111111') |
| 237 |
assert string_to_float('1e111111111111111111111') == float('1e111111111111111111111') |
| 238 |
assert string_to_float('1e-111111111111111111111') == float('1e-111111111111111111111') |
| 239 |
assert string_to_float('-1e111111111111111111111') == float('-1e111111111111111111111') |
| 240 |
assert string_to_float('-1e-111111111111111111111') == float('-1e-111111111111111111111') |
| 241 |
|
| 242 |
valid_parts = [['', ' ', ' \f\n\r\t\v'], |
| 243 |
['', '+', '-'], |
| 244 |
['00', '90', '.5', '2.4', '3.', '0.07', |
| 245 |
'12.3489749871982471987198371293717398256187563298638726' |
| 246 |
'2187362820947193247129871083561249818451804287437824015' |
| 247 |
'013816418758104762348932657836583048761487632840726386'], |
| 248 |
['', 'e0', 'E+1', 'E-01', 'E42'], |
| 249 |
['', ' ', ' \f\n\r\t\v'], |
| 250 |
] |
| 251 |
invalid_parts = [['#'], |
| 252 |
['++', '+-', '-+', '--'], |
| 253 |
['', '1.2.3', '.', '5..6'], |
| 254 |
['E+', 'E-', 'e', 'e++', 'E++2'], |
| 255 |
['#'], |
| 256 |
] |
| 257 |
for part0 in valid_parts[0]: |
| 258 |
for part1 in valid_parts[1]: |
| 259 |
for part2 in valid_parts[2]: |
| 260 |
for part3 in valid_parts[3]: |
| 261 |
for part4 in valid_parts[4]: |
| 262 |
s = part0+part1+part2+part3+part4 |
| 263 |
assert (abs(string_to_float(s) - float(s)) <= |
| 264 |
1E-13 * abs(float(s))) |
| 265 |
|
| 266 |
for j in range(len(invalid_parts)): |
| 267 |
for invalid in invalid_parts[j]: |
| 268 |
for i in range(20): |
| 269 |
parts = [random.choice(lst) for lst in valid_parts] |
| 270 |
parts[j] = invalid |
| 271 |
s = ''.join(parts) |
| 272 |
print repr(s) |
| 273 |
if s.strip(): |
| 274 |
pytest.raises(ParseStringError, string_to_float, s) |
| 275 |
pytest.raises(ParseStringError, string_to_float, "") |
| 276 |
|
| 277 |
def test_string_to_float_nan(): |
| 278 |
nan = float('nan') |
| 279 |
pinf = float('inf') |
| 280 |
for s in ['nan', '+nan', '-nan', 'NAN', '+nAn']: |
| 281 |
assert math.isnan(string_to_float(s)) |
| 282 |
for s in ['inf', '+inf', '-inf', '-infinity', ' -infiNITy ']: |
| 283 |
assert math.isinf(string_to_float(s)) |
| 284 |
|
| 285 |
def test_log2(): |
| 286 |
from rpython.rlib import rfloat |
| 287 |
assert rfloat.log2(1.0) == 0.0 |
| 288 |
assert rfloat.log2(2.0) == 1.0 |
| 289 |
assert rfloat.log2(2.0**1023) == 1023.0 |
| 290 |
assert 1.584 < rfloat.log2(3.0) < 1.585 |
| 291 |
pytest.raises(ValueError, rfloat.log2, 0) |
| 292 |
pytest.raises(ValueError, rfloat.log2, -1) |
| 293 |
|
| 294 |
def test_nextafter(): |
| 295 |
from rpython.rlib.rfloat import nextafter |
| 296 |
|
| 297 |
INF = float("inf") |
| 298 |
NAN = float("nan") |
| 299 |
assert nextafter(4503599627370496.0, -INF) == 4503599627370495.5 |
| 300 |
assert nextafter(4503599627370496.0, INF) == 4503599627370497.0 |
| 301 |
assert nextafter(9223372036854775808.0, 0.0) == 9223372036854774784.0 |
| 302 |
assert nextafter(-9223372036854775808.0, 0.0) == -9223372036854774784.0 |
| 303 |
|
| 304 |
|
| 305 |
assert nextafter(1.0, -INF) == float.fromhex('0x1.fffffffffffffp-1') |
| 306 |
assert nextafter(1.0, INF)== float.fromhex('0x1.0000000000001p+0') |
| 307 |
|
| 308 |
|
| 309 |
assert nextafter(2.0, 2.0) == 2.0 |
| 310 |
|
| 311 |
|
| 312 |
smallest_subnormal = sys.float_info.min * sys.float_info.epsilon |
| 313 |
assert nextafter(+0.0, INF) == smallest_subnormal |
| 314 |
assert nextafter(-0.0, INF) == smallest_subnormal |
| 315 |
assert nextafter(+0.0, -INF) == -smallest_subnormal |
| 316 |
assert nextafter(-0.0, -INF) == -smallest_subnormal |
| 317 |
|
| 318 |
|
| 319 |
largest_normal = sys.float_info.max |
| 320 |
assert nextafter(INF, 0.0) == largest_normal |
| 321 |
assert nextafter(-INF, 0.0) == -largest_normal |
| 322 |
assert nextafter(largest_normal, INF) == INF |
| 323 |
assert nextafter(-largest_normal, -INF) == -INF |
| 324 |
|
| 325 |
|
| 326 |
assert math.isnan(nextafter(NAN, 1.0)) |
| 327 |
assert math.isnan(nextafter(1.0, NAN)) |
| 328 |
assert math.isnan(nextafter(NAN, NAN)) |
| 329 |
|
| 330 |
def test_exp2(): |
| 331 |
for i in range(-100, 100): |
| 332 |
assert exp2(float(i)) == 2.0 ** i |
| 333 |
assert ulps_check(exp2(2.3), 4.924577653379665, 5) is None |
| 334 |
assert exp2(float('inf')) == float('inf') |
| 335 |
assert exp2(-float('inf')) == 0.0 |
| 336 |
assert math.isnan(exp2(-float('nan'))) |
| 337 |
with pytest.raises(OverflowError): |
| 338 |
exp2(10000000) |
| 339 |
|
| 340 |
def test_cbrt(): |
| 341 |
assert cbrt(0.0) == 0.0 |
| 342 |
assert cbrt(1.0) == 1.0 |
| 343 |
assert cbrt(8.0) == 2.0 |
| 344 |
assert cbrt(0.0) == 0.0 |
| 345 |
assert ulps_check(cbrt(-0.0), -0.0, 0) is None |
| 346 |
assert ulps_check(cbrt(1.2), 1.062658569182611, 5) is None |
| 347 |
assert ulps_check(cbrt(-2.6), -1.375068867074141, 5) is None |
| 348 |
assert ulps_check(cbrt(27.0), 3.0, 5) is None |
| 349 |
assert ulps_check(cbrt(-27.0), -3.0, 5) is None |
| 350 |
assert cbrt(-1.0) == -1.0 |
| 351 |
assert cbrt(float('inf')) == float('inf') |
| 352 |
assert cbrt(-float('inf')) == -float('inf') |
| 353 |
assert math.isnan(cbrt(float('nan'))) |
| 354 |
|
| 355 |
def test_exp2_cbrt_translated(): |
| 356 |
from rpython.translator.c.test.test_genc import compile |
| 357 |
def wrapper(arg, use_exp2): |
| 358 |
try: |
| 359 |
if use_exp2: |
| 360 |
return exp2(arg) |
| 361 |
else: |
| 362 |
return cbrt(arg) |
| 363 |
except OverflowError: |
| 364 |
return -42 |
| 365 |
|
| 366 |
f = compile(wrapper, [float, bool]) |
| 367 |
|
| 368 |
for i in range(-100, 100): |
| 369 |
assert f(float(i), True) == 2.0 ** i |
| 370 |
assert f(10000000.0, True) == -42 |
| 371 |
|
| 372 |
assert f(0.0, False) == 0.0 |
| 373 |
assert f(1.0, False) == 1.0 |
| 374 |
assert f(8.0, False) == 2.0 |
| 375 |
assert f(0.0, False) == 0.0 |
| 376 |
assert f(-1.0, False) == -1.0 |
| 377 |
assert f(float('inf'), False) == float('inf') |
| 378 |
assert f(-float('inf'), False) == -float('inf') |