Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Devaka Cooray
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Junilu Lacar
• Paul Clapham
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• salvin francis
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

# rint() and round()

Ranch Hand
Posts: 1873
hi,
whats the difference between rint() and round() methods of Math class?
i get same results for both of the call giving common value.
regards
maulin.

Ranch Hand
Posts: 203
From the API page

double rint(double a)
Returns the double value that is closest in value to a and is equal to a mathematical integer.

and

long round(double a)
Returns the closest long to the argument.
int round(float a)
Returns the closest int to the argument.

HTH
[ January 25, 2002: Message edited by: Shivaji Marathe ]

Ranch Hand
Posts: 94
they are supposed to be the same (in English). but in practice they are not, see this example:
<code>
for(int i=0; i<100; i++){
double d = i/10. - 5.;
double rint = Math.rint(d);
long round = Math.round(d);
System.out.println( round + ", " + rint + ", " + d);
}
</code>
This is the output (pay attention to values when d is -2.5, -1.5, 2.5, 3.5,4.5, quite confusing, isnt it?)
-5, -5.0, -5.0
-5, -5.0, -4.9
-5, -5.0, -4.8
-5, -5.0, -4.7
-5, -5.0, -4.6
-4, -4.0, -4.5
-4, -4.0, -4.4
-4, -4.0, -4.3
-4, -4.0, -4.2
-4, -4.0, -4.1
-4, -4.0, -4.0
-4, -4.0, -3.9
-4, -4.0, -3.8
-4, -4.0, -3.7
-4, -4.0, -3.6
-3, -4.0, -3.5
-3, -3.0, -3.4
-3, -3.0, -3.3
-3, -3.0, -3.2
-3, -3.0, -3.1
-3, -3.0, -3.0
-3, -3.0, -2.9
-3, -3.0, -2.8
-3, -3.0, -2.7
-3, -3.0, -2.6
-2, -2.0, -2.5
-2, -2.0, -2.4
-2, -2.0, -2.3
-2, -2.0, -2.2
-2, -2.0, -2.1
-2, -2.0, -2.0
-2, -2.0, -1.9
-2, -2.0, -1.7999999999999998
-2, -2.0, -1.7000000000000002
-2, -2.0, -1.6
-1, -2.0, -1.5
-1, -1.0, -1.4
-1, -1.0, -1.2999999999999998
-1, -1.0, -1.2000000000000002
-1, -1.0, -1.1
-1, -1.0, -1.0
-1, -1.0, -0.9000000000000004
-1, -1.0, -0.7999999999999998
-1, -1.0, -0.7000000000000002
-1, -1.0, -0.5999999999999996
0, -0.0, -0.5
0, -0.0, -0.40000000000000036
0, -0.0, -0.2999999999999998
0, -0.0, -0.20000000000000018
0, -0.0, -0.09999999999999964
0, 0.0, 0.0
0, 0.0, 0.09999999999999964
0, 0.0, 0.20000000000000018
0, 0.0, 0.2999999999999998
0, 0.0, 0.40000000000000036
1, 0.0, 0.5
1, 1.0, 0.5999999999999996
1, 1.0, 0.7000000000000002
1, 1.0, 0.7999999999999998
1, 1.0, 0.9000000000000004
1, 1.0, 1.0
1, 1.0, 1.0999999999999996
1, 1.0, 1.2000000000000002
1, 1.0, 1.2999999999999998
1, 1.0, 1.4000000000000004
2, 2.0, 1.5
2, 2.0, 1.5999999999999996
2, 2.0, 1.7000000000000002
2, 2.0, 1.7999999999999998
2, 2.0, 1.9000000000000004
2, 2.0, 2.0
2, 2.0, 2.0999999999999996
2, 2.0, 2.2
2, 2.0, 2.3
2, 2.0, 2.4000000000000004
3, 2.0, 2.5
3, 3.0, 2.5999999999999996
3, 3.0, 2.7
3, 3.0, 2.8
3, 3.0, 2.9000000000000004
3, 3.0, 3.0
3, 3.0, 3.0999999999999996
3, 3.0, 3.1999999999999993
3, 3.0, 3.3000000000000007
3, 3.0, 3.4000000000000004
4, 4.0, 3.5
4, 4.0, 3.5999999999999996
4, 4.0, 3.6999999999999993
4, 4.0, 3.8000000000000007
4, 4.0, 3.9000000000000004
4, 4.0, 4.0
4, 4.0, 4.1
4, 4.0, 4.199999999999999
4, 4.0, 4.300000000000001
4, 4.0, 4.4
5, 4.0, 4.5
5, 5.0, 4.6
5, 5.0, 4.699999999999999
5, 5.0, 4.800000000000001
5, 5.0, 4.9

bill williams
Ranch Hand
Posts: 94
the usual practice that Math.round(d)is Math.floor(d+0.5) might be not correct, see above example: for d = 1.5, Math.round(d) = 1???

bill williams
Ranch Hand
Posts: 94
sorry, that is correct