# How to chop a function?

Suppose I want to write a function that produces small values. I know the Chop can be used to treat very small numbers as zeros. How do I force the small output of a function to be automatically zero (i.e. not every time I evaluate the function)

Also, Is there any command that I could add once in the begining of the notebook so that every number (in a function or array) that goes below some threshold automatically evaluate to zero?

Example is

f[x_] = 10^-300*x


and what I want to do is something like this

f[x_] = Chop[f[x],10^-16]


which clearly doesn't work

• Maybe the Clip? Oct 19, 2022 at 12:56
• According to the documentation it looks like Chop. Is there any specific way to apply to function? Not function output Oct 19, 2022 at 13:02
• The Chop doesn't work in your example because you used infinite precision numbers rather than reals and you used Set rather than SetDelayed. Use f[x_] := Chop[(10^-300)*x], then f /@ (10.^(100 Range[4])) evaluates to {0, 0, 1., 9.99999999999997*10^99} Oct 19, 2022 at 13:49

There is a variable named "$Post". If you give this variable a value of a function, this function is applied to every output. Further, Chop does not act on accurate numbers. To make it do it, you must first change the number to a machine number. Here is how you would proceed: Set $Post = Chop[N@#, 10^-16] &


From now on until every output of the current session will be truncated. E.g.:

10^-10 10^-7.
(* 0 *)

Cos[Pi/2 + 10^-17]
(* 0 *)

• Out of curiosity how did you come across this variable? I knew one must exist but wasn't sure how to search / find it Oct 19, 2022 at 14:15
• @IntroductionToProbability I must say your user name made me laugh so hard! Oct 19, 2022 at 15:23
• @ IntroductionToProbability I already used $Pre once and then it was no big jump to assume that$Post exists. Oct 19, 2022 at 15:31
• @DanielHuber Ah that makes sense. Thanks for highlighting it Oct 19, 2022 at 15:51
• @XXX1010 Glad I could entertain :) Oct 19, 2022 at 15:51