Author |
Message |
newTOjava365
|
Posted: 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
|
|
|
wtd
|
|
|
|
|
zylum
|
Posted: Wed Nov 24, 2004 10:17 pm Post subject: (No subject) |
|
|
yout forgot Math.round() |
|
|
|
|
|
bugzpodder
|
Posted: Thu Nov 25, 2004 11:43 am Post subject: (No subject) |
|
|
or some casting would do:
(int)(x+0.5) |
|
|
|
|
|
wtd
|
Posted: 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.
Does that vastly better than
The latter is correct, but not as good expressive as the first. |
|
|
|
|
|
bugzpodder
|
Posted: Thu Nov 25, 2004 1:50 pm Post subject: (No subject) |
|
|
i am guessing that integer casting is faster than Math.Round |
|
|
|
|
|
rizzix
|
Posted: 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 |
|
|
|
|
|
wtd
|
Posted: Thu Nov 25, 2004 2:09 pm Post subject: (No subject) |
|
|
bugzpodder wrote: i am guessing that integer casting is faster than Math.Round
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
|
|
|
bugzpodder
|
Posted: 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
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
|
Posted: 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
|
Posted: 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 |
|
|
|
|
|
bugzpodder
|
Posted: 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
right on |
|
|
|
|
|
|