Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Examples of 32bit float's displayed as 64bit floats in CPython.
- 0.0005 -> 0.0005000000237487257
- 0.025 -> 0.02500000037252903
- 0.04 -> 0.03999999910593033
- 0.05 -> 0.05000000074505806
- 0.3 -> 0.30000001192092896
- 0.98 -> 0.9800000190734863
- 1.2 -> 1.2000000476837158
- 4096.3 -> 4096.2998046875
- def as_float_32(f):
- from struct import pack, unpack
- return unpack("f", pack("f", f))[0]
- print(0.025) # --> 0.025
- print(as_float_32(0.025)) # --> 0.02500000037252903
- >>> x = "634278e13"
- >>> y = float(np.float32(x))
- >>> y
- 6.342780214942106e+18
- >>> "{:.6g}".format(y)
- '6.34278e+18'
- >>> x = np.float32(3.14159265358979)
- >>> x
- 3.1415927
- >>> np.float32('{:.9g}'.format(x)) == x
- True
- def original_string(x):
- for places in range(6, 10): # try 6, 7, 8, 9
- s = '{:.{}g}'.format(x, places)
- y = np.float32(s)
- if x == y:
- return s
- # If x was genuinely a float32, we should never get here.
- raise RuntimeError("We should never get here")
- >>> original_string(0.02500000037252903)
- '0.025'
- >>> original_string(0.03999999910593033)
- '0.04'
- >>> original_string(0.05000000074505806)
- '0.05'
- >>> original_string(0.30000001192092896)
- '0.3'
- >>> original_string(0.9800000190734863)
- '0.98'
- >>> x = 2.0**87
- >>> x
- 1.5474250491067253e+26
- >>> s = '{:.8g}'.format(x)
- >>> s
- '1.547425e+26'
- >>> np.float32(s) == x
- False
- >>> np.float32('1.5474251e+26') == x
- True
- >>> x = 2**-96.
- >>> x
- 1.262177448353619e-29
- >>> s = '{:.8g}'.format(x)
- >>> s
- '1.2621774e-29'
- >>> np.float32(s) == x
- False
- >>> np.float32('1.2621775e-29') == x
- True
- def original_string(x):
- """
- Given a single-precision positive normal value x,
- return the shortest decimal numeric string which produces x.
- """
- # Deal with the three awkward cases.
- if x == 2**-96.:
- return '1.2621775e-29'
- elif x == 2**87:
- return '1.5474251e+26'
- elif x == 2**90:
- return '1.2379401e+27'
- for places in range(6, 10): # try 6, 7, 8, 9
- s = '{:.{}g}'.format(x, places)
- y = np.float32(s)
- if x == y:
- return s
- # If x was genuinely a float32, we should never get here.
- raise RuntimeError("We should never get here")
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from decimal import Decimal
- data = (
- 0.02500000037252903,
- 0.03999999910593033,
- 0.05000000074505806,
- 0.30000001192092896,
- 0.9800000190734863,
- )
- for f in data:
- dec = Decimal(f).quantize(Decimal('1.0000000')).normalize()
- print("Original %s -> %s" % (f, dec))
- Original 0.0250000003725 -> 0.025
- Original 0.0399999991059 -> 0.04
- Original 0.0500000007451 -> 0.05
- Original 0.300000011921 -> 0.3
- Original 0.980000019073 -> 0.98
- def round_float_32(f):
- from struct import pack, unpack
- return unpack("f", pack("f", f))[0]
- def as_float_low_precision_repr(f, round_fn):
- f_round = round_fn(f)
- f_str = repr(f)
- f_str_frac = f_str.partition(".")[2]
- if not f_str_frac:
- return f_str
- for i in range(1, len(f_str_frac)):
- f_test = round(f, i)
- f_test_round = round_fn(f_test)
- if f_test_round == f_round:
- return "%.*f" % (i, f_test)
- return f_str
- # ----
- data = (
- 0.02500000037252903,
- 0.03999999910593033,
- 0.05000000074505806,
- 0.30000001192092896,
- 0.9800000190734863,
- 1.2000000476837158,
- 4096.2998046875,
- )
- for f in data:
- f_as_float_32 = as_float_low_precision_repr(f, round_float_32)
- print("%s -> %s" % (f, f_as_float_32))
- 0.02500000037252903 -> 0.025
- 0.03999999910593033 -> 0.04
- 0.05000000074505806 -> 0.05
- 0.30000001192092896 -> 0.3
- 0.9800000190734863 -> 0.98
- 1.2000000476837158 -> 1.2
- 4096.2998046875 -> 4096.3
- a = 0.1
- a.as_integer_ratio()
- (3602879701896397, 36028797018963968)
- # A value in python floating point precision
- a = 0.1
- # The value as ratio of integers
- b = a.as_integer_ratio()
- import numpy as np
- # Force the result to have some precision:
- res = np.array([0], dtype=np.float16)
- np.true_divide(b[0], b[1], res)
- print(res)
- # Compare that two the wanted result when inputting 0.01
- np.true_divide(1, 10, res)
- print(res)
- # Other precisions:
- res = np.array([0], dtype=np.float32)
- np.true_divide(b[0], b[1], res)
- print(res)
- res = np.array([0], dtype=np.float64)
- np.true_divide(b[0], b[1], res)
- print(res)
- [ 0.09997559] # Float16 with integer-ratio
- [ 0.09997559] # Float16 reference
- [ 0.1] # Float32
- [ 0.1] # Float64
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement