help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Negative zeros?


From: John W. Eaton
Subject: Re: Negative zeros?
Date: Wed, 14 Sep 2005 17:46:06 -0400

On 14-Sep-2005, Tom Holroyd wrote:

| It should be quite clear, however, that this has nothing to do with 
| floating point representation (or Octave, for that matter).  But I 
| like the idea of thinking of -0 as the limit from the left.  As for 
| Octave:
| 
| octave:1> sqrt(-.1i-1)
| ans = 0.049938 - 1.001246i
| octave:2> sqrt(-.00001i-1)
| ans = 5.0000e-06 - 1.0000e+00i
| octave:3> sqrt(-0*i-1)
| ans = 0 + 1i
| 
| The problem, of course, is the subtraction,
| 
| octave:4> -0
| ans = -0
| octave:5> -0*i
| ans = -0
| octave:6> -0*i - 1
| ans = -1
| 
| since there aren't two forms of -1 (from the left and from the right) 
| in floating point.

Note that -0i is parsed as

  UMINUS IMAG_NUM

with the value of IMAG_NUM == 0i, which is normally converted to 0.
So the result is -0 (real) instead of -0 (imaginary).  But even if we
"fix" Octave to avoid the conversion to real, we don't have a pure
imaginary data type, so we would get the UMINUS applied to the complex
value (0, 0), and end up with (-0, -0) instead of -0 (imaginary).

The lack of a pure imaginary data type causes other trouble too.  For
example, Inf * i ==> NaN + Infi instead of 0+Infi.

I don't see a quick fix for these problems, but the following patch
does allow you to write

  0*(-i)

to produce (0, -0).

jwe


src/ChangeLog:

2005-09-14  John W. Eaton  <address@hidden>

        * ov-complex.cc (octave_complex::try_narrowing_conversion):
        Don't drop -0i.
        * ov-cx-mat.cc (octave_complex_matrix::try_narrowing_conversion):
        Likewise.


Index: src/ov-complex.cc
===================================================================
RCS file: /cvs/octave/src/ov-complex.cc,v
retrieving revision 1.29
diff -u -r1.29 ov-complex.cc
--- src/ov-complex.cc   26 Apr 2005 19:24:33 -0000      1.29
+++ src/ov-complex.cc   14 Sep 2005 21:44:02 -0000
@@ -56,7 +56,9 @@
 {
   octave_value *retval = 0;
 
-  if (imag (scalar) == 0.0)
+  double im = std::imag (scalar);
+
+  if (im == 0.0 && ! lo_ieee_signbit (im))
     retval = new octave_scalar (real (scalar));
 
   return retval;
Index: src/ov-cx-mat.cc
===================================================================
RCS file: /cvs/octave/src/ov-cx-mat.cc,v
retrieving revision 1.46
diff -u -r1.46 ov-cx-mat.cc
--- src/ov-cx-mat.cc    26 Apr 2005 19:24:33 -0000      1.46
+++ src/ov-cx-mat.cc    14 Sep 2005 21:44:02 -0000
@@ -74,7 +74,9 @@
        {
          Complex c = matrix (0, 0);
 
-         if (std::imag (c) == 0.0)
+         double im = std::imag (c);
+
+         if (im == 0.0 && ! lo_ieee_signbit (im))
            retval = new octave_scalar (std::real (c));
          else
            retval = new octave_complex (c);



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]