Amazon.com Widgets 11.2 trouble
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 23, 2014, 06:45:22 AM
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
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 11
| | | |-+ 11.2 trouble
Pages: [1] Go Down
Print
Author Topic: 11.2 trouble (Read 513 times)
electricb
Newbie
*
Posts: 6


Email




on: May 02, 2012, 04:27:54 PM

Why is my output rounding to the nearest whole number? For example, if I set one fraction to 6/8 and the other to 4/8, both the compare and isEqualTo methods will state that they're equal. Unless I set the values of the fractions far enough apart, this happens. I don't know why this happens considering that in both of my methods I set the result of the fractions to a double type which should prevent the answer from being truncated. Here are my methods the main part of the code:

-(BOOL) isEqualTo: (Fraction *) f
{
     double a = self.numerator / f.numerator;
     double b = self.denominator / f.denominator;
    
    if (a == b)
        return YES;
    else
        return NO;
}

-(int) compare: (Fraction *) f
{
    double a = self.numerator / self.denominator;
    double b = f.numerator / f.denominator;
    
    if (a == b)
        return 0;
    else if (a < b)
        return -1;
    else
        return 1;
}


int main (int argc, const char * argv[]) {  
    @autoreleasepool {
        
        Fraction *a = [[Fraction alloc] init];  
        Fraction *b = [[Fraction alloc] init];  
        BOOL result;  
        
        [a setTo: 6 over: 8];  
        [b setTo: 4 over: 8];  
        
        result = [a isEqualTo: b];
        
        if (result == YES)
        {
            [a print];
            NSLog(@"is equal to");
            [b print];
        }
        else {
            [a print];
            NSLog(@"is NOT equal to");
            [b print];
        }
        
        result = [a compare: b];
        
        if (result == -1)
        {
            [a print];
            NSLog(@"is less than");
            [b print];
        }
        else if (result == 0)
        {
            [a print];
            NSLog(@"is equal to");
            [b print];
        }
        else
        {
            [a print];
            NSLog(@"is greater than");
            [b print];
        }
    }
    return 0;  
}
Last Edit: May 02, 2012, 05:44:52 PM by electricb Logged
Qpido
Newbie
*
Posts: 7






Reply #1 on: May 04, 2012, 04:06:45 AM

Hey bro,

I think you need to make sure the calculation inside a and b are also cast to double. Right now you're trying to assign integers to a double a/b. Integers will do that to ya Wink.

-(BOOL) isEqualTo: (Fraction *) f
{
     double a = (double) self.numerator / f.numerator;
     double b = (double) self.denominator / f.denominator;
   
    if (a == b)
        return YES;
    else
        return NO;
}

There you go.

Q
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.