Re: rounding a float
Re: rounding a float
- Subject: Re: rounding a float
- From: Ed Watkeys <email@hidden>
- Date: Sat, 29 Nov 2003 22:41:59 -0500
On Nov 29, 2003, at 7:17 PM, Jay Rimalrick wrote:
What is the easiest way to round a float value with either c or
objective-c. I
need this float value to be consistent with the rounding of currency
e.g.
1.255 rounds to 1.26 and
1.254 rounds to 1.25
You can't do this with floating point numbers; their representation
makes it impossible. Consider the following program, float-test:
#include <stdlib.h>
#include <stdio.h>
int main (int argc, char **argv) {
int i;
float f = 0.0f, g= 123.456789f;
for(i = 0; i < 10000; i++) {
printf("%.10f\n", f);
f += 0.10f;
}
}
When run on my PowerBook, I get the following result:
-bash-2.05b$ ./float-test | tail -1
999.8029174805
Hmm. Off by about 0.197. Floating point arithmetic is by nature
inexact; floating point numbers are represented by a binary mantissa
and an exponent. The mantissa only has so much precision, and no amount
of calculating is going to get you nice round (decimal) numbers. I
believe Knuth's Art of Computer Programming, Volume 2 addresses
floating point arithmetic. I used single precision floats in this
example; doubles would produce the same phenomenon, albeit the error
would be smaller.
Unless you want to use a special-purpose arbitrary precision math
library, here's an example of what you probably want to do:
[NSString stringWithFormat:@"%.2f", 1.25]
Round floats for presentation to humans by creating strings. That said,
if you're dealing with money, you're better off calculating everything
using integers and then doing input and output that interprets the
integer as a fixed-precision decimal number. Why? Integer arithmetic is
exact.
Ed
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.