That is actually documented I believe. It's not limited to jdk1.2. I
don't know why it can't be just 0.3000000000000 but it is. This is why
the language spec says not to compare floats and doubles without casting,
you will always get negative results even though you think you should have
a positive one.
So to compare 0.3d with 0.3f you would do:
double d = 0.3;
float f = 0.3f;
if ((float)d == f) { do something };
You will lose precicion on your double variable though, so make sure you
don't need it before you do it.
Back to why you can't store 0.3 as a double. I'm guess this has more to
do with the limitations of binary representation of floating point
numbers. It's been a long time since my intro to CS courses when I
learned all this, anybody have anymore info on this?
On Fri, 24 Sep 1999 [EMAIL PROTECTED] wrote:
> Dear all,
>
> If I try to put a float into a double variable the last bits of the
> precision are total garbage ! If I try to put the float 0.3 into the a
> double the result is 0.30000001192092896 !!! This could be a 'feature'
> but it renders java (or at least jdk1.2) completely useless for numerics
> !
>
> Has anyone seen this behaviour
>
> below is the small program I used to test it, we first discovered this
> in a jpython program so hence the cross post to the jpython group.
>
> public class Main {
> public static void main(String args[]) {
>
> float a = 3.3f;
> double b = 3.3d;
>
> System.out.println("flt (ToIntBits) = "+Float.floatToIntBits(a));
> System.out.println("flt (ToLongBits) = "+Double.doubleToLongBits(a));
> System.out.println("dbl (ToLongBits) = "+Double.doubleToLongBits(b));
>
> }
>
> }
>
> For jpython this means that it is not possible to return a float array
> from java to jpython session, since it is always converted to double !!
>
> I modified the PyFloat.py file in such a way that the contructor for the
> PyFloat(float) uses the Float.toString() and then the
> Double.parse(string) to put the proper value in the private attribute
> value; It was also
>
> It is also necesary to modify the Py.java2py in the Py.py file, this
> function transforms a java array into a Python array. It seems that this
> code make no difference between a double and a float array, it always
> uses the double contructor from the PyFloat class so I modified that. I
> agree it is not very nice but at least it works.
>
> public PyFloat(float v) {
> super(__class__);
>
> // Nasty !!!!
> Float _tmp = new Float(v);
> value = Double.parseDouble(_tmp.toString());
>
> //this((double)v); ** Original code
> }
>
> ---- snip function top ----
> if (o instanceof Number) {
> if (o instanceof Double ) {
> return new PyFloat(((Number)o).doubleValue());
> }
> else if(o instanceof Float) {
> return new PyFloat(((Float)o).floatValue());
> }
> ---- snip function end ----
>
>
> Last question, It seems that if I create and later print an array (say
> something like : array([0.3, 0.4], 'f')) it first allocates PyFloat
> variables for 0.3 and 0.4, that it allocates the array BUT then it,
> when I want to print it, it again allocates two PyFloats to print it (in
> the __repr__() fucntion in PyArray, or actually in the get(int i) method
> which calls the Java method Array::get(Object, int)). Q: Why does it
> allocate twice ?????
>
> Kind regards,
> Niels Hilbrink
>
> --
> Stehlin Merazzi Research sa. tel : +41 32 345 2123 - direct
> http://www.smr.ch +41 79 607 1014 - mobile
> Rue Dufour 109b +41 32 345 2120 - fax
> CH-2500 Bienne 4, Switzerland
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]