Re: frustrating arithmetic
- From: "Rhino" <no.offline.contact.please@xxxxxxxxxx>
- Date: Wed, 28 Dec 2005 11:19:44 -0500
"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
.
- Follow-Ups:
- Re: frustrating arithmetic
- From: Jonie
- Re: frustrating arithmetic
- References:
- frustrating arithmetic
- From: Jonie
- Re: frustrating arithmetic
- From: Rhino
- frustrating arithmetic
- Prev by Date: Re: Suduko solver demo
- Next by Date: Re: Loop help
- Previous by thread: Re: frustrating arithmetic
- Next by thread: Re: frustrating arithmetic
- Index(es):
Relevant Pages
|