Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect Multiplication Result [duplicate]

Tags:

java

I'm trying to understand why this multiplication results in an incorrect value:

long max = (60 * 24 * 60 * 60 * 1000);

This should = 5,184,000,000

But in my Java program it = 889,032,704

Any idea why this is the case?

like image 968
Kyle Anderson Avatar asked Apr 25 '14 17:04

Kyle Anderson


2 Answers

All of the values you're multiplying are ints so the result is an int which is cast to a long after overflow has already happened. Make one of the factors a long so that they are multiplied using 64b instructions

Try

long max = (60L * 24L * 60L * 60L * 1000L);

The L suffix specifies that the constant is a long value, not an int constant.

The language specification says

An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

and the section on multiplication says

The type of a multiplicative expression is the promoted type of its operands.

and type promotion of two ints leaves them as ints:

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int
like image 160
Mike Samuel Avatar answered Oct 10 '22 18:10

Mike Samuel


This is due to integer overflow. Your right-side product is represented as int (default type of all operands) and it obliviously can't hold your "long" value.

Just make the first factor of your product long, either as long constant 60L, as in Mike's answer, or cast it explicitly (long) 60 :

long max = (long) 60 * 24 * 60 * 60 * 1000;

and you will get what you expect.

like image 32
kiruwka Avatar answered Oct 10 '22 17:10

kiruwka