Skip to content

Chop ​

Replaces a real number very close to 0 with exact 0.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageChop!
Chop(x)0 if ∣x∣|x| is smaller than about 10−1010^{-10}, else x unchanged.
Chop(x, tolerance)0 if ∣x∣|x| is smaller than tolerance, else x unchanged.
Typed signatures
TypePackageApplies
(T) -> T where T: numbercompute-enginealways
(any, number?) -> anycombinatoricsreplaces compute-enginealways

Domain: Arithmetic

Details
  • Replaces a value smaller in magnitude than about 10−1010^{-10} with exact 0 -- cleanup for the floating-point noise left over from a numeric computation.
  • Chops the real and imaginary parts of a complex number independently.
  • Only the value's own magnitude matters -- Chop(1.000000000001)\mathrm{Chop}(1.000000000001) stays as is, since it isn't close to 0, even though it's close to the integer 1.
  • A second argument overrides the default 10−1010^{-10} threshold.

Examples

3 more kept as data, checked against the oracles
Basic #

Values not near zero pass through unchanged

live ↗
Inchop(1.2)\mathrm{chop}(1.2)Out1.21.2

Values smaller than about 10−1010^{-10} collapse to exactly 0

live ↗
Inchop(1.0⋅10−11)\mathrm{chop}(1.0\cdot10^{-11})Out0.00.0

Negative values chop the same way

live ↗
Inchop(−1.0⋅10−11)\mathrm{chop}(-1.0\cdot10^{-11})Out0.00.0

Chops the real and imaginary parts independently

live ↗
Inchop(1.0⋅10−12+3i)\mathrm{chop}(1.0\cdot10^{-12}+3i)Out3.0i3.0i
Scope #

Threads element-wise over a list

live ↗
Inchop([1.2,1.0⋅10−11,3.5])\mathrm{chop}(\bigl\lbrack1.2, 1.0\cdot10^{-11}, 3.5\bigr\rbrack)Out[1.2,0.0,3.5]\bigl\lbrack1.2, 0.0, 3.5\bigr\rbrack

A negligible imaginary part is dropped, leaving a real number

live ↗
Inchop(1+1.0⋅10−14i)\mathrm{chop}(1+1.0\cdot10^{-14}i)Out1.01.0

Stays symbolic

live ↗
Inchop(x)\mathrm{chop}(x)Outchop(x)\mathrm{chop}(x)

Exact numbers not near zero pass through

live ↗
Inchop(13)\mathrm{chop}(\frac{1}{3})Out13\frac{1}{3}

A second argument sets the tolerance, here chopping anything below 0.01

live ↗
Inchop(0.001,0.01)\mathrm{chop}(0.001, 0.01)Out00

...and a tighter tolerance keeps a value the default would chop

live ↗
Inchop(1.0⋅10−15,1.0⋅10−20)\mathrm{chop}(1.0\cdot10^{-15}, 1.0\cdot10^{-20})Out1.0⋅10−151.0\cdot10^{-15}
Applications #

Cleans up the floating-point residue from computing 3×(1/3)−13\times(1/3)-1 numerically

live ↗
Inchop(3N(13)−1)\mathrm{chop}(3N(\frac{1}{3})-1)Out0.00.0
Possible issues #

Only the value's own magnitude matters -- this stays as is since it isn't close to 0, even though it's close to the integer 1

live ↗
Inchop(1+1.0⋅10−12)\mathrm{chop}(1+1.0\cdot10^{-12})Out1.000 000 000 0011.000\,000\,000\,001

A hard cutoff at about 10−1010^{-10}: a value just above the threshold survives untouched

live ↗
Inchop(1.000 000 000 1⋅10−10)\mathrm{chop}(1.000\,000\,000\,1\cdot10^{-10})Out1.000 000 000 1⋅10−101.000\,000\,000\,1\cdot10^{-10}

An exact small number is chopped too

live ↗
Inchop(10−20)\mathrm{chop}(10^{-20})Out00

A negligible imaginary part is dropped

live ↗
Inchop(2+i×10−12)\mathrm{chop}(2+i×10^{-12})Out22
About
Examples18
Checked againstWolfram Language 17 agree1 differ
CrosswalkWolfram Language

See also: Round