unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#69725: Incorrect comapration between exact and inexact numbers
@ 2024-03-11  7:46 Maciej
  2024-03-18 16:17 ` lloda
  0 siblings, 1 reply; 3+ messages in thread
From: Maciej @ 2024-03-11  7:46 UTC (permalink / raw)
  To: 69725


description:

script(named main.scm):
(use-modules (srfi srfi-11))

(define* (newton-sqrt x #:optional (tolerance 0.001) (guess 1))
  (if (< (abs (- x (* guess guess)))
	 tolerance)
      (values guess (abs (- x (* guess guess))) tolerance)
      (newton-sqrt x tolerance (/ (+ guess (/ x guess)) 2))))

(let-values ([(root diff tolerance)
	      (newton-sqrt 1000)])
  (display (exact->inexact diff)) ;; 1.217168231948292
  (newline)
  (display tolerance) ;; 0.001
  (newline)
  ;; bug here 1.217 in exact form apparently is smaller than 0.001
  (display (< diff tolerance)) ;; #t
  (newline)
  (display (< diff (inexact->exact tolerance))) ;; #f
  (newline)
  (display (< (exact->inexact diff) tolerance)) ;; #f
  (newline))

guile version:
guile --version
guile (GNU Guile) 3.0.9

machine-type:
./config.guess
x86_64-pc-linux-gnu

system package:
guix package -V guile
guix package (GNU Guix) f18d13b5f89c161c6ee88d15ecdaa8d973ee4503

additional system info:
guix channels.scm
(list (channel
        (name 'guix)
        (url "https://git.savannah.gnu.org/git/guix.git")
        (branch "master")
        (commit
          "aeb494322ca9dec4a4d66a7d063239c8536bd538")
        (introduction
          (make-channel-introduction
            "9edb3f66fd807b096b48283debdcddccfea34bad"
            (openpgp-fingerprint
              "BBB0 2DDF 2CEA F6A8 0D1D  E643 A2A0 6DF2 A33A 54FA")))))

what i ran to run the script:
guix time-machine -C channels.scm -- shell --pure guile -- ./main.scm
1.217168231948292
0.001
#t
#f
#f





^ permalink raw reply	[flat|nested] 3+ messages in thread

* bug#69725: Incorrect comapration between exact and inexact numbers
  2024-03-11  7:46 bug#69725: Incorrect comapration between exact and inexact numbers Maciej
@ 2024-03-18 16:17 ` lloda
  2024-03-19 17:29   ` lloda
  0 siblings, 1 reply; 3+ messages in thread
From: lloda @ 2024-03-18 16:17 UTC (permalink / raw)
  To: Maciej; +Cc: 69725

[-- Attachment #1: Type: text/plain, Size: 61 bytes --]


Thanks for the report! Patch attached.

regards

  Daniel



[-- Attachment #2: 0001-Add-missing-branch-in-scm_is_less_than.patch --]
[-- Type: application/octet-stream, Size: 3125 bytes --]

From 189d3acdbebde503643da3817dae3fc0dd14db7d Mon Sep 17 00:00:00 2001
From: Daniel Llorens <lloda@sarc.name>
Date: Mon, 18 Mar 2024 13:53:17 +0100
Subject: [PATCH] Add missing branch in scm_is_less_than()

Fixes https://debbugs.gnu.org/69725.

* libguile/numbers.c (scm_is_less_than): Add branch for (< fraction real).
* test-suite/tests/numbers.test (<): New test.
---
 libguile/numbers.c            | 17 ++++++++++++++---
 test-suite/tests/numbers.test | 12 ++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/libguile/numbers.c b/libguile/numbers.c
index 30a826f13..ae2aa7766 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -4559,6 +4559,7 @@ scm_is_less_than (SCM x, SCM y)
 	return SCM_REAL_VALUE (x) < SCM_REAL_VALUE (y);
       if (!SCM_FRACTIONP (y))
         abort ();
+      /* REALP x FRACTIONP y, see symmetric case below  */
       if (isnan (SCM_REAL_VALUE (x)))
         return 0;
       if (isinf (SCM_REAL_VALUE (x)))
@@ -4569,9 +4570,19 @@ scm_is_less_than (SCM x, SCM y)
   if (!SCM_FRACTIONP (x))
     abort ();
 
-  /* "a/b < " becomes "a < y*b" */
-  return scm_is_less_than (SCM_FRACTION_NUMERATOR (x),
-                           scm_product (y, SCM_FRACTION_DENOMINATOR (x)));
+  if (SCM_REALP (y))
+    {
+      /* FRACTIONP x REALP y, see symmetric case above */
+      if (isnan (SCM_REAL_VALUE (y)))
+        return 0;
+      if (isinf (SCM_REAL_VALUE (y)))
+        return 0.0 < SCM_REAL_VALUE (y);
+      return scm_is_less_than (x, scm_inexact_to_exact (y));
+    }
+  else
+    /* "a/b < y" becomes "a < y*b" */
+    return scm_is_less_than (SCM_FRACTION_NUMERATOR (x),
+                             scm_product (y, SCM_FRACTION_DENOMINATOR (x)));
 }
 
 static int
diff --git a/test-suite/tests/numbers.test b/test-suite/tests/numbers.test
index 0b80c0356..cf54f0369 100644
--- a/test-suite/tests/numbers.test
+++ b/test-suite/tests/numbers.test
@@ -266,6 +266,18 @@
     (pass-if (not (exact? -inf.0)))
     (pass-if (not (exact? +nan.0)))))
 
+;;;
+;;; <
+;;;
+
+(with-test-prefix "bug #69725 for (< fraction float)"
+
+  (let ((diff 879797032764096269186597679847100265884832692381817056132294700511440987615745549195576394246327599597660137250885282118923685378363495296576250891618672897887455888641129758660040580125471657527501887655846743402333407472717488972378318312674085710204045036645651022493812366478520603176717119923372376049351273979050815421434638458049196631010462508376132028146644267658632127872001/722822868417972349291869843452690786425750906620439512189327802121058061301404726887882493296646015935903482610570303382076049858386583955490031061870416075735232078523284307346352926571709615036536557063178291059619363366958123014280624087029240670600236519573317406921775499062373875623452948086789385672089163532498692518546438923870180903091519351584154949372291968970659168272384)) ; 1.217168231948292
+  (pass-if "< fraction float (I)"
+    (not (< diff 1.0)))
+  (pass-if "< fraction float (II)"
+    (< diff 2.0))))
+
 ;;;
 ;;; exp
 ;;;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* bug#69725: Incorrect comapration between exact and inexact numbers
  2024-03-18 16:17 ` lloda
@ 2024-03-19 17:29   ` lloda
  0 siblings, 0 replies; 3+ messages in thread
From: lloda @ 2024-03-19 17:29 UTC (permalink / raw)
  To: Maciej; +Cc: 69725-done


Patch committed to 54c4753dd3f7506bee2778b36d7263b613ffd579, with a couple extra tests.

Thanks

	Daniel






^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-03-19 17:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-11  7:46 bug#69725: Incorrect comapration between exact and inexact numbers Maciej
2024-03-18 16:17 ` lloda
2024-03-19 17:29   ` lloda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).