Simple Division in Java - Is This a Bug or a Feature

Simple division in Java - is this a bug or a feature?

You're using integer division.

Try 7.0/10 instead.

java double number division giving strange output

In the 1st case first division is done so an int divided by an int gives a integer i.e, 11/12=0 Then this integer is converted to double .ie, 0.0. In the 2nd case a double(11.00) is divided by a integer(12).The integer 12 is then automatically type casted to double as division should occur with similar types. This division gives a double value which is exact in reality(0.916666667)

See this link https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

wrong results from simple java method

Change

double average = sum/numbers;

to

double average = (double)sum/numbers;

to force floating point division.

Otherwise, int division (which is the operation that takes place when dividing two variables of int type) will give you 2 when dividing 8/3 (as you do in your example).

Java, 15/6 equation returning 2.0?

double average = 15/6.0;

Division of int by int will return an int. That is why you get 2. Then, as you declared it as a double, it would add the .0 at the end.

If you divide a double by double (15/6.0, 15.0/6, or 15.0/6.0) it will come out as a double.

Java - custom integer division function

In

return dividend/divisor + dividend%divisor == 0 ? 0:1;

The addition of

dividend/divisor + dividend%divisor

is performed and then the result is compared to 0.

You want:

return dividend/divisor + (dividend%divisor == 0 ? 0:1);

or

return dividend/divisor + (dividend%divisor == 0) ? 0:1;

In order that only dividend%divisor will be compared to 0.

Looking to have a precise number

Dividing two ints will use integer division, so 46/45 will yield 1. If you want to ceil the result, you'll need to perform a floating point division:

int pages = (int) Math.ceil(number / 45.0); 

Java Double variables have strange values

In IEEE-754 binary double, we need to consider 1.1 and 1.2 in the binary representation:

1.2 = 0b1.001100110011001100110011001100110011001100110011001100110011...
1.1 = 0b1.000110011001100110011001100110011001100110011001100110011001...

note that we need infinitely many bits to represent them exactly in binary. double only has 53 bits of significance, we have to chop off the numbers:

1.2 = 0b1.001100110011001100110011001100110011001100110011001100110011...
1.1 = 0b1.000110011001100110011001100110011001100110011001100110011001...
^ round from here
==>
1.2 ~ 0b1.0011001100110011001100110011001100110011001100110011
(= exactly 1.1999999999999999555910790149937383830547332763671875)
1.1 ~ 0b1.0001100110011001100110011001100110011001100110011010
(= exactly 1.100000000000000088817841970012523233890533447265625)

Hence 1.2 - 1.1 is:

  1.2 ~ 0b1.0011001100110011001100110011001100110011001100110011
- 1.1 ~ 0b1.0001100110011001100110011001100110011001100110011010
————————————————————————————————————————————————————————————————
0b0.00011001100110011001100110011001100110011001100110010000
(= exactly 0.09999999999999986677323704498121514916419982910156250000)

We can actually compute 60 / 0.0999999999999998667732370449812151491641998291015625 exactly, which gives

600.0000000000007993605777301137740672368493927467455286920109359612256820927...
^ 16th significant figure

that matches OP's result of

600.0000000000008


Related Topics



Leave a reply



Submit