classpath
[Top][All Lists]
Advanced

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

Re: floats ??????


From: Eric Blake
Subject: Re: floats ??????
Date: Sat, 14 Sep 2002 14:54:06 -0600
User-agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0

Giannis Georgalis wrote:
Now, that's strange because the implementation of the java.lang.Float
is correct!!!  (and so is java.lang.Math). I'm assuming that there is
a serious problem in the representation of the java floats (jfloat).
I'm aware that jfloat is typedef'ed (C/C++) float and I'm absolutely
sure that (C/C++) floats work *fine* on my machine (I've extensively
tested them). I'll just provide one of the cases that java floats fail
to work properly (there are quite some cases, but I think the root of
evil is the same).

take as an example the code:

float a=-1.0f,b=-2.0f;
if(a > b)x
  System.out.println("correct!!");
else
  System.out.println("FAIL !!!");

It outputs "FAIL !!!", but if I substitute the line:
"float a=-1.0f,b=-2.0f;" with "double a=-1.0f,b=-2.0f;"
it outputs "correct!!".

Is this a GNU classpath problem, a jikes problem or a kissme problem ?
I made some changes to java.lang.Float, so that it passes the tests,
but the solution is a very nasty hack so it cannot be taken seriously,
as the problem resides on the nature of java primitive floats and not the class' implementation.

--------------------start patch-----------------------

*** Float.java.~1.25.~  Mon Feb 25 22:02:58 2002
--- Float.java  Fri Sep 13 16:00:47 2002
***************
*** 84,91 ****
/**
     * All IEEE 754 values of NaN have the same value in Java.
     */
!   public static final float NaN = 0.0f / 0.0f;
/**
     * The primitive type <code>float</code> is represented by this
--- 84,94 ----
/**
     * All IEEE 754 values of NaN have the same value in Java.
+    * NaN value is the same with the return value of
+    * intBitsToFloat(0x7fc00000).
+    * @see intBitsToFloat(int bits)
     */
!   public static final float NaN = intBitsToFloat(0x7fc00000);
/**
     * The primitive type <code>float</code> is represented by this

Do not make this patch. Float.NaN must be a compile time constant, and your patch breaks that.


***************
*** 185,191 ****
     */
    public static String toString(float f)
    {
!     return Double.toString(f, true);
    }
/**
--- 188,194 ----
     */
    public static String toString(float f)
    {
!     return Double.toString((double)f, true);
    }
/**

This patch is unnecessary. The compiler automatically does the widening cast for you.


***************
*** 270,276 ****
    {
      // This works since NaN != NaN is the only reflexive inequality
      // comparison which returns true.
!     return v != v;
    }
/**
--- 273,282 ----
    {
      // This works since NaN != NaN is the only reflexive inequality
      // comparison which returns true.
!     // FIXME: (very nasty hack) see also Float.NaN.
!     // I've added the "|| v == NaN", which makes isNaN work correctly.
!     // I have no idea why the initial version doesn't work...
!     return (v != v) || (v == NaN);
    }
/**

Again, this patch is not needed. v == NaN will always return false (even when v is NaN), so it is a pointless addition - the logic was already correct as it was.

It is probably an implementation bug in the VM, like you point out.


--------------------end patch-----------------------

Additionally in my effort to figure out what is going on, I have
altered a jni file and prevented it from calling a java method. I
think this minor change makes it more efficient. ...and it is portable
(tested on solaris sparc gcc, solaris intel gcc/cc, sunos gcc,
gnu/linux intel gcc, gnu/linux alpha gcc). If you have a different
opinion, please tell me the reason, so I can adapt to the "philosophy"
of the project.

I'm not as sure about commenting about this patch, and will leave others to comment. But you are right that once you are in native code, it is more efficient to stay native than to call back to Java.

--
This signature intentionally left boring.

Eric Blake             address@hidden
  BYU student, free software programmer






reply via email to

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