Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 11, 2017, 05:26:24 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: 11.3 Epsilon necessary in compare method?  (Read 2261 times)
theconnorpower
Newbie
*
Posts: 4



Email
« on: October 14, 2010, 02:27:37 AM »

I thought it was always best practice to compare using an epsilon when comparing float values in C. Am I wrong? No one else seems to have implemented this approach. Example below.

Code: (Objective-C)
-(int) compare: (Fraction *)f
{
double selfNum = [self convertToNum];
double fNum = [f convertToNum];

if (fabs(selfNum - fNum) < DBL_EPSILON)
return 0;
else if (selfNum > fNum)
return 1;
else
return -1;
}
« Last Edit: October 15, 2010, 02:25:11 PM by theconnorpower » Logged
theconnorpower
Newbie
*
Posts: 4



Email
« Reply #1 on: October 24, 2010, 06:37:02 PM »

Bumpitty bump?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #2 on: October 24, 2010, 09:17:17 PM »

Yeah, I do think it's a good idea to compare floats against some epsilon due to the inherent inaccuracy of representing floating point numbers.

Cheers,

Steve
Logged
theconnorpower
Newbie
*
Posts: 4



Email
« Reply #3 on: October 24, 2010, 09:23:49 PM »

Awesome.

Thanks Steve.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.