Thread: Scientific notation

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    207

    Scientific notation

    Hi! I'm using LONG DOUBLE data types & I get scientific notation starting at a million.

    Is there any way to "force" the program to either:

    a) DISPLAY it as a regular number
    b) CHANGE it to a regular number

    Also, I would imagine that there is a loss in precision with scientific notation. Is there a way to calculate numbers on that scale (maybe up to 1E+36) without any loss in precision using C?

    Thanks!

    mw

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you can tweak the width and precision to get more significant digits out
    Code:
    #include <stdio.h>
    
    int main ( void ) {
      double d = 12345678901.2345;
      printf( "%15.30f\n", d );
      return 0;
    }
    > Is there a way to calculate numbers on that scale (maybe up to 1E+36) without any loss in precision using C?
    In my math.h, I have
    #define LDBL_DIG 18
    Basically, long doubles can store 18 decimal digits accurately.

    Use a math library like this if you want more digits http://www.swox.com/gmp/
    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
    May 2005
    Posts
    207
    Uh, I don't understand that "%15.30f" in your printf statement. Can you clarify?

    Also, is there a way to display it without a decimal?

    Thanks!

    mw

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Lionmane
    Uh, I don't understand that "%15.30f" in your printf statement. Can you clarify?

    Also, is there a way to display it without a decimal?

    Thanks!

    mw
    It's just a extra info how the number is formated.
    that means: put up to 15 digits on the left side of the point, and up to 30 digit on the right side. something like
    000000000000000.000000000000000000000000000000
    trailing zeros are disposed.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    207
    If I change the printf statement to this:

    printf ("%15f\n", d);

    It still prints 6 digits on the right side of the decimal. Is there a way to print any number of digits to the left side of the decimal & none on the right?

    Thanks!

    mw

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by xErath
    It's just a extra info how the number is formated.
    that means: put up to 15 digits on the left side of the point, and up to 30 digit on the right side. something like
    000000000000000.000000000000000000000000000000
    trailing zeros are disposed.
    Not exactly. It means, "use at least 15 characters to represent the whole shebang, but use 30 digits after the decimal point".
    Quote Originally Posted by Lionmane
    Is there a way to print any number of digits to the left side of the decimal & none on the right?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       double d = 12345678901.2345;
       printf("d = %45.30f\n", d);
       printf("d = %.0f\n", d);
       return 0;
    }
    
    /* my output
    d =     12345678901.23450088000000000000000000000
    d = 12345678901
    */
    [edit]
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       double d = 12345678901.2345;
       int any_number = 40;
       printf("d = '%45.30f'\n", d);
       printf("d = '%*.0f'\n", any_number, d);
       return 0;
    }
    
    /* my output
    d = '    12345678901.23450088000000000000000000000'
    d = '                             12345678901'
    */
    Last edited by Dave_Sinkula; 06-01-2005 at 10:01 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    207
    I hate to beat a dead horse here, but when I use this:

    printf ("%45.0f\n", d);

    It prints 45 spaces to the left of the decimal whether there are 45 digits or not. Is there a way to truncate the extra spaces?

    mw

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Reread the second output of the first code fragment of my previous reply.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    207
    Heck yeah! Thanks, man!

    mw

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Dave_Sinkula
    Not exactly. It means, "use at least 15 characters to represent the whole shebang, but use 30 digits after the decimal point".
    Yes whatever... tricky english

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scientific notation
    By manzoor in forum C++ Programming
    Replies: 6
    Last Post: 11-13-2007, 09:46 AM
  2. Formating Scientific Notation Output
    By RedSoxFan in forum C Programming
    Replies: 2
    Last Post: 12-21-2005, 10:38 AM
  3. Reading scientific notation
    By PunkyBunny300 in forum C Programming
    Replies: 1
    Last Post: 04-24-2003, 09:22 PM
  4. Scientific Notation
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 03:08 PM