Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save michaelchughes/96af54089de84d674720f09f8e494c01 to your computer and use it in GitHub Desktop.
Save michaelchughes/96af54089de84d674720f09f8e494c01 to your computer and use it in GitHub Desktop.
vals_float32 = np.logspace(0, 5, dtype=np.float32)
vals_float64 = np.logspace(0, 5, dtype=np.float64)
## Pretty-print output of array so each float takes same num chars
def pprint_arr(arr, n_per_line=6):
for s in range(0, arr.size, n_per_line):
chunk = arr[s:s+n_per_line]
print(" ".join(["%10s" % np.format_float_scientific(x, precision=2, unique=False, exp_digits=3) for x in chunk]))
print()
print("=================== with 32-bit floats")
## Grid of possible inputs
pprint_arr(vals_float32)
pprint_arr(np.exp(vals_float32))
print()
print("=================== with 64-bit floats")
## Grid of possible inputs
pprint_arr(vals_float64)
pprint_arr(np.exp(vals_float64))
@michaelchughes
Copy link
Author

Expected Output

=================== with 32-bit floats
 1.00e+000  1.26e+000  1.60e+000  2.02e+000  2.56e+000  3.24e+000
 4.09e+000  5.18e+000  6.55e+000  8.29e+000  1.05e+001  1.33e+001
 1.68e+001  2.12e+001  2.68e+001  3.39e+001  4.29e+001  5.43e+001
 6.87e+001  8.69e+001  1.10e+002  1.39e+002  1.76e+002  2.22e+002
 2.81e+002  3.56e+002  4.50e+002  5.69e+002  7.20e+002  9.10e+002
 1.15e+003  1.46e+003  1.84e+003  2.33e+003  2.95e+003  3.73e+003
 4.71e+003  5.96e+003  7.54e+003  9.54e+003  1.21e+004  1.53e+004
 1.93e+004  2.44e+004  3.09e+004  3.91e+004  4.94e+004  6.25e+004
 7.91e+004  1.00e+005

 2.72e+000  3.54e+000  4.95e+000  7.57e+000  1.29e+001  2.55e+001
 6.00e+001  1.78e+002  7.00e+002  3.97e+003  3.56e+004  5.72e+005
 1.92e+007  1.63e+009  4.48e+011  5.45e+014  4.36e+018  3.77e+023
 6.62e+029  5.24e+037        inf        inf        inf        inf
       inf        inf        inf        inf        inf        inf
       inf        inf        inf        inf        inf        inf
       inf        inf        inf        inf        inf        inf
       inf        inf        inf        inf        inf        inf
       inf        inf

=================== with 64-bit floats
 1.00e+000  1.26e+000  1.60e+000  2.02e+000  2.56e+000  3.24e+000
 4.09e+000  5.18e+000  6.55e+000  8.29e+000  1.05e+001  1.33e+001
 1.68e+001  2.12e+001  2.68e+001  3.39e+001  4.29e+001  5.43e+001
 6.87e+001  8.69e+001  1.10e+002  1.39e+002  1.76e+002  2.22e+002
 2.81e+002  3.56e+002  4.50e+002  5.69e+002  7.20e+002  9.10e+002
 1.15e+003  1.46e+003  1.84e+003  2.33e+003  2.95e+003  3.73e+003
 4.71e+003  5.96e+003  7.54e+003  9.54e+003  1.21e+004  1.53e+004
 1.93e+004  2.44e+004  3.09e+004  3.91e+004  4.94e+004  6.25e+004
 7.91e+004  1.00e+005

 2.72e+000  3.54e+000  4.95e+000  7.57e+000  1.29e+001  2.55e+001
 6.00e+001  1.78e+002  7.00e+002  3.97e+003  3.56e+004  5.72e+005
 1.92e+007  1.63e+009  4.48e+011  5.45e+014  4.36e+018  3.77e+023
 6.62e+029  5.24e+037  5.12e+047  2.21e+060  2.13e+076  3.50e+096
 1.30e+122  2.86e+154  2.31e+195  1.28e+247        inf        inf
       inf        inf        inf        inf        inf        inf
       inf        inf        inf        inf        inf        inf
       inf        inf        inf        inf        inf        inf
       inf        inf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment