Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 684315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:48:11+00:00 2026-05-14T01:48:11+00:00

I’ve been coding my way through Steve Kochan’s Programming in Objective-C 2.0 book. I’m

  • 0

I’ve been coding my way through Steve Kochan’s Programming in Objective-C 2.0 book. I’m up to an exercise in chapter 7, ex 4, in case anyone has the book.

The question posed by the exercise it will the Fraction class written work with negative fractions such as -1/2 + -2/3?

Here’s the implementation code in question –

@implementation Fraction

@synthesize numerator, denominator;

-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}

-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}

-(double) convertToNum
{
if (denominator != 0)
    return (double) numerator / denominator;
else
    return 1.0;
}

-(Fraction *) add: (Fraction *) f
{
// To add two fractions:
// a/b + c/d = ((a * d) + (b * c)) / (b * d)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = (numerator * f.denominator) + (denominator * f.numerator);
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
[result reduce];

return result;
}

-(Fraction *) subtract: (Fraction *) f
{
// To subtract two fractions:
// a/b - c/d = ((a * d) - (b * c)) / (b * d)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.denominator - denominator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
[result reduce];

return result;
}

-(Fraction *) multiply: (Fraction *) f
{
// To multiply two fractions
// a/b * c/d = (a*c) / (b*d)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.numerator;
resultDenom = denominator * f.denominator;

[result setTo: resultNum over: resultDenom];
[result reduce];

return result;
}

-(Fraction *) divide: (Fraction *) f
{
// To divide two fractions
// a/b / c/d = (a*d) / (b*c)

// result will store the result of the addition
Fraction *result = [[Fraction alloc] init];
int resultNum, resultDenom;

resultNum = numerator * f.denominator;
resultDenom = denominator * f.numerator;

[result setTo: resultNum over: resultDenom];
[result reduce];

return result;
}

-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;

while (v != 0) {
    temp = u % v;
    u = v;
    v = temp;
}

numerator /= u;
denominator /= u;
}

@end

My question to you is will it work with negative fractions and can you explain how you know? Part of the issue is I don’t know how to calculate negative fractions myself so I’m not too sure how to know.

Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:48:12+00:00Added an answer on May 14, 2026 at 1:48 am

    The comments in the code give the formulas. Since you don’t know them, it’s reasonable for you to trust the comments are correct.

    From plain common sense, you can also infer that the behavior of negative fractions doesn’t depend on their sign.

    For example, if you know how negative numbers work in general (think temperature for example), there is no reason for fractions to behave differently (it’s quite fine to say that a temperature is 160/3 for example). After all, a fraction is but a number.

    Now from my quick look at the code, I don’t see any case where this code fools with the sign of its data. It never takes the absolute value of anything for example. So my conclusion is that it should work with negative fractions just fine.

    Now if you have the option to actually run the code, you can check that on a few test cases.

    That being said, there is one thing that this code doesn’t handle: mathematically, a negative fraction is one where either the numerator or the denominator is negative (but not both). The convention is to prefer to the minus sign on the numerator. That code will not respect or enforce that convention.

    That doesn’t make the code wrong, though. However, this places a trap for future expansion. For example, suppose you expand that class to also compare two fractions for equality. The following naive implementation would be wrong:

    - (BOOL) isEqualTo:(Fraction*)f
    {
      [self reduce];
      [f reduce];
      BOOL b = (self.numerator == f.numerator) && (self.denominator == f.denominator);
      return b;
    }
    

    Such an implementation would wrongly claim that (-2)/3 is not equal to 2/(-3).

    I leave it to you to imagine a better implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer this should help you out: http://fajkowski.com/blog/2009/08/02/openflow-a-coverflow-api-replacement-for-the-iphone/ May 14, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer You can solve this 1:1 in two different ways: Add… May 14, 2026 at 10:28 pm
  • Editorial Team
    Editorial Team added an answer function sumIntegerUpTo(number) { return (1 + number) * number /… May 14, 2026 at 10:28 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.