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
S/MIME Cryptographic Signature