0
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ Maybe the Clip? $\endgroup$
    – cvgmt
    Oct 19, 2022 at 12:56
  • $\begingroup$ According to the documentation it looks like Chop. Is there any specific way to apply to function? Not function output $\endgroup$
    – XXX1010
    Oct 19, 2022 at 13:02
  • $\begingroup$ 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} $\endgroup$
    – Bob Hanlon
    Oct 19, 2022 at 13:49

1 Answer 1

1
$\begingroup$

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.