#! /usr/bin/env python """ Precision.py is a program which calculate the smallest floating point number such that when it is added to the value 1.0 the sum due to round-off precision results in a value of 1.0 Paul Eugenio PHZ4151C 13 Feb 2019 """ from __future__ import division, print_function import numpy as np # define lists to hold functions and the function names # This make for easy index access to the numpy float types # ftype = [ np.float16, np.float32, np.float64, np.float128] fname = ["float16", "float32", "float64", "float128"] # find the percision for all 4 float types. Be certain that all variables # in an operations are of the same float type. for k in range( len(ftype) ): eps = ftype[k](1.0) # same as eps = np.float16(1.0) one = ftype[k](1.0) two = ftype[k](2.0) while one + eps != one: # find eps to within a factor of 2 eps = eps/two print(fname[k],": 1.0 + ",eps, "is ", ftype[k](one + eps) )