• Post Reply Bookmark Topic Watch Topic
  • New Topic

Math.round()  RSS feed

 
age spets
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wondering...
This code:
double a;
a=i/2.00;
System.out.println("a="+a+" Math.round(a)=" +Math.round(a));
if(Math.round(a)==i/2)
{
}
Gives correct output, until it starts acting strange and gives the following output:
SystemOut U a=17.5 Math.round(a)=18
SystemOut U a=18.0 Math.round(a)=18
SystemOut U a=18.5 Math.round(a)=18
SystemOut U a=19.0 Math.round(a)=19
SystemOut U a=19.5 Math.round(a)=20
SystemOut U a=20.0 Math.round(a)=20
SystemOut U a=20.5 Math.round(a)=20
SystemOut U a=21.0 Math.round(a)=21
If I use:
if(i/2.00==i/2)
Solves my problem..
Why is it like this, seems nearly like a memory thing???
 
Vinod Chandana
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the data type of i?
- Vinod.
Originally posted by age spets:
Just wondering...
This code:
double a;
a=i/2.00;
System.out.println("a="+a+" Math.round(a)=" +Math.round(a));
if(Math.round(a)==i/2)
{
}
Gives correct output, until it starts acting strange and gives the following output:
SystemOut U a=17.5 Math.round(a)=18
SystemOut U a=18.0 Math.round(a)=18
SystemOut U a=18.5 Math.round(a)=18
SystemOut U a=19.0 Math.round(a)=19
SystemOut U a=19.5 Math.round(a)=20
SystemOut U a=20.0 Math.round(a)=20
SystemOut U a=20.5 Math.round(a)=20
SystemOut U a=21.0 Math.round(a)=21
If I use:
if(i/2.00==i/2)
Solves my problem..
Why is it like this, seems nearly like a memory thing???
 
age spets
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i is a int.. This is in a loop. See your point. Tried the code again with ii as a double:
double ii=Double.parseDouble(Integer.toString(i));
a=ii/2.00;
Same result..
Any ideas?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!