September 21, 2017, 04:02:05 PM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Read this please.
♦
The Great Kangaroo Escape
♦
Looking for reviews of the 4th ed on Amazon!
♦
Twitter:
@skochan
Home
Help
Search
Login
Register
Chat
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
« previous
next »
Print
Author
Topic: Question about Ex. 4.5... (Read 2718 times)
ljaygould
Newbie
Posts: 16
Question about Ex. 4.5...
«
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
Re: Question about Ex. 4.5...
«
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
Re: Question about Ex. 4.5...
«
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
Re: Question about Ex. 4.5...
«
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
Re: Question about Ex. 4.5...
«
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
Re: Question about Ex. 4.5...
«
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
« previous
next »
Jump to:
Please select a destination:
-----------------------------
Programming in Objective-C, 5th edition
-----------------------------
=> General Discussions
=> Exercises
-----------------------------
Programming in Objective-C, 4th edition
-----------------------------
=> General Discussion
=> Exercises
===> Chapter 2
===> Chapter 3
===> Chapter 4
===> Chapter 5
===> Chapter 6
===> Chapter 7
===> Chapter 8
===> Chapter 9
===> Chapter 10
===> Chapter 11
===> Chapter 12
===> Chapter 13
===> Chapter 14
===> Chapter 15
===> Chapter 16
===> Chapter 17
===> Chapter 18
===> Chapter 19
===> Chapter 20
===> Chapter 21
=> Chapter 2
=> Chapter 3
=> Chapter 4
=> Chapter 5
=> Chapter 6
=> Chapter 7
=> Chapter 8
=> Chapter 9
=> Chapter 10
=> Chapter 11
=> Chapter 12
=> Chapter 13
=> Chapter 14
=> Chapter 15
=> Chapter 16
=> Chapter 17
=> Chapter 18
=> Chapter 19
=> Chapter 20
=> Chapter 21
=> Errata
===> 1st Printing
===> 2nd Printing
-----------------------------
Members' Corner
-----------------------------
=> Look What I'm Doing...
-----------------------------
Study Group
-----------------------------
=> Objective-C Quizzes
===> Comprehensive Quiz
===> Chapter 1 - Introduction
===> Chapter 2 - Programming in Objective-C
===> Chapter 3 - Classes, Objects, and Methods
===> Chapter 4 - Data Types and Expressions
===> Chapter 5 - Program Looping
===> Chapter 6 - Making Decisions
===> Chapter 7 - More on Classes
===> Chapter 8 - Inheritance
===> Chapter 9 - Polymorphism, Dynamic Typing, and Dynamic Binding
===> Chapter 10 - More on Variables and Data Types
===> Chapter 11 - Categories and Protocols
===> Chapter 12 - The Preprocessor
===> Chapter 15 - Numbers, Strings & Collections
===> Chapter 17 - Memory Management
-----------------------------
iOS Programming
-----------------------------
=> General Questions
-----------------------------
Programming in C, 3rd ed. (Stephen Kochan)
-----------------------------
=> Programming in C webcasts?
=> Answers to Odd-Numbered Exercises
=> Questions and Help
-----------------------------
Old Stuff
-----------------------------
=> Errata, Answers to Exercises, Code, etc.
=> Shared Code Library
=> Welcome
=> News
=> Errata
===> 1st Printing
===> 2nd printing
=> Newbie Corner
=> Chapter Study
===> Chapter 2 - Programming in Objective-C
===> Chapter 3 - Classes, Objects, and Methods
===> Chapter 4 - Data Types and Expressions
===> Chapter 5 - Program Looping
===> Chapter 6 - Making Decisions
===> Chapter 7 - More on Classes
===> Chapter 8 - Inheritance
===> Chapter 9 - Polymorphism, Dynamic Typing, and Dynamic Binding
===> Chapter 10 - More on Variables and Data Types
===> Chapter 11 - Categories and Protocols
===> Chapter 12 - The Preprocessor
===> Chapter 13 - Underlying C Language Features
===> Chapter 15 - Numbers, Strings, and Collections
===> Chapter 16 - Working with Files
===> Chapter 17 - Memory Management
===> Chapter 18 - Copying Objects
===> Chapter 19 - Archiving
===> Chapter 21 - Writing iPhone Applications
=> Answers to Exercises
===> Chapter 2
===> Chapter 3
===> Chapter 4
===> Chapter 5
===> Chapter 6
===> Chapter 7
===> Chapter 8
===> Chapter 9
===> Chapter 10
===> Chapter 11
===> Chapter 12
===> Chapter 13
===> Chapter 15
===> Chapter 16
===> Chapter 17
===> Chapter 18
===> Chapter 19
===> Chapter 21
=> General Discussion
=> Program Examples
===> Chapter 2
===> Chapter 3
===> Chapter 4
===> Chapter 5
===> Chapter 6
===> Chapter 7
===> Chapter 8
===> Chapter 9
===> Chapter 10
===> Chapter 11
===> Chapter 12
===> Chapter 13
===> Chapter 15
===> Chapter 16
===> Chapter 17
===> Chapter 18
===> Chapter 19
===> Chapter 21
=> XCode 4 Help
=> Suggestions
=> Help!!!
=> Fraction Calculator Available from the App Store
=> Resources
=> Errata
===> 1st & 2nd printings
===> 3rd & 4th printings
===> 5th printing
===> Current Errata
=> Suggestion Box
=> Tips & Tricks
=> Part 3
===> Welcome to the Class
===> Special Announcements
===> Assignments
=> Parts 1 and 2
===> Welcome to the Class
===> Questions and Discussion
===> SPECIAL ANNOUNCEMENTS
===> Assignments
===> Chapter 4, Exercise 7
===> Suggestions
=> Webcast Series Part I, Sept. 1 - 24
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Questions and Discussion
===> Assignments
===> Chapter 4, Exercise 7
===> Chapter 6, Exercise 4
===> Suggestions
=> Webcast Series Part II, Oct. 1 - 27
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Questions and Discussion
===> Assignments
===> Example using hasPrefix: and hasSufix:
===> Suggestions
=> Objective-C Workshop Aug 11 - 13
=> Webcast Series Part I, Oct. 29 - Dec. 1
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Assignments
===> Chapter 4, Exercise 7
===> Chapter 6, Exercise 4
===> Suggestions
=> Webcast Series Part II, Jan 5 - 28
===> Welcome
===> SPECIAL ANNOUNCEMENTS
===> Assignments
===> Jan 7th Solutions
Loading...