Re: frustrating arithmetic




"Mark Haase" <mehaase@xxxxxxxxx> wrote in message
news:mehaase-5CFB9B.17425127122005@xxxxxxxxxxxxxxxxxxxxxxxxxxx
> In article <_Fgsf.4320$l87.243555@xxxxxxxxxxxxxxxxxxxxx>,
> "Rhino" <no.offline.contact.please@xxxxxxxxxx> wrote:
>
>> But Java actually sees 5/50 as an _integer_ expression so it gives you 0
>> because 0 is the integer that is closest to the value of 5/50. That
>> probably
>> seems odd because you've defined prevalence as being a double so you
>> expect
>> the result to be 0.1 but this is the way Java works by default.
>
> Its actually not the integer closest to 5/50, its the integer result of
> division. There's a difference, because if you divided 9 / 10, you would
> still get zero even though 1 is closer.
>
> Integer division is like the division you did in 5th grade: 50/8 = 6 +
> 2R, where 6 is the integer quotient and 2R means 2 left over (in other
> words, 2/8 or .25). So you can think of integer division as performing
> proper division and throwing away everything after the decimal point --
> even if you cast the result to double afterwards.
>
> Example:
>
> marks:~ mehaase$ cat Test.java
> class Test {
> public static void main(String args[]) {
> double test = 9/10;
> System.out.println("test is " + test);
> }
> }
> marks:~ mehaase$ javac Test.java
> marks:~ mehaase$ java Test
> test is 0.0
>
> There is a Math.round() operation if you want to round 9/10 up to 1.
>
I stand corrected.

Rhino


.



Relevant Pages

  • Re: Null Pointer Exception
    ... > Thank you for the response; ... I can't seem to get away from the Null Pointer Runtime ... >> marks:~ mehaase$ javac Test.java ...
    (comp.lang.java.help)
  • Re: If statement - whats going on here?
    ... > how a compiler would even detect that its nonsense code. ... > class Test { ... > marks:~ mehaase$ javac Test.java ...
    (comp.lang.java.help)