Computer Science Canada

rounding in java

Author:  newTOjava365 [ Wed Nov 24, 2004 9:26 pm ]
Post subject:  rounding in java

how do i round numbers to the nearest whole number (int)?

Author:  wtd [ Wed Nov 24, 2004 9:35 pm ]
Post subject: 

Ceiling:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#ceil(double)

Floor:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#floor(double)

Author:  zylum [ Wed Nov 24, 2004 10:17 pm ]
Post subject: 

yout forgot Math.round() Laughing

Author:  bugzpodder [ Thu Nov 25, 2004 11:43 am ]
Post subject: 

or some casting would do:
(int)(x+0.5)

Author:  wtd [ Thu Nov 25, 2004 1:38 pm ]
Post subject: 

bugzpodder wrote:
or some casting would do:
(int)(x+0.5)


The goal of programming should be expressivity. You should be able to read a program and understand what the programmer meant to do.

code:
Math.round(x);


Does that vastly better than

code:
(int)(x + 0.5)


The latter is correct, but not as good expressive as the first.

Author:  bugzpodder [ Thu Nov 25, 2004 1:50 pm ]
Post subject: 

i am guessing that integer casting is faster than Math.Round Wink

Author:  rizzix [ Thu Nov 25, 2004 1:52 pm ]
Post subject: 

but u can't simply assume that. they might have optimized Math.Round (since it's so commonly used) who knows Question

Author:  wtd [ Thu Nov 25, 2004 2:09 pm ]
Post subject: 

bugzpodder wrote:
i am guessing that integer casting is faster than Math.Round Wink


This attitude is called "premature optimization". It kills projects.

First make your program work, then make it fast. Part of making that first step easier is making your code easier to read and understand. If you use the cast, then you go back and read your code a few weeks later, you may not have any clue what that cast is about. Now you have to read through the code and remember why you did that.

If you see "Math.round" (or floor or ceil), then you know instantly what's going on.

The best optimizations come out of elegantly written software.

Author:  bugzpodder [ Thu Nov 25, 2004 2:39 pm ]
Post subject: 

rizzix wrote:
but u can't simply assume that. they might have optimized Math.Round (since it's so commonly used) who knows Question

the casting thing is 2 operations. but i think they do a lot of stuff in Math.Round, including checking for overflows, etc
look at the link i posted in the C# vs java thread in general programming.

wtd, of course that depends on the situation... i've never being a professional developer, so i dont really regard these things as important.

Author:  wtd [ Thu Nov 25, 2004 3:17 pm ]
Post subject: 

If you ever hope to be a professional developer, start taking them seriously.

Author:  zylum [ Thu Nov 25, 2004 3:41 pm ]
Post subject: 

i think bugzpodder is in the programming contest mentality where everything needs to be as fast as possible or else it might time out Wink

Author:  bugzpodder [ Sat Nov 27, 2004 3:35 pm ]
Post subject: 

zylum wrote:
i think bugzpodder is in the programming contest mentality where everything needs to be as fast as possible or else it might time out Wink


right on Wink


: