Re: rounding a float
Re: rounding a float
- Subject: Re: rounding a float
- From: Michael Hudson <email@hidden>
- Date: Tue, 02 Dec 2003 17:38:50 +0000
Alastair Houghton <email@hidden> writes:
>
On 30 Nov 2003, at 03:41, Ed Watkeys wrote:
>
>
> 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.
>
>
[snip]
>
>
> 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.
>
>
That isn't strictly speaking the case. It is the specific format
>
mandated by the IEEE standard that makes it impossible, the reason
>
being that it uses a binary exponent.
Well, OK, but realistically any data stored in a variable of C type
'float' or 'double' is going to be base-2.=, because modern FPUs are
all based more-or-less on the ieee754 standard.
>
Not all implementations of floating point arithmetic use binary as
>
the base for their exponent, hexadecimal is quite common,
"quite common"? I think some old IBM mainframes use it...
>
but I'm sure I've heard of a format that used a decimal exponent
>
(which *will* allow accurate representation of decimal numbers).
This page may be of interest:
http://www2.hursley.ibm.com/decimal/
(I don't know if there are any machines that do base-10 arithmetic in
hardware; again big iron from IBM seems the most likely).
NSDecimalNumber does seem to be the answer here, mind.
Cheers,
mwh
--
Programming languages should be designed not by piling feature on
top of feature, but by removing the weaknesses and restrictions
that make the additional features appear necessary.
-- Revised(5) Report on the Algorithmic Language Scheme
_______________________________________________
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.