The thing is that the compiler evaluates the right side of the
assignment instruction as an operation between integer constants, and
thus produces the overflow, which is aftterwards passed to the double
variable.

In C you would have the same mistake and thus is always a good
practice do represent numbers in a way that is consistent with the
variable they will be assigned to.

Check the use of the 'd' and 'f' clauses, as in this example:

http://www.concentric.net/~Ttwang/tech/javafloat.htm

Best regards from Portugal,

Nuno Garcia

2010/1/2 MARIO CAMPAZ <[email protected]>:
> Exactly, overflow int multiplication. Even when you are using a double
> variable.
>
> On Sat, Jan 2, 2010 at 1:57 PM, Rory O'Conor <[email protected]>
> wrote:
>>
>> sorted
>>
>> public class MathsBug {
>>
>>    public static void main(String[] args) {
>>        double b = 0.0;
>>        b = 1000 * 1000 * 1000;
>>        System.out.println("1000 * 1000 * 1000 = "+b);
>>        b = b * b;
>>        System.out.println("above squared = "+b);
>>        b =  1000 * 1000 * 1000 * 1000;
>>        System.out.println("1000 * 1000 * 1000 * 1000 = "+b);
>>        System.out.println("Surely some mistake!");
>>        b =  1000.0 * 1000 * 1000 * 1000;
>>        System.out.println("1000.0 * 1000 * 1000 * 1000 = "+b);
>>        b =  (double) 1000 * 1000 * 1000 * 1000;
>>        System.out.println("(double) 1000 * 1000 * 1000 * 1000 = "+b);
>>    }
>> }
>>
>> --
>> To post to this group, send email to
>> [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>
> --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to