[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 ! 

You got a problem with 0's ??? :>

0.3 cannot be represented exactly in either float or double.  When
the float gets extended to double precision it is simply adding 0 bits 
onto the end of the mantissa -- by this time Java has no idea what 
double precision  number you wanted, so what else can it do?

A more useful test program would be something like the following:
The mask extracts the mantissa from each `bit' representation of the
float or double. the lines with ffi take the float's mantissa (23 bits
plus
implied 1) and slides it over to where it would be in a double (52 bits
plus implied); ie shift it over 29 places.
Sure enough (on my machine (an SGI)) ffi is the same as dfi.
Of course, both are different from di which comes from computing the
double precision approximation from the outset.

public class number {
        public static void main(String args[]) {

        float a = 3.3f;
        double b = 3.3d; 
        double aa=a;

        int fi  =Float.floatToIntBits(a) & 0x007fffff;
        long fl = fi;
        long ffi = fl << 29;
        long dfi=Double.doubleToLongBits(aa) & 0x000fffffffffffffL;
        long di =Double.doubleToLongBits(b) & 0x000fffffffffffffL;

        System.out.println("flt (ToIntBits)       = "+fi);
        System.out.println("flt (ToLongBits)      = "+dfi);
        System.out.println("dbl (ToLongBits)      = "+di);
        System.out.println("flt (ToIntBits) << 29 = "+ffi);
        }
        
}

--
[EMAIL PROTECTED]
http://math.nist.gov/~BMiller/


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to