Amazon.com Widgets Question about Ex. 4.5...
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 30, 2014, 09:39:28 AM
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
| |-+ Exercises
| | |-+ Chapter 4
| | | |-+ Question about Ex. 4.5...
Pages: [1] Go Down
Print
Author Topic: Question about Ex. 4.5... (Read 1664 times)
ljaygould
Newbie
*
Posts: 16






on: January 02, 2012, 10:00:39 AM

I have the 4th Edition (Kindle) and did a search for the words "exponential format" and the ONLY time it's mentioned is in this exercise. Can someone post for this newbie to Objective-C an example of how to write exponential format? Is there a way to indicate in a NSLog statement the super and subscript characters? OR is there an alternative kind of notation that's standard? Thanks...

Jay
Logged
hchclement
Newbie
*
Posts: 1


Email




Reply #1 on: January 05, 2012, 01:48:47 AM

I use the caret, like so:

Code: (Objective-C)
NSLog(@"The result of (3.31 * 10^-8 + 2.01 * 10^-7) / (7.16 * 10^-6 + 2.01 * 10^-8) is %f", 
          result);

According to a thread i found on CocoaDev, "NSLog is a FoundationKit function for printing debug statements to the console." So it isn't really intended to support fancier formatting. It's plaintext.

Hope this helps.

Helen
Logged
jgelling
Newbie
*
Posts: 27






Reply #2 on: January 05, 2012, 02:33:19 AM

Well NSLog will display whatever you tell it to, but if you want to work with numbers in scientific notation use e to whatever power. Here's an example:

        float n = 7e4;  //this is 7 x 10^4
        NSLog(@"%e", n);

//This displays 7.000000e4

%e will display floating point numbers in scientific notation in NSLog - the format for storing numbers in scientific notation is <mantissa>e<power>. You can limit the accuracy of the decimal points (say, to 2 digits) by specifying the token %.[desired precision]e, so for 2 decimal places %.2e in NSLog:

        float n = 7e4;  //this is 7 x 10^4
        NSLog(@"%.2e", n);

//This displays 7.00e4
Last Edit: January 05, 2012, 02:41:05 AM by jgelling Logged
ljaygould
Newbie
*
Posts: 16






Reply #3 on: January 10, 2012, 11:47:13 AM

Thanks so much for the feedback & support, it's very helpful for answering the exercise. I just have a problem when my conception is that of a beginner, and therefore somewhat shaky, when the author asks for something that seems to have come from left field. The fact that there are two ways (7^4 in the first example and 7e4 in the second) to notate, NEITHER of which is discussed in the chapter before the exercise, seems to bear this out.

I am enjoying working through this book and feel he's done a great job for those of us with limited programming background...but this is one where I hope he takes a look and either removes the exponential-notation requirement in a subsequent edition, or prefaces it with a description of how HE recommends doing it, introducing it earlier in the chapter by name and laying groundwork for it). The only place I see a %e is in the table on P. 55, and its use is not broken out from the other float NSLog chars. Let me know if I'm blind...and again thanks for answering.
Logged
jgelling
Newbie
*
Posts: 27






Reply #4 on: January 10, 2012, 12:21:44 PM

Yeah, I remember being very confused by the myriad of tokens used by NSLog too. Coming from a Perl background, it was entirely alien to me that I couldn't just do in-line string substitution of any type of variable in standard output. You don't even have to declare their type. Perl just figures out what to put where if you print out a line with a bunch of variables interspersed.

In Objective C, you have to specify exactly what type of variable is being used, and use a token placeholder for that substitution, and there are, as you said a lot of different tokens for every possible type of variable under the sun. If you get it wrong, you crash your program. At least there *is* a chart of the dozens of different tokens, however, which is much better than I've seen in a couple of other books I've read.
Logged
jero
Newbie
*
Posts: 1






Reply #5 on: March 11, 2012, 11:19:23 AM

Hi ljaygould

It may be a typo, but just be careful: 7^4 and 7e4 are not the same numbers.
7^4 = 7*7*7*7
7e4 = 7 * 10^4 = 70000

Have a good day,
Andras
Logged
Pages: [1] Go Up
Print
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.