MS wrote:

> Arne Vajhøj emailed this:

>> MS wrote:

>>> I haven't programmed in Java for a while and just wrote a program to

>>> calc the odds of winning the British national lottery. BUT it is

>>> giving me the wrong result !!

>>>

>>> Here's the relevant bit:

>>>

>>> public void DoCalc()

>>> {

>> > float winOdds = 49 * 48 * 47 * 46 * 45 * 44;

>>

>> I am pretty sure you make an integer overflow here.

>>

>> try:

>>

>> float winOdds = 49.0 * 48.0 * 47.0 * 46.0 * 45.0 * 44.0;

>

> Many thanks. What you suggested gave a 'possible loss of precision'

> error, when I swapped the floats to doubles I got the correct result.

>

> Strange oversight by the Java designers that Java doesn't assume the .0

> at the end of any real number.
Strange oversight by the Java programmers who can't

distinguish between `int' and `double' constants ...

To put it another way: The eventual "target" of an

expression does not influence the way the expression is

calculated. You wrote `float winOdds =' and apparently

expected the right-hand side to somehow magically understand

that it should be evaluated in `float' arithmetic. But the

original is an expression involving `int' values multiplied

together, so it is evaluated according to the rules of `int'.

After the evaluation, the result is converted to `float' for

the assignment -- but by then it's too late, the overflow has

already occurred and the damage has been done.

Perhaps the commonest situation where this misunderstanding

crops up is

double fahrenheit = 212;

double celsius = 5 / 9 * (fahrenheit - 32);

As a test of your newfound understanding of how expressions

are evaluated, can you say what's wrong with this formula?

For extra credit, can you explain why

double celsius = (fahrenheit - 32) * 5 / 9;

works as (presumably) intended?

--

Eric Sosman

http://www.velocityreviews.com/forums/(E-Mail Removed)lid