From de59c1f7c704a62a343bdd4937d70f6e36094785 Mon Sep 17 00:00:00 2001 From: Oldes Date: Tue, 23 May 2023 16:40:04 +0200 Subject: [PATCH] FIX: finer decimal `equiv?` comparison than `equal?` resolves: https://github.com/Oldes/Rebol-issues/issues/1134 --- src/core/t-decimal.c | 4 ++-- src/tests/units/compare-test.r3 | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/core/t-decimal.c b/src/core/t-decimal.c index 2c8a594615..80e756428f 100644 --- a/src/core/t-decimal.c +++ b/src/core/t-decimal.c @@ -178,8 +178,8 @@ REBOOL almost_equal(REBDEC a, REBDEC b, REBCNT max_diff) { ***********************************************************************/ { if (mode >= 0) { - if (mode <= 1) return almost_equal(VAL_DECIMAL(a), VAL_DECIMAL(b), 21); //O: there was 10, but 21 is the minimum to have: (100% // 3% = 1%) == true - if (mode == 2) return almost_equal(VAL_DECIMAL(a), VAL_DECIMAL(b), 0); + if (mode == 0) return almost_equal(VAL_DECIMAL(a), VAL_DECIMAL(b), 21); //O: there was 10, but 21 is the minimum to have: (100% // 3% = 1%) == true + if (mode == 1) return almost_equal(VAL_DECIMAL(a), VAL_DECIMAL(b), 0); return VAL_INT64(a) == VAL_INT64(b); // bits are identical } if (mode == -1) return VAL_DECIMAL(a) >= VAL_DECIMAL(b); diff --git a/src/tests/units/compare-test.r3 b/src/tests/units/compare-test.r3 index 702a789b6b..c84fd1b041 100644 --- a/src/tests/units/compare-test.r3 +++ b/src/tests/units/compare-test.r3 @@ -111,6 +111,15 @@ Rebol [ --test-- "decimal! invalid compare" --assert all [error? e: try [90.0 < "a" ] e/id = 'invalid-compare] --assert all [error? e: try [90.0 < 1x1 ] e/id = 'invalid-compare] + + --test-- "decimal! equal?/equiv?/same?" + ;@@ https://github.com/Oldes/Rebol-issues/issues/1134 + --assert equal? to decimal! #{3FD3333333333333} to decimal! #{3FD3333333333333} + --assert equiv? to decimal! #{3FD3333333333333} to decimal! #{3FD3333333333333} + --assert same? to decimal! #{3FD3333333333333} to decimal! #{3FD3333333333333} + --assert equal? to decimal! #{3FD3333333333333} to decimal! #{3FD3333333333334} + --assert not equiv? to decimal! #{3FD3333333333333} to decimal! #{3FD3333333333334} + --assert not same? to decimal! #{3FD3333333333333} to decimal! #{3FD3333333333334} ===end-group===