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 with assert_array_almost_equal_nulp


Let's see the assert_array_almost_equal_nulp() function in action:

  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. Compare 1.0 with 1 + epsilon using the assert_almost_equal_nulp() function. Do the same for 1 + 2 * epsilon:

    print("1", np.testing.assert_array_almost_equal_nulp(1.0, 1.0 + eps))
    print("2", np.testing.assert_array_almost_equal_nulp(1.0, 1.0 + 2 * eps))

    The result is as follows:

    1 None
    2
    Traceback (most recent call last):
    
     assert_array_almost_equal_nulp
        raise AssertionError(msg)
    AssertionError: X and Y are not equal to 1 ULP (max is 2)
    

What just happened?

We determined the machine epsilon with the finfo() function. We then compared 1.0 with 1 + epsilon with the assert_almost_equal_nulp() function. This test passed however, adding another epsilon resulted in an exception.