# Comparatives

## An Implementation of J

Comparisons between finite numbers are tolerant, as defined in Bernecky [1977]:

`   x = y   if   (|x-y) <:!.0 ct * (|x)>.(|y) `

(<:!.0 means exact less than or equal.) That is, x and y are tolerantly equal if the smaller is on or within the circle centered at the larger, having radius ct times the magnitude of the larger. ct, comparison tolerance, is a real number between 0 and 2_34 with a default value of 2_44; a non-default tolerance may be specified using the fit conjunction (!.). Tolerant relations can be modelled as follows:

```        ct      =:      2^_44   comparison tolerance
teq     =:      |@- <:!.0 ct&*@>.&|     equal
tne     =:      -.@teq  not equal
tlt     =:      < !.0 *. tne    less than
tgt     =:      > !.0 *. tne    greater than
tle     =:      <:!.0 +. teq    less than or equal to
tge     =:      >:!.0 +. teq    greater than or equal to
tfloor  =:      <.!.0@(0.5&+) ([ - tgt) ]       floor
tceil   =:      <.!.0@(0.5&+) ([ + tlt) ]       ceiling
dsignum =:      ct&<@| * 0&< - 0&>      signum (real)
jsignum =:      ct&<@| * (%|)   signum (complex)```

Additionally, some comparisons internal to the system are fuzzy. Fuzzy comparisons are like tolerant comparisons, but depend on the parameter fuzz, having fixed value 2^_44. Such comparisons are used in certain domain tests; for example, (2 3+1e_14)\$'abc' is valid but (2 3+1e_12)\$'abc' is not. Fuzzy comparisons can be modelled as follows:

```        fuzz    =:      2^_44
int     =:      (-2^31)&<: *. <&(2^31)
real    =:      {.@+.
feq     =:      |@- <:!.0 fuzz&*@>.&|
freal   =:      >:!.0 / @: ((fuzz,1)&*) @: | @: +.
BfromD  =:      ]`(1&=) @. (feq 1&=)
IfromD  =:      ]`<. @. (int *. (feq <.))
DfromZ  =:      ]`real @. (feq real)```

The utility int tests for membership in the interval -231 to _1+231 inclusive. real produces the real part of a complex number. feq is 1 if its arguments are equal within fuzz; freal is 1 if its complex argument is within fuzz of real. BfromD, IfromD, and DfromZ convert between types: boolean from real ("double"), integer from real, and real from complex.