Numpy.sert_almost_equal — NumPy V1.23 Manual

Skip to main content Back to top Ctrl+K
  • User Guide
  • API reference
  • Building from source
  • Development
  • Release notes
  • Learn
  • NEPs
Choose version
  • GitHub

Section Navigation

  • NumPy’s module structure
  • Array objects
  • Universal functions (ufunc)
  • Routines and objects by topic
    • Constants
    • Array creation routines
    • Array manipulation routines
    • Bit-wise operations
    • String functionality
    • Datetime support functions
    • Data type routines
    • Mathematical functions with automatic domain
    • Floating point error handling
    • Exceptions and Warnings
    • Discrete Fourier Transform
    • Functional programming
    • Input and output
    • Indexing routines
    • Linear algebra
    • Logic functions
    • Masked array operations
    • Mathematical functions
    • Miscellaneous routines
    • Polynomials
    • Random sampling
    • Set routines
    • Sorting, searching, and counting
    • Statistics
    • Test support
      • numpy.testing.assert_allclose
      • numpy.testing.assert_array_almost_equal_nulp
      • numpy.testing.assert_array_max_ulp
      • numpy.testing.assert_array_equal
      • numpy.testing.assert_array_less
      • numpy.testing.assert_equal
      • numpy.testing.assert_raises
      • numpy.testing.assert_raises_regex
      • numpy.testing.assert_warns
      • numpy.testing.assert_no_warnings
      • numpy.testing.assert_no_gc_cycles
      • numpy.testing.assert_string_equal
      • numpy.testing.assert_
      • numpy.testing.assert_almost_equal
      • numpy.testing.assert_approx_equal
      • numpy.testing.assert_array_almost_equal
      • numpy.testing.print_assert_equal
      • numpy.testing.decorate_methods
      • numpy.testing.clear_and_catch_warnings
      • numpy.testing.measure
      • numpy.testing.rundocs
      • numpy.testing.suppress_warnings
      • numpy.testing.overrides.allows_array_function_override
      • numpy.testing.overrides.allows_array_ufunc_override
      • numpy.testing.overrides.get_overridable_numpy_ufuncs
      • numpy.testing.overrides.get_overridable_numpy_array_functions
      • Testing guidelines
    • Window functions
  • Typing (numpy.typing)
  • Packaging
  • NumPy C-API
  • Array API standard compatibility
  • CPU/SIMD optimizations
  • Thread Safety
  • Global Configuration Options
  • NumPy security
  • Testing guidelines
  • Status of numpy.distutils and migration advice
  • numpy.distutils user guide
  • NumPy and SWIG
  • NumPy reference
  • NumPy’s module structure
  • Test support
  • numpy.testing.assert_almost_equal
numpy.testing.assert_almost_equal# testing.assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True)[source]#

Raises an AssertionError if two items are not equal up to desired precision.

Note

It is recommended to use one of assert_allclose, assert_array_almost_equal_nulp or assert_array_max_ulp instead of this function for more consistent floating point comparisons.

The test verifies that the elements of actual and desired satisfy:

abs(desired-actual) < float64(1.5 * 10**(-decimal))

That is a looser test than originally documented, but agrees with what the actual implementation in assert_array_almost_equal did up to rounding vagaries. An exception is raised at conflicting values. For ndarrays this delegates to assert_array_almost_equal

Parameters: actualarray_like

The object to check.

desiredarray_like

The expected object.

decimalint, optional

Desired precision, default is 7.

err_msgstr, optional

The error message to be printed in case of failure.

verbosebool, optional

If True, the conflicting values are appended to the error message.

Raises: AssertionError

If actual and desired are not equal up to specified precision.

See also

assert_allclose

Compare two array_like objects for equality with desired relative and/or absolute precision.

assert_array_almost_equal_nulp, assert_array_max_ulp, assert_equal

Examples

Try it in your browser! >>> fromnumpy.testingimport assert_almost_equal >>> assert_almost_equal(2.3333333333333, 2.33333334) >>> assert_almost_equal(2.3333333333333, 2.33333334, decimal=10) Traceback (most recent call last): ... AssertionError: Arrays are not almost equal to 10 decimals ACTUAL: 2.3333333333333 DESIRED: 2.33333334 >>> assert_almost_equal(np.array([1.0,2.3333333333333]), ... np.array([1.0,2.33333334]), decimal=9) Traceback (most recent call last): ... AssertionError: Arrays are not almost equal to 9 decimals Mismatched elements: 1 / 2 (50%) Mismatch at index: [1]: 2.3333333333333 (ACTUAL), 2.33333334 (DESIRED) Max absolute difference among violations: 6.66669964e-09 Max relative difference among violations: 2.85715698e-09 ACTUAL: array([1. , 2.333333333]) DESIRED: array([1. , 2.33333334]) Go BackOpen In Tab On this page
  • testing.assert_almost_equal

Tag » Approx Equal Python