# Modulo operator

Sapna Mathur

Greenhorn

Posts: 8

Oliver Grass

Ranch Hand

Posts: 65

posted 15 years ago

Hi Sapna,

JLS states " Instead, the Java programming language defines % on floating-point operations to behave in a manner analogous to that of the integer remainder operator; this may be compared with the C library function fmod. "

So the computation works like a integer modulo operation. For your example: 2.2d is treated as a 2, so the remainder is 2....

Cause its a double you have a "incorrect" value, when displaying it...

hope that helps,

correct me if i'm wrong

Oliver

JLS states " Instead, the Java programming language defines % on floating-point operations to behave in a manner analogous to that of the integer remainder operator; this may be compared with the C library function fmod. "

So the computation works like a integer modulo operation. For your example: 2.2d is treated as a 2, so the remainder is 2....

Cause its a double you have a "incorrect" value, when displaying it...

hope that helps,

correct me if i'm wrong

Oliver

lakshmi nair

Ranch Hand

Posts: 63

bill bozeman

Ranch Hand

Posts: 1070

posted 15 years ago

It has to do with the precision of doubles and floats. Since doubles are not precise numbers like integers, you lose a .00000001 here or there. So you end up with 44d % 2.2d not ever giving you 0 becuase like lakshmi said, it is like writing 44d - 2.2d over and over until you are less than 2.2d.

You can try this in a loop like this:

You can try this in a loop like this:

Sapna Mathur

Greenhorn

Posts: 8

Shubhangi A. Patkar

Ranch Hand

Posts: 78

posted 15 years ago

You can't. It is to do with the way java stores the floating point numbers internally. You will always get an approximate value while dealing with floating points no matter what operation you perform on them.

HTH

Shubhangi

Originally posted by Sapna Mathur:

So how can I get an accurate modulus value if I have to use it

in a program ? Will it not affect the accuracy of the program if an incorrect value is produced in the beginning of a process?

You can't. It is to do with the way java stores the floating point numbers internally. You will always get an approximate value while dealing with floating points no matter what operation you perform on them.

HTH

Shubhangi

Sapna Mathur

Greenhorn

Posts: 8

lakshmi nair

Ranch Hand

Posts: 63

posted 15 years ago

I just wanted to share some knowledge i gained.

There is a method Math.IEEEremainder(double,double) which can give you the a result which can be sometimes equivalent to % operation.

Here goes the actual explanation from API..

and some results...

IEEEremainder(10,2.5) 0.0

IEEEremainder(10.5, 2.5) 0.5

IEEEremainder(11, 2.5) 1.0

IEEEremainder(9.5, 2.5) -0.5

IEEEremainder(9.0, 2.5) -1.0

IEEEremainder(8.0, 2.5) 0.5

IEEEremainder(8.5, 2.5) 1.0

IEEEremainder(7.4, 2.5) -0.09999999999999964

lakshmi

[This message has been edited by lakshmi nair (edited November 16, 2000).]

There is a method Math.IEEEremainder(double,double) which can give you the a result which can be sometimes equivalent to % operation.

Here goes the actual explanation from API..

*Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. The remainder value is mathematically equal to f1 - f2 � n, where n is the mathematical integer closest to the exact mathematical value of the quotient f1/f2, and if two mathematical integers are equally close to f1/f2, then n is the integer that is even. If the remainder is zero, its sign is the same as the sign of the first argument.*

and some results...

IEEEremainder(10,2.5) 0.0

IEEEremainder(10.5, 2.5) 0.5

IEEEremainder(11, 2.5) 1.0

IEEEremainder(9.5, 2.5) -0.5

IEEEremainder(9.0, 2.5) -1.0

IEEEremainder(8.0, 2.5) 0.5

IEEEremainder(8.5, 2.5) 1.0

IEEEremainder(7.4, 2.5) -0.09999999999999964

lakshmi

[This message has been edited by lakshmi nair (edited November 16, 2000).]

Don't get me started about those stupid light bulbs. |