And of course, it should be noted that due to the inaccuracy of floats, you still might not get exactly the answer you expect.
For instance, round( 1.234 * 100.0 ) / 100.0 might give 1.229999999, which would be printed as 1.22 if you are limiting the number of digits.
But that's just the way the cookie crumbles.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Bleh - brainfart. For ome reason I thought it didn't mention rounding.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
i don't have a need for rounding to a particular number of decimal points, just to intelligently convert floats into ints.Code:int round(double f) { unsigned int i = fabs(f*2); if(i&1) { if(f>0) { return (int)++f; } return (int)--f; } else { return (int)f; } }
this seems to do the trick, but i thought i'd throw it up in case someone can spot a problem.