Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python math.isclose() Method

Python math.isclose() Method

Python math Module Python math Module

Python math.isclose() The method checks whether two values are close to each other, returning True if they are close and False otherwise.

math.isclose() Determines whether two values are considered close based on given absolute and relative tolerances.

Python version: 3.5

The formula is:

abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

Syntax

The syntax of the math.isclose() method is as follows:

math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)

Parameter Description:

Return Value

Returns a Boolean checking whether two values are close to each other; True if they are close, otherwise False.

Example

The following example checks whether two values are close to each other:

Example

Import the math package

import

math

Output whether the two values are close

print

(

math

.

isclose

(

8.005

,

8.450

,

abs_tol

=

0.4

)

)

print

(

math

.

isclose

(

8.005

,

8.450

,

abs_tol

=

0.5

)

)

Output result:


False
True

The following example checks whether two floating-point numbers are close:

Example

Import the math package

import

math

Can be used for floating-point comparisons

This outputs false, because 0.1+0.2 does not equal 0.3

print

(

0.1

0.2

==

0.3

)

print

(

0.1

0.2

)

This outputs true

print

(

math

.

isclose

(

0.1

0.2

,

0.3

)

)

Output result:


False
0.30000000000000004
True

Python math Module Python math Module