Welcome, Guest. Please login or register.
Did you miss your activation email?
May 27, 2017, 11:31:47 PM
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
| |-+ Chapter 4
| | |-+ Very confused about exponents. 4.4 / 4.5
Pages: [1] Go Down
Author Topic: Very confused about exponents. 4.4 / 4.5 (Read 3611 times)
Posts: 2

on: March 13, 2012, 12:10:27 PM


    I'm working through  exercises 4.4 and 4.5 and I'm a little confused about the way exponents work. In 4.4 it states

(to calculate the exponents, just do straight multiplication-there is no exponentiation operator in Objective-C)

Fair enough, so I used a command similar to 3(x*x*x) etc.

However, immediately afterward in 4.5 it says:

(remember to use exponential format to display the result)

This confused the heck out of me.

So I flipped back through the entire chapter and found absolutely nothing on using exponential format whatsoever. I'm uncertain how the book expects me to complete this exercise without understanding what it's asking.

Did I just miss something? If I didn't, what am I supposed to be doing? I see a lot of posts showing functions such as:


or even people suggesting 3.31*10^-8+2.01

But I get errors using both of those, and it doesn't help much that I don't know what they're supposed to be, or the context they should be used in.

I'm a little confused, and not sure if I am explaining my issue correctly; but any help would be appreciated.

Thank you very much.
Posts: 2

Reply #1 on: March 13, 2012, 12:32:17 PM

Just for the record; I ended up using a program that looks like this.

        float r1 = 3.31*10e-8+2.01*10*-10*-10*-10*-10*-10*-10*-10;
        float r2 = 7.16*10*-10*-10*-10*-10*-10*-10+2.01*10*-10*-10*-10*-10*-10*-10*-10*-10;
        float sum = r1 / r2;
        NSLog(@"The total of these 2 ridiculous numbers is %f", sum);

I know it at least isn't displayed in the format required by the exercise, but it's the best I could do with what I'd been given.
Posts: 9

Reply #2 on: March 28, 2012, 08:10:41 AM

I'm not sure, but try using %e instead of %f in your NSLog statement. I got that from Table 4.1, and I'm assuming it's for displaying in exponential format. Not sure, though Smiley
Posts: 1

Reply #3 on: April 23, 2012, 08:31:56 PM

I'm not sure, but try using %e instead of %f in your NSLog statement. I got that from Table 4.1, and I'm assuming it's for displaying in exponential format. Not sure, though Smiley

I used %e too. 

Another thing I looked up and did a bit different was the exponent math, figured there had to be a better way then doing 10*10*10 over and over.   Searching around  a a bit I found that you can do powers in the following fashion.

pow(x, y) which creates x^y.

For problem 5 I made three calculations to cover 10^-8, 10^-7 and 10^-6 with:
exponent1 = pow(10, -8);
exponent2 = pow(10, -7);
exponent3 = pow(10, -6);
Then I just plug those into the expression given in place of the exponents for a neater line of code!
Posts: 9

Reply #4 on: April 25, 2012, 10:49:46 PM

You can use the "e" notation to represent numbers, similar to the E button (not e, but E) on a scientific calculator.

Code: (Objective-C)
        float number1 = 3.31e-8;

this would set number1 = 3.31 * 10^(-8).

The e in the float represents a "times ten to the power of."

This means something like number = 4e5 would represent 4*10^5.

Hope this helps clarify some confusion on cleaning up some of the computation in problem 4.5.

The above post is correct to use %e (instead of %g) in your NSLog to have your answer returned to you in scientific notation (exponential format.)

Posts: 1

Reply #5 on: July 18, 2012, 11:06:57 PM

Thanks for the input everyone! I've been on this problem all day trying to figure out if I missed something from the first. This is a great help!!
Jr. Member
Posts: 83

WWW Email
Reply #6 on: August 14, 2012, 12:27:21 PM

        NSLog(@"Result : %.2f",(3.31e-08 + 2.01e-07) / (7.16e-06 + 2.01e-08));

        Result : 0.03

        NSLog(@"Result : %e",(3.31e-08 + 2.01e-07) / (7.16e-06 + 2.01e-08));
        Result : 3.260400e-02

        10-8 = 1.0e-08
        3.1 * 10-8 = 3.1e-8

Annonce Tunisie

Posts: 17

Reply #7 on: February 22, 2013, 01:09:57 PM

If you use the pow function you must add #include <math.h> otherwise it won't work. You can also create a method which has a for loop. The method has two inputs, original value and the power value and return the answer. Below is a program listing of using both the pow function and the for loop.

#import <Foundation/Foundation.h>
#import <math.h>

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

    @autoreleasepool {
        int answr = 0;
        int value = 5, pwr = 3;
        answr = pow(value, pwr);
        printf("%i raised to the power of %i is %d.\n", value, pwr, answr);
        answr = 5;
        for(int i = 1; i < pwr; i++)
            answr *= value;
        printf("The answer looping is %i.\n", answr);
    return 0;

5 raised to the power of 3 is 125.
The answer looping is 125.
Global Moderator
Full Member
Posts: 131

Reply #8 on: February 24, 2013, 03:59:37 AM

pow() works also without explicitly including math.h. That and a lot of other libraries are already included by Foundation.h. I found pow() with the rest of math.h to be declared in a file called fp.h (double click on the function pow() and the Quick Help Inspector (at the right hand side of XCode) will show where and how it is declared, and the associated man pages and/or other documentation)

I am not a specialist in libraries, so maybe I misunderstand something. But in my XCode pow() works without including more than just Foundation.h

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