| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* ecnum_comp.c - comparisons on numbers |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
/* |
| 16 |
* In Common Lisp, comparisons between floats and integers are performed |
| 17 |
* via an intermediate rationalization of the floating point number. In C, |
| 18 |
* on the other hand, the comparison is performed by converting the integer |
| 19 |
* into a floating point number. However, if the double type is too small |
| 20 |
* this may lead to a loss of precision and two numbers being told equal |
| 21 |
* when, by Common Lisp standards, would not. |
| 22 |
*/ |
| 23 |
static int |
| 24 |
double_fix_compare(cl_fixnum n, double d) |
| 25 |
{ |
| 26 |
if ((double)n < d) { |
| 27 |
return -1; |
| 28 |
} else if ((double)n > d) { |
| 29 |
return +1; |
| 30 |
} else if (sizeof(double) > sizeof(cl_fixnum)) { |
| 31 |
return 0; |
| 32 |
} else { |
| 33 |
/* When we reach here, the double type has no |
| 34 |
* significant decimal part. However, as explained |
| 35 |
* above, the double type is too small and integers |
| 36 |
* may coerce to the same double number giving a false |
| 37 |
* positive. Hence we perform the comparison in |
| 38 |
* integer space. */ |
| 39 |
cl_fixnum m = d; |
| 40 |
if (n == m) { |
| 41 |
return 0; |
| 42 |
} else if (n > m) { |
| 43 |
return +1; |
| 44 |
} else { |
| 45 |
return -1; |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
static int |
| 51 |
long_double_fix_compare(cl_fixnum n, long double d) |
| 52 |
{ |
| 53 |
if ((long double)n < d) { |
| 54 |
return -1; |
| 55 |
} else if ((long double)n > d) { |
| 56 |
return +1; |
| 57 |
} else if (sizeof(long double) > sizeof(cl_fixnum)) { |
| 58 |
return 0; |
| 59 |
} else { |
| 60 |
cl_fixnum m = d; |
| 61 |
if (n == m) { |
| 62 |
return 0; |
| 63 |
} else if (n > m) { |
| 64 |
return +1; |
| 65 |
} else { |
| 66 |
return -1; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|