4

I am looking for a possibility within C/C++ to print a float (or double) f, say f = 1.234e-15, such that it is printed as

  • f = 1.234*10^-15, or, even better, as
  • f = 1.234*10^{-15}

Can anyone help me? Maybe there is a way to get the exponent "-15" and mantissa "1.234" in base 10. I found the question how can I extract the mantissa of a double, but unfortunately that did not really help, since it only gets the mantissa in base 2.

Community
  • 1
  • 1
alex
  • 2,252
  • 4
  • 23
  • 34

4 Answers4

8

You can print to a string using the output string stream, and then replace "e" with "*10^".

ostringstream ss;
ss << scientific << 123456789.87654321;
string s = ss.str();
s.replace(s.find("e"), 1, "*10^");
cout << s << endl;

This snippet produces

1.234568*10^+08
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Thanks a lot! I did not know the string methods replace() and find(). Thought I will need to use some regular expression library to replace the 'e' and that seemed to complicated. Your solution is really short, which I like. While waiting for replies I actually wrote my own code, see below... – alex Jun 16 '12 at 09:10
2

Why not use string parsing? scan the string and replace e with 10^.

edison
  • 56
  • 1
  • 3
2
#include <cmath>
#include <iostream>

using namespace std;

template <typename F>
F round_away_from_zero (F x)
{
  return x < 0 ? floor(x) : ceil(x);
}

template <class O, typename F>
O &print_float (O &out, F f) {
    signed ex = round_away_from_zero(log10(f)); // exponent
    F mant = f / pow(10, ex);                   // mantissa
    out << mant << "*10^" << ex;
}

int main () {
    double f = 1.234e-15;
    print_float(cout, f) << endl; // prints 1.234*10^-15
    return 0;
}
Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • 1
    Thanks! Also nice mathematical way to get exponent and mantissa! And easy to include my brace brackets into the output. And now I also know floor() and ceil() :)) – alex Jun 16 '12 at 09:25
  • Just discovered a small error in your code: In print_float() you have to take the log10 of abs(f), otherwise the result will be infinite for negative f. But with this small change it also works for negative numbers. – alex Jun 16 '12 at 10:08
0

While waiting for your solutions I came up with the following idea:

Use sprintf() to print the float or double to an array of char. Parse this to get the exponent and mantissa. Code looks now the following way:

void printDoubleToChar(double d, char* c){
    char valAsChar[256];

    sprintf(valAsChar, "%.12e", d);

    int expStart = 0, expWidth = 0;
    for(int i=0; i<sizeof(valAsChar); i++){
        if(valAsChar[i] == 'e'){
            expStart = i+1;
            break;
        }
    }
    for(int i=expStart; i<sizeof(valAsChar); i++){
        if(valAsChar[i] == '\0'){
            expWidth = i - expStart;
            break;
        }
    }

    char chValExp[32];
    memcpy(chValExp, &valAsChar[expStart], expWidth+1);

    char chValMan[128];
    memcpy(chValMan, valAsChar, expStart-1);
    chValMan[expStart-1] = '\0';

    sprintf(c, "%s*10^{%s}", chValMan, chValExp);
}

int main(){
    double myNewDbl = 3.95743e-5;
    char chDbl[256];
    printDoubleToChar(myNewDbl, chDbl);
    printf("\nchDbl: %s\n", chDbl); // output: chDbl: 3.957430000000*10^{-05}
}

But honestly, I prefer the much simpler solution by dasblinkenlight :)

Thank you all for your help!

Alex

alex
  • 2,252
  • 4
  • 23
  • 34