9!:18 and 9!:19

From J Wiki
< Vocabulary‎ | Foreigns‎ | 9
Jump to navigation Jump to search

Global Parameters/Comparison Tolerance

Comparison tolerance means that inexact numeric types (floating-point and complex) are considered equal if they are "close enough". The parameter specifies how close is "close enough".

9!:18'' reports the current value of comparison tolerance.

9!:19(y) sets comparison tolerance to y. Comparison tolerance must be non-negative and less than 2^-34.

MOTIVATION

J uses IEEE-754 double precision binary floating point arithmetic to represent numbers. The machine implementations for numbers in this format are fast, and efficient, but some numbers are represented only approximately.

   ":!.30(0.1)
0.100000000000000005551115123126

A related issue is that some calculations can only provide approximate results.

In "real life" we have a similar issue, which is that our measurements have limited precision. It's difficult to obtain more than four digits of accuracy in measurements, and often we work with less accuracy than that.

By default, J displays numbers to six digits of accuracy.

But sometimes when comparing numbers, for example 0.99999999999999 and 1.00000000000000, they can be basically the same number within the limits of accuracy even though they are represented differently using more precision than available accuracy.

Accuracy and Precision



APPROACH

When comparing numbers represented internally using a floating point representation, J considers them equal if the absolute value of their difference is less than the conparison tolerance times the larger of the absolute values of the numbers. If e is the comparison tolerance, x and y are equal if (|x-y) < e*(|x)>.(|y) or, expressed as a tacit verb: x(|@- < e * >.&|)y.

Comparison tolerance should be significantly less than 1. Normally, comparison tolerance is 2^-44 (0.00000000000005684341886080801486968994140625).



Operations which test for equality (<, <:, >, >:, +., *., -., -:, |, E., i.and i:) will accept comparison tolerance specified using !. (for example -:!.0 looks for an exact match instead of a tolerant match).


See also:
Sharp APL Technical Note 23
Absolute and Relative Tolerance
Tolerant Comparison