Thread: math function

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    11

    math function


    Hi,

    anyone know of a function in the standard library that will expand a scientific number ?


    for example,

    change 1.503149e-03 to 0.00153149
    which is the source to the power of -3


    tia,
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Er.... there's no conversion necessary as the number remains the same -- the only difference is the way you write it out. Do you mean that you want to print it out in a different format? If so, then look at using cout.setf():
    Code:
    double foo = 0.00153149;
    cout.precision(10); // Show up to 10 decimal places
    cout.setf(ios::fixed, ios::floatfield);
    cout << foo << endl; // print as 0.00153149 
    cout.setf(ios::scientific, ios::floatfield);
    cout << foo << endl; // print as 1.503149e-03
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    11
    Hi,

    Sorry, two points I failed to mention, it has to be C and 1.503149e-03 is currently in a delimited text file that I am reading and will eventually write out to a new delimited text file expanded to 0.00153149

    so now I need to look at fputs() to see how I can format it


    Thanks Scorpions4ever
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by cplusplos
    Hi,

    Sorry, two points I failed to mention, it has to be C and 1.503149e-03 is currently in a delimited text file that I am reading and will eventually write out to a new delimited text file expanded to 0.00153149

    so now I need to look at fputs() to see how I can format it


    Thanks Scorpions4ever
    fputs() will not format for you. Create a string first and then fputs it.

    Do the number formatting with sprintf. Here's a test program I ran:
    Code:
    C:\dcw\PROJECTS\TEST>cat floats.c
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        double num = 1.503149e-03;
        char  buffer[20];
    
        sprintf(buffer,"Num == %1.9f",num);
        puts(buffer);
    
        return 0;
    }
    
    
    C:\dcw\PROJECTS\TEST>a
    Num == 0.001503149
    
    C:\dcw\PROJECTS\TEST>
    OBTW, you have a typo in your message. 1.503149e-03 would be rewritten as 0.001503149, not as 0.00153149. You accidentally dropped the zero between the five and three.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    107
    Rep Power
    11
    Thanks dwise1_aol, thats excellent

    and I had to look twice at the typo beforeI saw it :)

IMN logo majestic logo threadwatch logo seochat tools logo