summaryrefslogtreecommitdiff
path: root/tests/float
diff options
context:
space:
mode:
authorGlenn Ruben Bakke <glennbakke@gmail.com>2017-03-07 17:46:34 +0100
committerGlenn Ruben Bakke <glennbakke@gmail.com>2017-03-07 17:46:34 +0100
commitfd8f2c36f539a386192d106ef5ca0ce6a3926aec (patch)
tree7b0bf8cb7a0677c1987b2d3e725f57cffc2e8633 /tests/float
parent5f705adae06d4925351fe716f956d0fc922e53cf (diff)
parentfd49ff991769a29b2dcc0f8ed88143487f2bd131 (diff)
Merge branch 'master' into nrf5_no_sdk
Diffstat (limited to 'tests/float')
-rw-r--r--tests/float/complex1.py3
-rw-r--r--tests/float/complex1_intbig.py4
-rw-r--r--tests/float/float2int_doubleprec_intbig.py (renamed from tests/float/float2int_doubleprec.py)32
-rw-r--r--tests/float/float2int_fp30_intbig.py (renamed from tests/float/float2int_fp30.py)32
-rw-r--r--tests/float/float2int_intbig.py (renamed from tests/float/float2int.py)34
-rw-r--r--tests/float/string_format_modulo2.py4
-rw-r--r--tests/float/string_format_modulo2_intbig.py21
7 files changed, 89 insertions, 41 deletions
diff --git a/tests/float/complex1.py b/tests/float/complex1.py
index 1031111f3..8c8d285e1 100644
--- a/tests/float/complex1.py
+++ b/tests/float/complex1.py
@@ -49,9 +49,6 @@ print(float('nan') * 1j)
print(float('inf') * (1 + 1j))
print(float('-inf') * (1 + 1j))
-# convert bignum to complex on rhs
-ans = 1j + (1 << 70); print("%.5g %.5g" % (ans.real, ans.imag))
-
# can't assign to attributes
try:
(1j).imag = 0
diff --git a/tests/float/complex1_intbig.py b/tests/float/complex1_intbig.py
new file mode 100644
index 000000000..ed2390bba
--- /dev/null
+++ b/tests/float/complex1_intbig.py
@@ -0,0 +1,4 @@
+# test basic complex number functionality
+
+# convert bignum to complex on rhs
+ans = 1j + (1 << 70); print("%.5g %.5g" % (ans.real, ans.imag))
diff --git a/tests/float/float2int_doubleprec.py b/tests/float/float2int_doubleprec_intbig.py
index acdc8c69c..de2137d66 100644
--- a/tests/float/float2int_doubleprec.py
+++ b/tests/float/float2int_doubleprec_intbig.py
@@ -5,21 +5,29 @@ try:
except:
import struct
+import sys
+maxsize_bits = 0
+maxsize = sys.maxsize
+while maxsize:
+ maxsize >>= 1
+ maxsize_bits += 1
+
# work out configuration values
-is_64bit = struct.calcsize("P") == 8
+is_64bit = maxsize_bits > 32
# 0 = none, 1 = long long, 2 = mpz
-try:
- dummy = 0x7fffffffffffffff
- try:
- if (0xffffffffffffffff + 1) > 0:
- ll_type = 2
- else:
- ll_type = 1
- except:
- # in case the sum in the if statement above changes to raising an exception on overflow
+ll_type = None
+if is_64bit:
+ if maxsize_bits < 63:
+ ll_type = 0
+else:
+ if maxsize_bits < 31:
+ ll_type = 0
+if ll_type is None:
+ one = 1
+ if one << 65 < one << 62:
ll_type = 1
-except:
- ll_type = 0
+ else:
+ ll_type = 2
# This case occurs with time.time() values
if ll_type != 0:
diff --git a/tests/float/float2int_fp30.py b/tests/float/float2int_fp30_intbig.py
index bad9c31e9..fbb94a4cc 100644
--- a/tests/float/float2int_fp30.py
+++ b/tests/float/float2int_fp30_intbig.py
@@ -5,21 +5,29 @@ try:
except:
import struct
+import sys
+maxsize_bits = 0
+maxsize = sys.maxsize
+while maxsize:
+ maxsize >>= 1
+ maxsize_bits += 1
+
# work out configuration values
-is_64bit = struct.calcsize("P") == 8
+is_64bit = maxsize_bits > 32
# 0 = none, 1 = long long, 2 = mpz
-try:
- dummy = 0x7fffffffffffffff
- try:
- if (0xffffffffffffffff + 1) > 0:
- ll_type = 2
- else:
- ll_type = 1
- except:
- # in case the sum in the if statement above changes to raising an exception on overflow
+ll_type = None
+if is_64bit:
+ if maxsize_bits < 63:
+ ll_type = 0
+else:
+ if maxsize_bits < 31:
+ ll_type = 0
+if ll_type is None:
+ one = 1
+ if one << 65 < one << 62:
ll_type = 1
-except:
- ll_type = 0
+ else:
+ ll_type = 2
# basic conversion
print(int(14187744.))
diff --git a/tests/float/float2int.py b/tests/float/float2int_intbig.py
index da530cee6..3596d2f73 100644
--- a/tests/float/float2int.py
+++ b/tests/float/float2int_intbig.py
@@ -5,21 +5,31 @@ try:
except:
import struct
+import sys
+
+maxsize_bits = 0
+maxsize = sys.maxsize
+while maxsize:
+ maxsize >>= 1
+ maxsize_bits += 1
+
# work out configuration values
-is_64bit = struct.calcsize("P") == 8
+is_64bit = maxsize_bits > 32
# 0 = none, 1 = long long, 2 = mpz
-try:
- dummy = 0x7fffffffffffffff
- try:
- if (0xffffffffffffffff + 1) > 0:
- ll_type = 2
- else:
- ll_type = 1
- except:
- # in case the sum in the if statement above changes to raising an exception on overflow
+ll_type = None
+if is_64bit:
+ if maxsize_bits < 63:
+ ll_type = 0
+else:
+ if maxsize_bits < 31:
+ ll_type = 0
+if ll_type is None:
+ one = 1
+ if one << 65 < one << 62:
ll_type = 1
-except:
- ll_type = 0
+ else:
+ ll_type = 2
+
# basic conversion
print(int(14187745.))
diff --git a/tests/float/string_format_modulo2.py b/tests/float/string_format_modulo2.py
index d35f2a2c0..f6b1ae537 100644
--- a/tests/float/string_format_modulo2.py
+++ b/tests/float/string_format_modulo2.py
@@ -18,7 +18,7 @@ def test(num, num_str):
# check pure zero
test(0.0, '0.0')
-# check most powers of 10, making sure to include exponents with 3 digits
-for e in range(-101, 102):
+# check some powers of 10, making sure to include exponents with 3 digits
+for e in range(-8, 8):
num = pow(10, e)
test(num, '1e%d' % e)
diff --git a/tests/float/string_format_modulo2_intbig.py b/tests/float/string_format_modulo2_intbig.py
new file mode 100644
index 000000000..9992ba65d
--- /dev/null
+++ b/tests/float/string_format_modulo2_intbig.py
@@ -0,0 +1,21 @@
+# test formatting floats with large precision, that it doesn't overflow the buffer
+
+def test(num, num_str):
+ if num == float('inf') or num == 0.0 and num_str != '0.0':
+ # skip numbers that overflow or underflow the FP precision
+ return
+ for kind in ('e', 'f', 'g'):
+ # check precision either side of the size of the buffer (32 bytes)
+ for prec in range(23, 36, 2):
+ fmt = '%.' + '%d' % prec + kind
+ s = fmt % num
+ check = abs(float(s) - num)
+ if num > 1:
+ check /= num
+ if check > 1e-6:
+ print('FAIL', num_str, fmt, s, len(s), check)
+
+# check most powers of 10, making sure to include exponents with 3 digits
+for e in range(-101, 102):
+ num = pow(10, e)
+ test(num, '1e%d' % e)