Skip to main content

numerics - Is there a way to globally set when to treat a very small number as zero?


I understand that I can use Chop to force a very small number to be treated as 0 and can use PossibleZeroQ to as a way to test whether such a number might effectively be 0, but applying Chop every time a small number is close to zero in order to "make it be" zero is tedious and error prone; while PossibleZeroQ seems to have its own ideas about what constitutes 0.



Are there global settings that will let me



  • treat every number smaller than some specified value as 0, effectively applying Chop automatically to all results; and

  • specify how large a number PossibleZeroQ should recognize as 0?



Answer



As was pointed out in the comments, $Post is a way to go here. Take as an example:


FourierDCT[FourierDCT[{0, 0, 1, 0, 0}, 2], 3]

(*{0., -5.55112*10^-17, 1., 1.38778*10^-17, 1.38778*10^-17}*)


Now lets set $Post to Chop with tolerance 10^-13.


$Post = Chop[#, 10^-13] &;

This will apply the Chop function to every output thereafter.


FourierDCT[FourierDCT[{0, 0, 1, 0, 0}, 2], 3]

(*{0, 0, 1., 0, 0}*)

If you want to make this behavior semi-permanent, you can set $Post in any one of your init.m files. A good choice might be the one located at



$InstallationDirectory <> "\\SystemFiles\\Kernel\\Packages\\init.m"

If you do this and want to not Chop for a particular output you can always clear $Post or remove this from init.m and restart the kernel.


Comments