i am new to objective C and I started to learn the basics in iOS development.
How can I easily calculate with fractions in Objective C?
Example 1: 1/8000 * 60
Example 2: 1/640 / 1/400
I need to have the result (if its < 1) as a fraction, too.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
I think I would implement a new class, say
Fractionwhich has two properties, the numerator and denominator. I would then implement four (or more if necessary) of the following functions:In these methods, do the usual arithmetic you learned in school. Remember to simplify if both numerator and denominator has common divisors.
EDIT: Example implementation for one of these functions:
Optionally you could add a
reduceorsimplifymethod and possibly anallowReducibleproperty and anisIrreduciblemethod. You could also implementisEqualandcompareTomethods. Adescriptionshould just print “a/b” as anNSString.