• 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: OT: floating point (was: Pure newbie :()
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: OT: floating point (was: Pure newbie :()


  • Subject: Re: OT: floating point (was: Pure newbie :()
  • From: Christopher Corbell <email@hidden>
  • Date: Tue, 11 Nov 2003 15:12:07 -0800

On Nov 11, 2003, at 1:18 PM, Sscar Morales Vivs wrote:

On 11 Nov 2003, at 20:54, David Blanton wrote:

On 11/11/03 12:07 PM, "Tony S. Wu" <email@hidden> wrote:

try using double.
never compare two floats.

Why never compare two floats?

It's more like "never trust == and != when operating with floating point numbers". '<' and '>' are ok.

If you really need to use equality you should define a macro or inline function like the following (I'm using C++ syntax as I'm just copying from my code):

bool
Math::Equals(const float a, const float b)
{
return std::abs(a - b) < kEpsilon;
}

With kEpsilon depending on what size of float you're using. A guideline (right out of my a** as I don't feel like digging the actual numbers). For 32 bit floats no smaller than 10E-7, and for 64 bit doubles I think it should be around 10E-12.

Corrections admitted there ;)

[....]

Changed to OT since this is more like a comp.lang.c thread -
in fact there may be a section in the FAQ - but FWIW the
appropriate size of a floating-point equality epsilon is
going to depend not only on type but also on context.

Picking an arbitrary small value won't necessarily work if
you're testing equality against the result of some floating-point
arithmetic operation - what if you're adding or otherwise
performing calculations with doubles that are smaller than
10E-12? An epsilon of this size would discard significant
differences in result. OTOH if you're using very large doubles
in calculations then this epsilon may be -too- small - you
may want to treat very large numbers results equal if they
differ by 1E-1. Especially if the operands come from arbitrary
sources (user input, or numerical data of potentially any
value range) epsilons for floatiing-point equality tests
need to be scalable according to context, esp. expected
magnitude of the result.

- Christopher
_______________________________________________
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.

  • Follow-Ups:
    • Re: OT: floating point (was: Pure newbie :()
      • From: "Angela Brett" <email@hidden>
References: 
 >Re: Pure newbie :( (From: Óscar Morales Vivó <email@hidden>)

  • Prev by Date: Re: Two views of same text
  • Next by Date: Matching Postal Addresses
  • Previous by thread: Re: Pure newbie :(
  • Next by thread: Re: OT: floating point (was: Pure newbie :()
  • Index(es):
    • Date
    • Thread