Helios - can you explain a little more how you got a set precision effect from ceil? |
ceil(num*pow(10,x))/pow(10,x) |
Mar 14, 2008 at the top of the file. I'm actually somewhat surprised that your compiler is not complaining about the cout statements, since you haven't specified it, and your not clarifying it with std::cout. Dev-C complains that is an antiquated header, too. What compiler are you using? The round functions return the integral value nearest to x rounding half-way cases away from zero, regardless of the current rounding direction. The lround and llround functions return the integral value nearest to x (rounding half-way cases away from zero, regardless of the current rounding direction) in the return formats specified. Duplicate of: round for float in C I'm using VS2008 and I've included math.h but I still can't find a round function. Does it exist? I'm seeing a bunch of 'add 0.5 and cast to int' solutio. Rounding and remainder functions ceil Round up value (function ) floor Round down value (function ) fmod Compute remainder of division (function ) trunc Truncate value (function ) round Round to nearest (function ) lround Round to nearest and cast to long integer (function ) llround Round to nearest and cast to long long integer (function ) rint. Formatting Numbers with C Output Streams David Kieras, EECS Dept., Univ. Of Michigan Revised for EECS 381, Winter 2004. Using the output operator with C streams is generally easy as pie, with the only hard part being controlling the format of.
What he is doing is multiply the number with a power of ten (the power depends from the number of decimal point you want), use the ceil() function to eliminate the remaining decimal points and then divide with the same power of ten again to put the decimal points (the one that we need the precision to be) back to their original place.
For example if you have the number 12.456789 and you want a precision of 2 decimal numbers then you will have:
12.456789 * 100 (10 to the 2nd power) = 1245.6789
ceil( 1245.6789 ) = 1246
![Table Table](/uploads/1/2/6/6/126610758/749683382.jpg)
So you have the presicion you want.
@helios
Just a typo:
C++ Rounding Doubles
The first 'x' you have must be your number and the rest 'x's the number of decimals points you want.[EDIT] This formula doesn't round the number correctly, just eliminates the decimal points after the precision you want and rounds up the number.
Dev C++ 5.11
This would round it correctly:
Dev C Rounding Worksheets
ceil( ( num * pow( 10,x ) ) - 0.49 ) / pow( 10,x );