make mixed inexact/exact ordering consistent, converting to exact for fixnums and ratios instead of just bignums (issue #812)

This commit is contained in:
Alex Shinn 2022-02-12 07:50:58 +09:00
parent eb6a2eeb78
commit 82d61b3d8e
2 changed files with 8 additions and 7 deletions

View file

@ -1886,12 +1886,13 @@ sexp sexp_compare (sexp ctx, sexp a, sexp b) {
r = sexp_make_fixnum(sexp_unbox_fixnum(a) - sexp_unbox_fixnum(b));
break;
case SEXP_NUM_FIX_FLO:
f = sexp_fixnum_to_double(a);
g = sexp_flonum_value(b);
if (isnan(g))
if (isinf(sexp_flonum_value(b))) {
r = sexp_flonum_value(b) > 0 ? SEXP_NEG_ONE : SEXP_ONE;
} else if (isnan(sexp_flonum_value(b))) {
r = sexp_xtype_exception(ctx, NULL, "can't compare NaN", b);
else
r = sexp_make_fixnum(f < g ? -1 : f == g ? 0 : 1);
} else {
r = sexp_compare(ctx, a, tmp=sexp_inexact_to_exact(ctx, NULL, 1, b));
}
break;
case SEXP_NUM_FIX_BIG:
if ((sexp_bignum_hi(b) > 1) ||
@ -1933,8 +1934,7 @@ sexp sexp_compare (sexp ctx, sexp a, sexp b) {
} else if (isnan(f)) {
r = sexp_xtype_exception(ctx, NULL, "can't compare NaN", a);
} else {
g = sexp_ratio_to_double(ctx, b);
r = sexp_make_fixnum(f < g ? -1 : f == g ? 0 : 1);
r = sexp_compare(ctx, tmp=sexp_inexact_to_exact(ctx, NULL, 1, a), b);
}
break;
case SEXP_NUM_FIX_RAT:

View file

@ -798,6 +798,7 @@
(test #f (< +nan.0 0.0))
(test #f (> +nan.0 0.0))
(test '(#t #f) (list (<= 1 1 2) (<= 2 1 3)))
(test #f (= 9007199254740992.0 9007199254740993))
;; From R7RS 6.2.6 Numerical operations:
;;