Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 rounding in java
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
newTOjava365




PostPosted: Wed Nov 24, 2004 9:26 pm   Post subject: rounding in java

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




PostPosted: Wed Nov 24, 2004 9:35 pm   Post subject: (No 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)
zylum




PostPosted: Wed Nov 24, 2004 10:17 pm   Post subject: (No subject)

yout forgot Math.round() Laughing
bugzpodder




PostPosted: Thu Nov 25, 2004 11:43 am   Post subject: (No subject)

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




PostPosted: Thu Nov 25, 2004 1:38 pm   Post subject: (No 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.
bugzpodder




PostPosted: Thu Nov 25, 2004 1:50 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 25, 2004 1:52 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 25, 2004 2:09 pm   Post subject: (No 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.
Sponsor
Sponsor
Sponsor
sponsor
bugzpodder




PostPosted: Thu Nov 25, 2004 2:39 pm   Post subject: (No 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.
wtd




PostPosted: Thu Nov 25, 2004 3:17 pm   Post subject: (No subject)

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




PostPosted: Thu Nov 25, 2004 3:41 pm   Post subject: (No 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
bugzpodder




PostPosted: Sat Nov 27, 2004 3:35 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: