Dalziel
12-02-2002, 09:18 PM
Is there a C++ equivalent to Javascript's Math.round?
|
||||
C++ roundingDalziel 12-02-2002, 09:18 PM Is there a C++ equivalent to Javascript's Math.round? jkd 12-02-2002, 10:41 PM Just add 0.5 and typecast to int: int round(double a) { return int(a + 0.5); } double x = 1.234; double y = 1.567; round(x) == 1; round(y) == 2; There is probably some round() function in a common file though, like math.h or something, but I don't know about that for sure. Bosko 12-03-2002, 05:49 PM There is no round function in math.h,but there are two functions that you can use for this: double ceil(double x); Returns the smallest integer value greater than or equal to x. double floor(double x); Returns the largest integer value less than or equal to x. Note that these functions don't round the number,so ceil(.4) would return 1 and floor(.6) 0 .Heres a little function which rounds the number using ceil and floor: double round(double x) { if(x>=0.5){return ceil(x);}else{return floor(x);} } Wichetael 12-04-2002, 12:07 PM Bosko, first of all, have you ever hear of the ?: operator... much cleaner in this case. Secondly your code has a flaw, it only works for number between 0 and 1, if the number for instance were 2.3 it would be bigger than 0.5 and according to your code it would then be rounded up to 3.0 which is of course not right. But anyway why use such comlex and unoptimized code when you can do it much simpler, do it as jkd suggested. That's they way it has been done for ages and that's how built in round functions work. It really is the best way. Bosko 12-04-2002, 03:21 PM Originally posted by Wichetael Bosko, first of all, have you ever hear of the ?: operator... much cleaner in this case. Secondly your code has a flaw, it only works for number between 0 and 1, if the number for instance were 2.3 it would be bigger than 0.5 and according to your code it would then be rounded up to 3.0 which is of course not right. But anyway why use such comlex and unoptimized code when you can do it much simpler, do it as jkd suggested. That's they way it has been done for ages and that's how built in round functions work. It really is the best way. Yes,I have.But with the ? operator the statement must look like this: expression ? expression2 : expression3 So this wont work here,unless you add one more statement,which would make it as long as the if-statement. I know that,but the short code was only meant to show how to do this with functions from math.h (as jkd was not sure if a round() function existed in math.h). Wichetael 12-06-2002, 12:36 PM Originally posted by Bosko Yes,I have.But with the ? operator the statement must look like this: expression ? expression2 : expression3 So this wont work here,unless you add one more statement,which would make it as long as the if-statement. Well, I just think that this code: return (x > 0.5) ? ceil(x) : floor(x); is still a whole lot better looking than what you had... And it's still wrong of course, you'd need though I don't know if the frac function exists. The whole point is moot though since you don't need it. I know that,but the short code was only meant to show how to do this with functions from math.h (as jkd was not sure if a round() function existed in math.h). But that was exactly the point, you don't even need the round function since you can easily make one yourself (in fact any round function in any decent implementation would do it the same way), simply take the code jdk supplied, ie [code]int double(double x) { return (int)(x + 0.5); } and there you have your round function. Derek777 09-02-2006, 10:36 AM Jdk's response was good for positive numbers. I usually use: inline int round(double x) { return int(x > 0.0 ? x + 0.5 : x - 0.5); } kccarter 10-30-2007, 05:39 AM Well, I just think that this code: return (x > 0.5) ? ceil(x) : floor(x); is still a whole lot better looking than what you had... And it's still wrong of course, you'd need though I don't know if the frac function exists. The whole point is moot though since you don't need it. But that was exactly the point, you don't even need the round function since you can easily make one yourself (in fact any round function in any decent implementation would do it the same way), simply take the code jdk supplied, ie [code]int double(double x) { return (int)(x + 0.5); } and there you have your round function. in this code you want to do the following: double round(double x) { return ((x - floor(x)) >= 0.5) ? ceil(x) : floor(x); } this way you operate on the the value to the right of the decimal regardless of the value of x. ralph l mayo 10-30-2007, 09:09 PM It's in the standard library. #include <cmath> // ... std::cout << round(some_double); deXo 07-22-2009, 09:24 PM Bosko, please be smart enough to realize that both jkd and wichetael are way out of your league. jkd posseses an intelligence rate that you will most likely only experience while watching television, and wichetael is the proud owner of the same rate, only his confirms his programming experience. if(x>=0.5){return ceil(x);}else{return floor(x);} could be written as return ((x >= 0.5) ? ceil(x) : floor(x)); I used an unnecessary pair or parentheses, and still it's shorter than your if-else. When my browser first loaded this page and I read your post, I really didn't care a whole lot as I was but immediately convinced that you were quite ordinary in terms of intelligence, but when I found out that jkd had posted his message before you had posted yours, I began to realize the maginute of your lack of at birth given gifts. Your post was so unnecessary and stupid that it made me euforic about my IQ-result of 144, which a few days ago I thought to be shamefully low compared to certain idols of mine. I am but certain that I have written nothing of what you want to hear, but the truth has mysterious ways of deploying itself. Though it could this time be explained by my adrenaline aneurism or my autistic behaviour, I think you should gather all your mental and physical resources and repeatedly attempt to increase thy brain acitivity. And by the wizzle my mizzle, english is more than far from my primary language, so misspellings and bad grammar is selfexplanatory for those who need not gather mental and physical resources. jkd 07-23-2009, 12:48 AM WTF? Thread digging an ancient thread to rant about intelligence? Thread closed. |
| |||
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum