Re: Color comparison
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.