unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com>
To: Mark H Weaver <mhw@netris.org>
Cc: guile-devel <guile-devel@gnu.org>
Subject: Re: fix for expt bug
Date: Wed, 3 Nov 2010 22:11:55 +0530	[thread overview]
Message-ID: <AANLkTim851fpA0KhUZKXriy5VfUT=OvetCvAiPpsJGjr@mail.gmail.com> (raw)
In-Reply-To: <8762wexw1f.fsf@yeeloong.netris.org>

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

On Wed, Nov 3, 2010 at 9:02 PM, Mark H Weaver <mhw@netris.org> wrote:
> Hi Ramakrishnan,
>
> We're almost there, but you neglected one of the comments I made about
> your previous patch.

Sorry, I should pay more attention. :-(

Attaching the modified patch.
-- 
  Ramakrishnan

[-- Attachment #2: expt-fix.patch --]
[-- Type: text/x-patch, Size: 3102 bytes --]

From 6cca8a66a3daedb551f4f80170966d74b6143ba6 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com>
Date: Sun, 31 Oct 2010 23:22:52 +0530
Subject: [PATCH] Adding a case for `expt' when base is negative.

* libguile/numbers.c (scm_expt): Add a case when base is a
  negative number. Also fix the bug for the case when exponent
  is an inexact number.

* test-suite/tests/numbers.test ("expt"): Add tests for the cases
  when base is an integer and exponent is an inexact number, base
  is an integer and exponent is a rational number, base and
  exponent are both inexact and so on.
---
 libguile/numbers.c            |   18 +++++++++++++++++-
 test-suite/tests/numbers.test |   13 ++++++++++++-
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/libguile/numbers.c b/libguile/numbers.c
index fbc6cc8..f07f53d 100644
--- a/libguile/numbers.c
+++ b/libguile/numbers.c
@@ -5445,12 +5445,28 @@ SCM_DEFINE (scm_expt, "expt", 2, 0, 0,
 	    "Return @var{x} raised to the power of @var{y}.") 
 #define FUNC_NAME s_scm_expt
 {
-  if (scm_is_true (scm_exact_p (x)) && scm_is_integer (y))
+  if (scm_is_true (scm_exact_p (y)) && scm_is_integer (y))
     return scm_integer_expt (x, y);
   else if (scm_is_real (x) && scm_is_real (y) && scm_to_double (x) >= 0.0)
     {
       return scm_from_double (pow (scm_to_double (x), scm_to_double (y)));
     }
+  else if (scm_is_real (x) && scm_is_integer (y) && (scm_to_double (x) < 0))
+    {
+      double result;
+
+      /* If base is negative, expt needs to find (-x)^n = (-1^n) * (x^n).
+         We find x^n and then if n is odd, we also multiply the result
+         with -1. These changes apply only for cases where n is an
+         integer.
+      */
+      result = pow (-scm_to_double (x), scm_to_double (y));
+
+      if (scm_is_true (scm_odd_p (y)))
+        return scm_from_double (-result);
+      else
+        return scm_from_double (result);
+    }
   else
     return scm_exp (scm_product (scm_log (x), y));
 }
diff --git a/test-suite/tests/numbers.test b/test-suite/tests/numbers.test
index 3c3e14f..97c58c9 100644
--- a/test-suite/tests/numbers.test
+++ b/test-suite/tests/numbers.test
@@ -2892,7 +2892,18 @@
   (pass-if "(= 1 (expt 0 0))" (= 1 (expt 0 0)))
   (pass-if "(= 1 (expt 0 0.0))" (= 1 (expt 0 0.0)))
   (pass-if "(= 1 (expt 0.0 0))" (= 1 (expt 0.0 0)))
-  (pass-if "(= 1 (expt 0.0 0.0))" (= 1 (expt 0.0 0.0))))
+  (pass-if "(= 1 (expt 0.0 0.0))" (= 1 (expt 0.0 0.0)))
+  ;; tests for non zero values of base and exponent.
+  (pass-if (eqv-loosely? -2742638075.5 (expt -2742638075.5 1)))
+  (pass-if (eqv-loosely? (* -2742638075.5 -2742638075.5)
+                         (expt -2742638075.5 2)))
+  (pass-if (eqv-loosely? 4.0 (expt -2.0 2.0)))
+  (pass-if (eqv-loosely? -0.125 (expt -2.0 -3.0)))
+  (pass-if (eqv-loosely? -0.125 (expt -2 -3.0)))
+  (pass-if (eqv-loosely? 0.25 (expt 2.0 -2.0)))
+  (pass-if (eqv-loosely? +i (expt -1 0.5)))
+  (pass-if (eqv-loosely? +i (expt -1 1/2)))
+  (pass-if (eqv-loosely? 1.0000000000000002+1.7320508075688772i (expt -8 1/3))))
 
 ;;;
 ;;; asinh
-- 
1.7.2.3


  reply	other threads:[~2010-11-03 16:41 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-31 18:12 fix for expt bug Ramakrishnan Muthukrishnan
2010-11-01  0:03 ` Mark H Weaver
2010-11-01  4:03   ` Ramakrishnan Muthukrishnan
2010-11-01 18:23     ` Ramakrishnan Muthukrishnan
2010-11-02  4:55       ` Mark H Weaver
2010-11-02  5:22         ` Ramakrishnan Muthukrishnan
2010-11-02  5:36           ` Ramakrishnan Muthukrishnan
2010-11-02 19:19             ` Mark H Weaver
2010-11-02 23:00             ` Ludovic Courtès
2010-11-03  2:10             ` Mark H Weaver
2010-11-03  9:41               ` Ramakrishnan Muthukrishnan
2010-11-03 15:32                 ` Mark H Weaver
2010-11-03 16:41                   ` Ramakrishnan Muthukrishnan [this message]
2010-11-03 16:22                 ` Mark H Weaver
2010-11-03 17:53                   ` Ramakrishnan Muthukrishnan
2010-11-03 18:25                     ` Ramakrishnan Muthukrishnan
2010-11-03 23:27                   ` Ludovic Courtès
2010-11-04 17:27                     ` Mark H Weaver
2010-11-08 21:08                       ` Ludovic Courtès
2010-11-20 21:49                         ` Andy Wingo
2010-11-21 22:34                           ` Ludovic Courtès
2010-11-05  2:18                     ` [PATCH] Fix bugs in expt and integer-expt Mark H Weaver
2010-11-10 22:01                       ` Ludovic Courtès
2011-01-20 22:47                       ` Andy Wingo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTim851fpA0KhUZKXriy5VfUT=OvetCvAiPpsJGjr@mail.gmail.com' \
    --to=vu3rdd@gmail.com \
    --cc=guile-devel@gnu.org \
    --cc=mhw@netris.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).