• 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: Color comparison
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Color comparison


  • Subject: Re: Color comparison
  • From: Marcel Weiher <email@hidden>
  • Date: Tue, 4 Jun 2002 07:39:29 +0200

On Tuesday, June 4, 2002, at 12:08 Uhr, David Sinclair wrote:


Alternatively, is there any way of avoiding the difference in values. I doubt this one is feasible, as it looks like a rounding difference -- e.g. 0.0588235 vs 0.0581305.

You generally can't use equality to compare float/double values, because of rounding differences. So you typically need some tolerances.

Here are two handy functions I use that compare to a tolerance specified as a percentage of the values in question.



static BOOL valuesWithinPercentRelativeTo( float v1, float v2,float baseVal, int percent )
{
if ( v1 == 0 && v2 ==0 ) {
return YES;
} else {
float diff = fabs( v2 - v1 );
float percentOfV1 = fabs(percent * baseVal / 100.0);
return diff <= percentOfV1;
}
}

static BOOL valuesWithinPercent( float v1, float v2, int percent )
{
return valuesWithinPercentRelativeTo( v1, v2, v1, percent );
}



--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.
_______________________________________________
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: Color comparison
      • From: Jason Harris <email@hidden>
References: 
 >Color comparison (From: David Sinclair <email@hidden>)

  • Prev by Date: Re: Switching between editable and not in a NSTableView
  • Next by Date: Help : Releasing NSWindowController object ?
  • Previous by thread: Re: Color comparison
  • Next by thread: Re: Color comparison
  • Index(es):
    • Date
    • Thread