The Zero Threshold

Home, Up: Numbers

 

On this page:

The Zero Threshold, Changing or Disabling the Zero Threshold, Not Affected by the Zero Threshold.

 

The Zero Threshold

Because of unavoidable rounding differences, zero is not always exactly zero. For instance, in theory "1.2 SQRT SQ 1.2 -" is zero, but the actual result is -1.084...e-19. While this is an extremely small number (with 18 zeros after the decimal point), it isn’t really zero -- in most cases, though, when you ask "is the result zero?", you will want to disregard such minor deviations.

This is what, by default, the SIGN and the "IS" operators do. By default, their "zero threshold" is plus/minus 1e-16 (if you're not familiar with scientific notation, this is 10^-16 or 0.0000000000000001).

If the absolute value of their argument is less than the zero threshold, then IS0, ISNOT0, IS0+, IS0- and SIGN consider it to be zero (which will be important in the context of IF ... THEN clauses in scripts).

Likewise, the six compare operators regard the difference between their two arguments as zero when it is below the zero threshold, and the statistical N0, N+ and N- operators count absolute values below the zero threshold as zero.

 

Note that the zero threshold does not mean that you cannot use smaller values in your calculations -- the smallest possible value is 1e-1000 -- it only means that some operators treat values lower than the zero threshold as if they were zero.

 

Changing or Disabling the Zero Threshold

(This paragraph deals with a variable -- they will be discussed in the chapter "Variables". The names of all variables in Hypatia begin with $.)

You can change the zero threshold or disable it by setting the variable $zero, which overrides the default 1e-16 value.

$zero = 0 disables the zero threshold, only the exact value of 0 is taken to be zero.

$zero = ... sets the zero threshold to the value of the variable $zero (it can be 0, but must not be negative).

DEL $zero (deleting the $zero variable) resets the threshold to its default value of 1e-16.

If $zero is negative, an error will occur when an operator attempts to use it.

When your calculation uses very small or very large numbers, it is a good idea to adjust the zero threshold accordingly.

If you want the zero threshold to be changed or disabled by default, add the line $zero = ... to the file hy.ini (you can open this file with the command EDINI, see "Viewing and Editing Files").

 

Not Affected by the Zero Threshold

The following operators are not affected by the value of the zero threshold (a and b are the arguments):

a b // (a minus b divided by b) -- the result is set to 0 if its absolute value is less than 1e-16. Being a quotient, the result is independend of the order of magnitude of the numbers involved.

a 0 GATE -- the result is 0 if the absolute value of a is less than 1e-16, but you are free to specify any threshold value instead of 0, including $zero.

Note: "a $zero GATE" sets absolute values less than the zero threshold to 0. Unlike with the SIGN and IS operators, though, you cannot disable the threshold by setting $zero to 0.

If $zero is 0, then "a $zero GATE" is the same as "a 0 GATE" and the threshold defaults to 1e-16. (Actually setting it to zero wouldn't make sense, it would be the same as simply not using the GATE operator.)

SIN and COS always round their results to 0 if they are less than plus/minus 1e-18.

Independent of the zero threshold, any operator's smallest possible result is plus/minus 1e-1000, anything smaller is always zero.

 

Home, Up: Numbers, Prev: Angles, Next: Integer Bias