how do you do it?
I want to round up if the value is .5+, and down if .49 or less.
how do you do it?
I want to round up if the value is .5+, and down if .49 or less.
preferably in an elegant way...
short code looks pretty.
round().
Unless you're on some compiler that hasn't gotten up to 1999, in which case you have to trunc(number+0.5).
how do you use it?
is there a guide here on the forum?
thnkx
Do you have a C book, or reference of some kind? They're "built-in" functions (library functions, really, but close enough).
Anyway, they're in <math.h>.
Not according to cplusplus.com
QuantumPete
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
And I guess trunc is C99 only, too, it's floor that's in C89.Originally Posted by ISO C99
Originally Posted by ISO C99
Alternatively, you could use ceil and floor..
Code:double n = 23.45, floorDiff = n - floor(n), ceilDiff = ceil(n) - n; if(floorDiff < ceilDiff) n = floor(n); else n = ceil(n);
"What's up, Doc?"
"'Up' is a relative concept. It has no intrinsic value."
And if you do this "a lot", you could optimize the calls to ceil/floor by doing:
Whether that is worth it or not depends a bit on the actual implementation of floor() and ceil(), as they may not be "true functions", in which case the overhead is much smaller than if they are real functions.Code:double n = 23.45, nFloor = floor(n), nCeil = ceil(n); double floorDiff = n - nFloor, ceilDiff = nCeil - n; if(floorDiff < ceilDiff) n = nFloor; else n = nCeil;
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.