• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: rounding a float
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: rounding a float


  • Subject: Re: rounding a float
  • From: Alastair Houghton <email@hidden>
  • Date: Sun, 30 Nov 2003 12:01:42 +0000

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. Not all implementations of
floating point arithmetic use binary as the base for their exponent,
hexadecimal is quite common, but I'm sure I've heard of a format that
used a decimal exponent (which *will* allow accurate representation of
decimal numbers).

Of course, you probably shouldn't use floating-point for currency
values even then, because floating point calculations silently lose
precision (whereas when dealing with money, you absolutely need to know
[and control] where [and how... some financial rounding rules are,
well, unusual] any rounding is taking place).

Kind regards,

Alastair.

[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.

References: 
 >rounding a float (From: "Jay Rimalrick" <email@hidden>)
 >Re: rounding a float (From: Ed Watkeys <email@hidden>)

  • Prev by Date: Re: rounding a float
  • Next by Date: Hypothetical NSTreeController and NSOutlineView with bindings
  • Previous by thread: Re: rounding a float
  • Next by thread: Re: rounding a float
  • Index(es):
    • Date
    • Thread