Tolerance

From J Wiki
Jump to navigation Jump to search


<=   =>

This section is a bit advanced and understanding it is not critical. If it makes sense, great. If not, don't worry about it, and just move on to the next section.

For some kinds of work with floating point numbers, this section is important, along with a more detailed understanding of how numbers are stored and manipulated by the hardware. For most work, however, this section can be ignored.

At the end of the last section we saw that computer cannot always represent numbers exactly. So how do we know if a number is equal to another number if they can't be exactly represented? the solution to this is the concept of tolerant comparison. That is, numbers do not have to be exactly identical to be considered equal.

Let's experiment to get an idea for how tolerant the comparison is by gradually taking the value further away from 100. The input line recall shortcut with Shift+Ctrl+up arrow is very useful for playing with things like this.

   100 = 100
1
   100 = 99.999999999999986
1
   100 = 99.99999999999998
1
   100 = 99.9999999999999
1
   100 = 99.999999999999
1
   100 = 99.99999999999
0


In the last example you crossed the line and the value is far enough away from 100 that it is no longer considered to be equal. Let's look at another example.

   a =. 23
   b =. a - 1e_12
   c =. a - 1e_11
   a
23
   a-b
9.9831254374294076115e_13
   a=b
1
   a-c
1.0000889005823410116e_11
   a=c
0


The values of a and b are close enough to be considered equal. The values of a and c are not close enough to be considered equal. Close enough refers to the difference between the two numbers. In both cases the difference is small, but b is closer than c to a. Reading the NuVoc definition for = you will see that the dividing line between close enough and not close enough is determined by the result of multiplying the larger of the numbers times the default tolerance value of 2^_44. That is, close enough is relative to the size of the numbers.

   tolerance =. a * 2 ^ _44
   tolerance
1.307398633798584342e_12


Check both differences against this tolerance:

   (a - b , c) <: tolerance    NB. Remember, this is equivalent to (a - (b,c)) due to right to left precedence.
1 0

The difference between a and b is less or equal to the tolerance, whereas the difference between a and c is not.

<=   =>

Primer Index               Hover to reveal titles   -   Click to access   -   Current page is highlighted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
45 46 47 48
50 51 52 53 54 55 56 57
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
97 98 99 100 101 102 103 104 105 106