Re: Blow me away, (long) does not truncate.

From: Roedy Green (
Date: 06/01/04

Date: Tue, 01 Jun 2004 16:38:56 GMT

On Tue, 01 Jun 2004 09:41:47 +0100, Yu SONG <> wrote or
quoted :

>The rules are here:

The rules are ambiguous.

They say they "round toward 0" which could mean ether truncation
toward 0 or rounding toward 0.

In their example they say:

System.out.print("cast to int rounds toward 0: ");
d = 12345.6;
System.out.println((int)d + " " + (int)(-d));

produces the output:

cast to int rounds toward 0: 12345 -12345

I ran the test and it indeed truncates.

Which suggests they mean truncation.

Yet the example I ran shows it sometime does rounding.

The mystery is explained at

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming. 
See for The Java Glossary.

Relevant Pages

  • Re: Rounding errors
    ... It maintains the average of the infinite precision original ... With enough digits the average is close enough to ... Rounding maintains this with an average of 0.500. ... >It only 'pushes it upwards' with respect to how much truncation ...
  • Re: Rounding errors
    ... Rounding maintains this with an average of 0.500. ... It only 'pushes it upwards' with respect to how much truncation ... You continue to say 'rounding error' when applied to an average. ... If the set of infinite precision reals adds up to the same as the set ...
  • Re: Rounding errors
    ... How does the rounding operation know what truncation came ... I am suggesting that you may get a different expectation 'with ... > by FACTS, ...
  • Re: Rounding errors
    ... >> Common sense says that if exactly half round up and half 'round down' ... You then seemed to conclude that the code that did the rounding was ... >And the amount that it is 'out' does not depend on how many digits are ... >In other words it is dependent on where the truncation occurs. ...
  • RE: [OT] Rounding v Truncation, was: Re: Platform Support vs.
    ... Subject: Rounding v Truncation, ... > If you don't exploit EXPLICIT conversion then you had better know the ... If you have received the email in error, please notify TransGrid ...