Amazon.com Widgets 11.3 Epsilon necessary in compare method?
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 18, 2013, 11:09:23 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 11
| | | |-+  11.3 Epsilon necessary in compare method?
Pages: [1]   Go Down
Print
Author Topic: 11.3 Epsilon necessary in compare method?  (Read 1064 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: 3109







« 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:  



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.