Amazon.com Widgets Question about %lf vs %Lf
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 20, 2014, 08:45:23 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
| |-+ Chapter 6
| | |-+ Question about %lf vs %Lf
Pages: [1] Go Down
Print
Author Topic: Question about %lf vs %Lf (Read 3091 times)
Trizney
Newbie
*
Posts: 8


Email




on: February 24, 2012, 12:47:16 AM

I am looking at Program 6.8. On page 111, the book reads
Quote
A double value can be read in with the %lf format characters

Looking back at the Basic Data Types chart on page 55, the book reads that:

Quote
TypeNSLog chars
float%f, %e, %g, %a
double%f, %e, %g, %a
long double%Lf, %Le, %Lg

So what is the difference between the two, and do you always have to use %lf when using a double?

-Trey
Logged
seerex
Full Member
***
Posts: 177


Email




Reply #1 on: February 24, 2012, 07:25:58 AM

The %lf is short for "long float" i believe. Now, a double value is the same as a float, although with roughly twice the range. That being said, when you declare the format character %lf (long float), you reserve enough space for a long float, and that covers a double value as well, or at least most of it.

and u don't HAVE to use the %lf to display double values. I mainly use %g, but on the other hand i rarely use double values, as float provides enough range for me.

Hope i helped :=)
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
Trizney
Newbie
*
Posts: 8


Email




Reply #2 on: February 27, 2012, 03:04:56 AM

seerex,

Looking back now on my question, I should have been more clear. I was having a problem with scanf, and the %lf and %LF for doubles was confusing me.

I am now working on Exercise 6.4. I set a double, and try to scanf with %g. Doing so, it will not bring the double properly into the Object. When I would call the method print, it would always show a value of 0. If I change the %g to %lf, it works fine, and will show the proper number entered.

The only thing that makes sense to me is that scanf is a C-Style string, and the list on page 55 is for NSLog which is not. So the only way to read in a double with scanf is %lf.

I wish the book would have explained this a little more, instead of the very small mention on page 111. Maybe even have another chart with all the scaf characters.

I did find this explanation on stackoverflow
Quote
The scanf() format for double is "%lf".

printf() can use the same "%f" format for both float and double, because float arguments to variadic functions are promoted to double. There's no such promotion for scanf() because you're passing a pointer, and you need to have an actual float or double object in memory.


My code
Code: (Objective-C)
//
//  main.m
//  Exercise 6.4
//
//  Print Calculator


//  Implement a Calculator Class

#import <Foundation/Foundation.h>

@interface Calculator: NSObject

//  accumulator methods
-(void) setAccumulator: (double) value;
-(double) print;

//  arithmetic methods
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
@end

@implementation Calculator
{
    double accumulator;
}

-(void) setAccumulator: (double) value
{
    accumulator = value;
}

-(double) print
{
    NSLog (@"Contents of Accumulator");
    return accumulator;
}

-(void) add: (double) value
{
    accumulator += value;
}

-(void) subtract: (double) value
{
    accumulator -= value;
}

-(void) multiply: (double) value
{
    accumulator *= value;
}

-(void) divide: (double) value
{
    if ( value != 0 )
        accumulator /= value;
    else {
        NSLog (@"Division by zero.");
    }
}


@end

int main (int argc, const char * argv[])
{
   
    @autoreleasepool {
       
        double value1;
        char operator;
        bool end;
       
        Calculator *deskCalc = [[Calculator alloc] init];
       
        NSLog (@"Begin Calculations");       
       
        while (end != YES) {
           
            NSLog (@"Type in your number and operator. Type O E to exit.");
            scanf ("%lf %c", &value1, &operator);
           
            if ( operator == '+' )
                [deskCalc add: value1];
            else if ( operator == '-' )
                [deskCalc subtract: value1];
            else if ( operator == '*' )
                [deskCalc multiply: value1];
            else if ( operator == '/' )
                [deskCalc divide: value1];
            else if ( operator == 'S' || operator == 's')
                [deskCalc setAccumulator: value1];
            else if ( operator == 'E' || operator == 'e') {
                NSLog (@"End of Program");
                end = YES;
            }
            else
                NSLog (@"Unknown operator");
           
            NSLog(@"%.2f", [deskCalc print]);
        }
       
        NSLog (@"End of Calculations");
       
    }
    return 0;
}


Logged
CMM83
Newbie
*
Posts: 5


Email




Reply #3 on: April 27, 2012, 07:10:42 AM

I just came across this today when working exercise 4.  I was using %f in scanf and it would not bring the value to object (accumulator for calculator exercise).  I switched to %lf and all was well.

Your explanation makes sense.  I was especially confused because in the exercise statement is specifically references using %f .... "Remember to use a space character in your scanf format string (e.g., "%f %c") to skip whitespace characters in the input."
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.