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

Re: Pure newbie :(


  • Subject: Re: Pure newbie :(
  • From: j o a r <email@hidden>
  • Date: Tue, 11 Nov 2003 22:30:27 +0100

This is a basic Computer Science question, and not really on topic for this list. Still, it is also something that you could find answers to in the list archives (<http://cocoa.mamasam.com/>).

That said, you should do something like this (given that you have two floating point values you want to compare, "f1" and "f2"):

if (fabs(f1 - f2) > 0.0001)
{
NSLog(@"Not equal!");
}
else
{
NSLog(@"Equal!");
}

The reason you need to do this is that floating point values will mutate ever so slightly while used because of the limits with which they can represent a number given the amounts of bits they have available. You might think that a float is 2.5, while in fact it's really 2.5000000000000000000001, or 2.499999999999999999999999999.

If you should use "> 0.1" or "> 0.000000000001" depends on the application. How much precision do you need? If you need absolute precision, use integers.

j o a r

On 2003-11-11, at 19.45, Denis Vaillant wrote:

I do not understand why the comparison does not work :

rxNext1 = 0.400000;

if(rxNext1==0.400000)
{
NSLog(@"Should work :(");
}

PS : rxNext1 is a float.
_______________________________________________
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: 
 >Pure newbie :( (From: Denis Vaillant <email@hidden>)

  • Prev by Date: Re: Pure newbie :(
  • Next by Date: Re: Pure newbie :(
  • Previous by thread: Re: Pure newbie :(
  • Next by thread: Re: Pure newbie :(
  • Index(es):
    • Date
    • Thread