Skip to content

The return value of function math.isclose can be somewhat misleading #126165

Closed
@Xiaokang2022

Description

@Xiaokang2022

Documentation

The following example may be misleading to people who are not familiar with the function:

>>> math.isclose(math.sin(0), 0)
... True
>>> math.isclose(math.sin(math.pi), 0)
... False
>>> 

To a newbie, the above results seem ridiculous. Correspondingly, the result in numpy is as follows:

>>> numpy.isclose(math.sin(0), 0)
... np.True_
>>> numpy.isclose(math.sin(math.pi), 0)
... np.True_
>>> 

For reference, some values are given here:

>>> math.sin(0)
... 0.0
>>> math.sin(math.pi)
... 1.2246467991473532e-16
>>> 

I think, the reason for this is that the default value of parameter abs_tol for function math.isclose is too small. Here are the definitions of the two functions mentioned above:

math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
numpy.isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)

I think we might need to state this in the docstring of function math.isclose, or increase the value of the default parameter abs_tol appropriately.

By the way, the numpy version I'm using here is v2.1.

If there is something wrong with my previous content, please feel free to point it out, thank you!

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions