| 1 | import math, sys |
| 2 | from rpython.jit.metainterp.test.support import LLJitMixin |
| 3 | from rpython.rlib.rarithmetic import intmask, r_uint |
| 4 | |
| 5 | |
| 6 | class FloatTests: |
| 7 | |
| 8 | def test_simple(self): |
| 9 | def f(a, b, c, d, e): |
| 10 | return (((a + b) - c) * d) / e |
| 11 | res = self.interp_operations(f, [41.5, 2.25, 17.5, 3.0, 2.5]) |
| 12 | assert res == 31.5 |
| 13 | |
| 14 | def test_cast_bool_to_float(self): |
| 15 | def f(a): |
| 16 | return float(a == 12.0) |
| 17 | res = self.interp_operations(f, [41.5]) |
| 18 | assert res == 0.0 |
| 19 | res = self.interp_operations(f, [12.0]) |
| 20 | assert res == 1.0 |
| 21 | |
| 22 | def test_abs(self): |
| 23 | def f(a): |
| 24 | return abs(a) |
| 25 | res = self.interp_operations(f, [-5.25]) |
| 26 | assert res == 5.25 |
| 27 | x = 281474976710656.31 |
| 28 | res = self.interp_operations(f, [x]) |
| 29 | assert res == x |
| 30 | |
| 31 | def test_neg(self): |
| 32 | def f(a): |
| 33 | return -a |
| 34 | res = self.interp_operations(f, [-5.25]) |
| 35 | assert res == 5.25 |
| 36 | x = 281474976710656.31 |
| 37 | res = self.interp_operations(f, [x]) |
| 38 | assert res == -x |
| 39 | |
| 40 | def test_singlefloat(self): |
| 41 | from rpython.rlib.rarithmetic import r_singlefloat |
| 42 | def f(a): |
| 43 | a = float(r_singlefloat(a)) |
| 44 | a *= 4.25 |
| 45 | return float(r_singlefloat(a)) |
| 46 | res = self.interp_operations(f, [-2.0], supports_singlefloats=True) |
| 47 | assert res == -8.5 |
| 48 | |
| 49 | def test_cast_float_to_int(self): |
| 50 | def g(f): |
| 51 | return int(f) |
| 52 | res = self.interp_operations(g, [-12345.9]) |
| 53 | assert res == -12345 |
| 54 | |
| 55 | def test_cast_float_to_uint(self): |
| 56 | def g(f): |
| 57 | return intmask(r_uint(f)) |
| 58 | res = self.interp_operations(g, [sys.maxint*2.0]) |
| 59 | assert res == intmask(long(sys.maxint*2.0)) |
| 60 | res = self.interp_operations(g, [-12345.9]) |
| 61 | assert res == -12345 |
| 62 | |
| 63 | def test_cast_int_to_float(self): |
| 64 | def g(i): |
| 65 | return float(i) |
| 66 | res = self.interp_operations(g, [-12345]) |
| 67 | assert type(res) is float and res == -12345.0 |
| 68 | |
| 69 | def test_cast_int_to_float_constant(self): |
| 70 | def h(i): |
| 71 | if i < 10: |
| 72 | i = 10 |
| 73 | return i |
| 74 | def g(i): |
| 75 | return float(h(i)) |
| 76 | res = self.interp_operations(g, [-12345]) |
| 77 | assert type(res) is float and res == 10.0 |
| 78 | |
| 79 | def test_cast_uint_to_float(self): |
| 80 | def g(i): |
| 81 | return float(r_uint(i)) |
| 82 | res = self.interp_operations(g, [intmask(sys.maxint*2)]) |
| 83 | assert type(res) is float and res == float(sys.maxint*2) |
| 84 | res = self.interp_operations(g, [-12345]) |
| 85 | assert type(res) is float and res == float(long(r_uint(-12345))) |
| 86 | |
| 87 | |
| 88 | class TestLLtype(FloatTests, LLJitMixin): |
| 89 | pass |