You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Phani <ph...@yahoo.com> on 2004/11/19 19:06:14 UTC
Remainder Operation
Sorry this is not a struts question..more anlytical..
I have this simple program where I want to check
whether the division operation returns a whole number
or not...For this purpose I am actually checking
whether the remainder is zero..which fails for a
particular case..
Here is my program below..Hope to get any help ..
Thanks.
public class ROPTest {
public double d1 = 0.0;
public double d2 = 1.0;
public double inc = 0.1;
public double diff;
public double res;
public ROPTest() {
}
/* This function is to verify whether the division
operation --> diff/inc returns a whole number or not
*/
public void compute() {
double diff = d2 - d1;
res = diff%inc;
if(res == 0)
System.out.println("The Result is Zero:
"+res);
else
System.out.println("The Result is not Zero:
"+res);
}
public static void main(String args[]) {
ROPTest rt = new ROPTest();
rt.compute();
}
}
__________________________________
Do you Yahoo!?
The all-new My Yahoo! - Get yours free!
http://my.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: OT Remainder Operation
Posted by Jeff Beal <jb...@webmedx.com>.
You can't use the == operator with doubles reliably. The values are
often imprecise. Due to rounding, you may be comparing
0.000000000000001 to 0, and those are not equal.
Change
if (res == 0)
to
if (Math.abs(res) <= 0.000000001)
You can probably just declare res as an int, also:
int res = (int)(diff % inc);
-- Jeff
Phani wrote:
> Sorry this is not a struts question..more anlytical..
>
> I have this simple program where I want to check
> whether the division operation returns a whole number
> or not...For this purpose I am actually checking
> whether the remainder is zero..which fails for a
> particular case..
>
> Here is my program below..Hope to get any help ..
>
> Thanks.
>
> public class ROPTest {
>
> public double d1 = 0.0;
> public double d2 = 1.0;
> public double inc = 0.1;
>
> public double diff;
> public double res;
>
> public ROPTest() {
>
> }
>
> /* This function is to verify whether the division
> operation --> diff/inc returns a whole number or not
> */
>
> public void compute() {
>
> double diff = d2 - d1;
> res = diff%inc;
>
> if(res == 0)
> System.out.println("The Result is Zero:
> "+res);
>
> else
> System.out.println("The Result is not Zero:
> "+res);
>
> }
>
>
> public static void main(String args[]) {
>
> ROPTest rt = new ROPTest();
> rt.compute();
> }
> }
>
>
>
>
> __________________________________
> Do you Yahoo!?
> The all-new My Yahoo! - Get yours free!
> http://my.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org