Thread: Exponential Notation

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Philly
    Posts
    2

    Question Exponential Notation

    Hi all. I'm new to C so please forgive my stupidity.
    I'm trying to use the a quantity such as 3 * 10^-5. With a positive exponent, I wrote it as 3.0e+5 and it worked. If I try 3.0e-5 it does not. What am I doing wrong?
    Also, with printf and scanf, I know you use %d for integers, %f for floats, and %l for long if I'm not mistaken, but what about double?
    Thank you for your consideration.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > What am I doing wrong?
    Dunno, you need to post more information about what you did.

    This for example seems to work for me.
    Code:
    #include <stdio.h>
    
    int main() {
        double d;
        scanf("&#37;lf",&d);
        printf("d=%f\n", d);
        return 0;
    }
    
    $ gcc -W -Wall foo.c
    $ ./a.exe
    3.0e-5
    d=0.000030
    But if you were expecting to see 3.0e-5 printed, then you need to look up what the "%e" and "%g" formats also do. printf() has many ways of controlling how floating point numbers are printed.


    > but what about double?
    As shown, use "%f" for printing, and "%lf" for scanning.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    Philly
    Posts
    2
    Thank you SO much! The book I'm using left out some of the details. I appreciate your taking the time to help me out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. CamelCase VS Hungarian notation, which is better?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2007, 09:31 PM
  3. I need help with RPN notation!!!
    By schnoor22 in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 05:12 PM
  4. A doubt about changing notation
    By louis_mine in forum C Programming
    Replies: 7
    Last Post: 11-21-2004, 10:15 PM
  5. exponential operator
    By mikebrewsj in forum C++ Programming
    Replies: 2
    Last Post: 01-16-2002, 08:05 AM