Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

Time for action – comparing using maxulp of 2


Let's do the same comparisons as in the previous Time for action section, but specify a maxulp of 2 when necessary:

  1. Determine the machine epsilon with the finfo() function:

    eps = np.finfo(float).eps
    print("EPS", eps)

    The epsilon would be as follows:

    EPS 2.22044604925e-16
    
  2. Do the comparisons as done in the previous Time for action section, but use the assert_array_max_ulp() function with the appropriate maxulp value:

    print("1", np.testing.assert_array_max_ulp(1.0, 1.0 + eps))
    print("2", np.testing.assert_array_max_ulp(1.0, 1 + 2 * eps, maxulp=2))

    The output is as follows:

    1 1.0
    2 2.0
    

What just happened?

We compared the same values as the previous Time for action section, but specified a maxulp of 2 in the second comparison. Using the assert_array_max_ulp() function with the appropriate maxulp value, these tests passed with a return value of the number of ULPs.