Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
May 23, 2018, 09:22:46 AM *
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: Subtract method shows negative sign in denominator  (Read 1579 times)
StrayCatSgp
Newbie
*
Posts: 4


Email
« on: October 20, 2012, 11:05:20 PM »

I programmed program 11.1

and I get following result:

2012-10-21 13:56:38.479 Program 11.1[6071:303] 1 / 3
2012-10-21 13:56:38.482 Program 11.1[6071:303]  +
2012-10-21 13:56:38.483 Program 11.1[6071:303] 2 / 5
2012-10-21 13:56:38.484 Program 11.1[6071:303] ------
2012-10-21 13:56:38.485 Program 11.1[6071:303] 11 / 15
2012-10-21 13:56:38.485 Program 11.1[6071:303]
2012-10-21 13:56:38.486 Program 11.1[6071:303] 1 / 3
2012-10-21 13:56:38.486 Program 11.1[6071:303]  -
2012-10-21 13:56:38.487 Program 11.1[6071:303] 2 / 5
2012-10-21 13:56:38.487 Program 11.1[6071:303] ------
2012-10-21 13:56:38.488 Program 11.1[6071:303] 1 / -15
2012-10-21 13:56:38.488 Program 11.1[6071:303]
2012-10-21 13:56:38.489 Program 11.1[6071:303] 1 / 3
2012-10-21 13:56:38.489 Program 11.1[6071:303]  *
2012-10-21 13:56:38.489 Program 11.1[6071:303] 2 / 5
2012-10-21 13:56:38.490 Program 11.1[6071:303] ------
2012-10-21 13:56:38.490 Program 11.1[6071:303] 2 / 15
2012-10-21 13:56:38.491 Program 11.1[6071:303]
2012-10-21 13:56:38.492 Program 11.1[6071:303] 1 / 3
2012-10-21 13:56:38.493 Program 11.1[6071:303]  /
2012-10-21 13:56:38.494 Program 11.1[6071:303] 2 / 5
2012-10-21 13:56:38.495 Program 11.1[6071:303] ------
2012-10-21 13:56:38.546 Program 11.1[6071:303] 5 / 6
2012-10-21 13:56:38.559 Program 11.1[6071:303]

Why does the "subtract" method show 1/-15 as a result instead of -1/15?
here's my main.m :

#import <Foundation/Foundation.h>
#import "Fraction.h"


@interface Fraction (MathOps)
-(Fraction *)  add:  (Fraction *) f;
-(Fraction *)  subtract:  (Fraction *) f;
-(Fraction *)  multiply:  (Fraction *) f;
-(Fraction *)  divide:  (Fraction *) f;
@end


@implementation Fraction (MathOps)


-(Fraction *)  add:(Fraction *)f
{
   //to add two fractions:
   // a/b + c/d = ((a*d) + (b*c))/(b * d);
   
   Fraction *result = [[Fraction alloc] init];
   
   result.numerator = (self.numerator * f.denominator) + (self.denominator * f.numerator);
   result.denominator = self.denominator * f.denominator;
   [result reduce];
   
   return result;
   
}

-(Fraction *)  subtract:(Fraction *)f
{
   //to subcttra two fractions:
   // a/b - c/d = ((a*d) - (b*c))/(b * d);
   
   Fraction *result = [[Fraction alloc] init];
   
   result.numerator = (self.numerator * f.denominator) - (self.denominator * f.numerator);
   result.denominator = self.denominator * f.denominator;
   [result reduce];
   
   return result;
   
}

-(Fraction *)  multiply:(Fraction *)f
{
   //to multiply two fractions:
   // a/b * c/d = ((a * c)/(b * d);
   
   Fraction *result = [[Fraction alloc] init];
   
   result.numerator = (self.numerator * f.numerator);
   result.denominator = self.denominator * f.denominator;
   [result reduce];
   
   return result;
   
}


-(Fraction *)  divide:(Fraction *)f
{
   //to divide two fractions:
   // a/b / c/d = ((a * d)/(b * c);
   
   Fraction *result = [[Fraction alloc] init];
   
   result.numerator = (self.numerator * f.denominator);
   result.denominator = (self.denominator * f.numerator);
   [result reduce];
   
   return result;
   
}
@end


int main(int argc, const char * argv[])
{

   @autoreleasepool {
       
      Fraction *a = [[Fraction alloc] init];
      Fraction *b = [[Fraction alloc] init];
     
      Fraction *result;
     
      [a setTo:1 over:3];
      [b setTo:2 over:5];
     
      [a print]; NSLog(@" + "); [b print]; NSLog(@"------");
      result = [a add: b];
      [result print];
      NSLog(@"\n");
     
      [a print]; NSLog(@" - "); [b print]; NSLog(@"------");
      result = [a subtract: b];
      [result print];
      NSLog(@"\n");
     
      [a print]; NSLog(@" * "); [b print]; NSLog(@"------");
      result = [a multiply: b];
      [result print];
      NSLog(@"\n");
     
      [a print]; NSLog(@" / "); [b print]; NSLog(@"------");
      result = [a divide: b];
      [result print];
      NSLog(@"\n");

   }
    return 0;
}
Logged
afterDark
Global Moderator
Full Member
*****
Posts: 131


« Reply #1 on: January 17, 2013, 03:26:22 PM »

After seeing your post I vaguely remembered I have stumbled in this same problem. I found where this happened to me, maybe it helps you too.
It might happen in your reduce method (the code for your reduce method you did not show so I am not sure). The  suggested reduce method in chapter 7 is

Code: (Objective-C)
-(void) reduce
{
    int u, v, temp;
    u = numerator;
    v = denominator;
   
    while (v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }
numerator /= u;
denominator /= u;
}

in case of a negative value for the numerator, that makes a negative u, which makes the numerator postive and denominator negative in the two divisions at the end. Going back to 7.4 I see I did this to solve that (sorry for the dutch comment lines, so I learned today to always make english comments also in private small projects...):

Code: (Objective-C)
-(void) reduce
{
    int u, v, temp;
    u = numerator;
    v = denominator;
   
    while (v != 0) {
        temp = u % v;
        u = v;
        v = temp;
    }
   
    //bij negatieve breuken kan u ook negatief worden. Als u
    //negatief is zou van zowel numerator als denominator het teken
    //omdraaien, resulterende in -3/5  >>  3/-5
    if (u<0)
        u = -u;
   
    numerator /= u;
    denominator /= u;
}

Hope that helped!
Logged

I am just an amateur with Objective-C, don't let the moderator label fool you. Working my way through the book slowly.
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.