Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 17, 2017, 07:10:34 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: Program 11.1 Page 228 (221 3rd edition)  (Read 2727 times)
PumaSpeedDrift
Newbie
*
Posts: 10


Email
« on: July 28, 2010, 09:49:08 AM »

I know I have an earlier version of the book, but could not find a correction, unless I am wrong in reading the example:

In the book, these two methods:

Code: (Objective-C)
-(Fraction *) mul: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
//*********

[result setTo: numerator * f.numerator  
over: denominator * f.denominator];

//********
[result reduce];
return result;
}
-(Fraction *) div: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
//**********

[result setTo: numerator * f.denominator              
over: denominator * f.numerator];

//*********
[result reduce];
return result;
}

Wouldn't they produce the same result since no math was really applied?
Should they have followed these formulas?

numerator = ((a*d) * (b*c)) / (b * d)   //For Multiply

and

 numerator = ((a*d) / (b*c)) / (b * d)  //For Division

since the output in the book shows otherwise?
« Last Edit: August 04, 2011, 08:54:16 AM by fujilla » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: August 02, 2010, 01:05:53 PM »

When you multiply two fractions you simply multiply the two numerators by each other and the two denominators by each other.  When you divide two fractions, it's the same as multiplying the first by the inverse of the second.

Cheers,

Steve Kochan
Logged
PumaSpeedDrift
Newbie
*
Posts: 10


Email
« Reply #2 on: August 09, 2010, 06:03:11 AM »

My bad, I didn't realize that was actually happening in the code, I was wrong.  I mis-read the code. ( I think I need to pay closer attention to what I am reading and seeing.)

Thank you 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.