Controlling precision after E in sci notation

This is a discussion on Controlling precision after E in sci notation within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to print a number but it's printing like this: 1.0000e+000 but I want to only show two ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    Controlling precision after E in sci notation

    Hi,

    I'm trying to print a number but it's printing like this:
    1.0000e+000
    but I want to only show two 0's after the e+, do you know of any manipulators or something I could use to restrict the numebr of digits?

    I'm printing it right now using:
    cout << setw(12) << right << scientific << setprecision(4) << F(i+1) << endl;

    thanks,

    Canadian

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    cplusplus.com states that three is the number of digits following the e, and the number of digits following the e shall be three. I can't find that language in the standard, but I also can't find anything that suggests it can be changed.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Okay, well if it's not possible, can anyone think of a way that I might be able to print a line e.g.
    3.8045e+00 5.3859e+34 5.3853e+53

    i.e. maybe is there a way where I can print columns of three values in this format by limiting the width given to each value so that the third digit after e+ gets cut off?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    setw only sets minimum width, not maximum. Your best bet may be something like this (tested, and similar to code I've posted before):
    Code:
    void print_with_two_exponents(std::ostream& os, float x, int precision) {
        double lg = log10(fabs(x));
        int exponent = static_cast<int>(floor(lg));
        double mantissa = pow(10,lg-exponent);
        if (x < 0) mantissa *= -1.0;
        os << std::fixed << std::setprecision(precision);
        os << mantissa << "e";
        os << std::setw(3) << std::showpos;
        os.setf(std::ios::internal, std::ios::adjustfield);
        os << std::setfill('0') << exponent;
    }
    Admittedly, this doesn't handle NaN's very well, but you can do what you want with those. You may also want to reset some of the flags before going back to where you were. If you only plan on using four digits after the decimal point, you can probably get away with using floats instead of doubles.
    Last edited by tabstop; 09-20-2008 at 09:24 AM.

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, 06:44 AM
  2. Setting Precision Confusion
    By dnguyen1022 in forum C++ Programming
    Replies: 11
    Last Post: 01-14-2009, 09:38 AM
  3. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  4. Precision based floating-point
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 07-17-2006, 10:35 AM
  5. Scientific notation
    By Lionmane in forum C Programming
    Replies: 9
    Last Post: 06-01-2005, 11:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21