Function Repository Resource:

IntegerChop

Source Notebook

Round near-integer values to integers

Contributed by: Ed Pegg Jr

ResourceFunction["IntegerChop"][n]

rounds n to an integer if n is close to an integer.

ResourceFunction["IntegerChop"][n,delta]

rounds n if the absolute difference between n and the result is within delta.

Details

The default tolerance for delta is 10-10.
ResourceFunction["IntegerChop"] threads over lists.

Examples

Basic Examples (2) 

Round a real number close to 7 to the integer 7:

In[1]:=
ResourceFunction["IntegerChop"][7.00000000000000000000000001]
Out[1]=

The following values are not close enough and remain unchanged:

In[2]:=
ResourceFunction["IntegerChop"][7.0000000001]
Out[2]=
In[3]:=
ResourceFunction["IntegerChop"][6.9999999999]
Out[3]=

Round elements of a list that are close to integers:

In[4]:=
InverseFourier[Fourier[Range[20]]]
Out[4]=
In[5]:=
ResourceFunction["IntegerChop"][%]
Out[5]=

Scope (3) 

The Ramanujan constant gets chopped by default:

In[6]:=
ResourceFunction["IntegerChop"][E^(Sqrt[163] \[Pi])]
Out[6]=

The number is within 10-10 of an integer:

In[7]:=
AccountingForm[N[E^(Sqrt[163] \[Pi]), 32]]
Out[7]=

A tighter bound eliminates the chopping:

In[8]:=
ResourceFunction["IntegerChop"][E^(Sqrt[163] \[Pi]), 10^-13]
Out[8]=

Version History

  • 1.0.0 – 26 May 2021

Related Resources

License Information