Dan D'amico

Ranch Hand

Posts: 94

posted 3 years ago

output :

8.5 , 1.0

4.25 , 0.5

2.125 , 0.25

1.0625 , 0.125

0.53125, 1.0625

17/2 = 8.5 (have 1 remainder so its equals 1 , ok )

now, i dont get it , 8.5 / 2 = 4.25 so why 8.5 % 2 = 0.5 ? it should be 2 ? because its have two digits after thr decimal point. - > 4.25 .

and the same for all the others

output :

8.5 , 1.0

4.25 , 0.5

2.125 , 0.25

1.0625 , 0.125

0.53125, 1.0625

17/2 = 8.5 (have 1 remainder so its equals 1 , ok )

now, i dont get it , 8.5 / 2 = 4.25 so why 8.5 % 2 = 0.5 ? it should be 2 ? because its have two digits after thr decimal point. - > 4.25 .

and the same for all the others

posted 3 years ago

Should it? First-off, the idea of a modulus op (java actually calls it the "remainder operator") on a floating-point value seems somewhat odd to me.

For an explanation like this, you need to go to the horse's mouth; but unfortunately, even it doesn't provide examples of the style that you're doing. What it does do though is explain

"the floating-point remainder r from the division of a dividend n by a divisor d is defined by the mathematical relation r = n - (d ⋅ q) where q is an integer that is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as possible without exceeding the magnitude of the true mathematical quotient of n and d."

So, in your case above (8.5%2), the calculation is 8.5 - (2 * 4), which == 0.5.

Winston

Dan D'amico wrote:now, i dont get it , 8.5 / 2 = 4.25 so why 8.5 % 2 = 0.5 ? it should be 2 ?

Should it? First-off, the idea of a modulus op (java actually calls it the "remainder operator") on a floating-point value seems somewhat odd to me.

For an explanation like this, you need to go to the horse's mouth; but unfortunately, even it doesn't provide examples of the style that you're doing. What it does do though is explain

*how*the calculation is done - specifically:

"the floating-point remainder r from the division of a dividend n by a divisor d is defined by the mathematical relation r = n - (d ⋅ q) where q is an integer that is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as possible without exceeding the magnitude of the true mathematical quotient of n and d."

So, in your case above (8.5%2), the calculation is 8.5 - (2 * 4), which == 0.5.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

posted 3 years ago

- 1

9.25 % 2 = 1.25

1) .25 straight away goes towards final answer

2) 9 / 2 = 4 (1 remainder)

3) 1 + 0.25 = 1.25

a) Basically all the time at the very beginning discard floating part as it's not significant and use only whole part.

b) Divide whole number by given number, in your case 2, take the remainder, and add him to floating part from the very beginning.

I hope it helps to understand the main idea.

1) .25 straight away goes towards final answer

2) 9 / 2 = 4 (1 remainder)

3) 1 + 0.25 = 1.25

a) Basically all the time at the very beginning discard floating part as it's not significant and use only whole part.

b) Divide whole number by given number, in your case 2, take the remainder, and add him to floating part from the very beginning.

I hope it helps to understand the main idea.

Campbell Ritchie

Marshal

Posts: 56578

172

posted 3 years ago

No, I am afraid that is not how it works. The details are in the JLS link which Winston posted earlier.

Remember that the operands of binary arithmetic operators undergo promotion before the operation. So the first things which happens is that the division is converted from

9.25 % 2

to

9.25 % 2.0

9.25 / 2.0 → 4.625

This is truncated to 4.0

4.0 * 2.0 → 8.0

9.25 - 8.0 → 1.25 QED

Remember that the operands of binary arithmetic operators undergo promotion before the operation. So the first things which happens is that the division is converted from

9.25 % 2

to

9.25 % 2.0

9.25 / 2.0 → 4.625

This is truncated to 4.0

4.0 * 2.0 → 8.0

9.25 - 8.0 → 1.25 QED

The IEEE remainder is described in the JLS link.campbell@XXXXX:~/java$ java RemainderDemo 9.25 2

9.250000 % 2.000000 = 1.250000

9.250000 BigDecimal.remainder(2.000000) = 1.250000

9.250000 IEEE% 2.000000 = -0.750000

.....

java RemainderDemo 9.25 0

9.250000 % 0.000000 = NaN

Exception in thread "main" java.lang.ArithmeticException: Division by zero

at java.math.BigDecimal.divide(BigDecimal.java:1742)

at java.math.BigDecimal.divideToIntegralValue(BigDecimal.java:1792)

at java.math.BigDecimal.divideAndRemainder(BigDecimal.java:1948)

at java.math.BigDecimal.remainder(BigDecimal.java:1890)

at RemainderDemo.showRemainders(RemainderDemo.java:27)

at RemainderDemo.main(RemainderDemo.java:7)