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, some of Hypatia's operators do. By default, their "zero threshold" is plus/minus 1e-16 (if you're not familiar with scientific notation, this is the reciprocal value of 10 to the 16th power, or 0.0000000000000001).

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

Likewise, the six compare operators regard their arguments as equal when the difference between them is below the zero threshold, and the statistical N0, N+ and N- operators count items with absolute values below the zero threshold as items that are zero.

 

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

 

Changing or Disabling the Zero Threshold

(This paragraph deals with a variable -- these 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 you assign to 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 calculations use 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 page Viewing and Editing Files in chapter "The Basics").

 

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 always set to 0 if its absolute value is smaller 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 smaller than 1e-16, independent of the zero threshold, but you are free to specify any threshold value instead of 0, including $zero. (If $zero is 0, then ... $zero GATE is the same as ... 0 GATE and the GATE threshold defaults to 1e-16. Actually setting it to zero would make no sense, it would be the same as not using the GATE operator.)

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

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

 

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