Thread: Displaying 9.8754321 exactly using cout and format flags

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    61

    Displaying 9.8754321 exactly using cout and format flags

    Pls suggest the code so that the value 9.8754321 is exactly displayed without any truncation? Dont use manipulator or printf function. It is expected to use only formatting flags.

    Code:
    int main()
    {
       float i=9.8754321 ;
       cout<<i;
    
    }

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    setprecision is a manipulator and hence this is not the required solution.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by forumuser
    setprecision is a manipulator and hence this is not the required solution.
    Then use the precision member function. That said, printing a floating point value exactly does not sound right, unless that value is exactly represented in floating point.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I don't think this can be done with format flags alone.

    You could print the integral and decimal part separately as integers (e.g use fmod from cmath to get the decimal part, multiply it by 10000000 and round to integer).

    As a general case, floating point representation has no notion of "digits". It is just a binary approximation of the value.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    61
    Is there any formatting flag that makes it possible to print decimal values with required precision?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by forumuser
    Is there any formatting flag that makes it possible to print decimal values with required precision?
    Frankly, I would just go with:
    Code:
    std::cout.precision(8);
    which is what I told you in post #4.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    37
    sorry but why u dont use printf("%f",float); to express your float?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by punkywow
    sorry but why u dont use printf("%f",float); to express your float?
    1. It is not allowed.
    2. It will not help anyway (but then the question is flawed to begin with).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by forumuser View Post
    Pls suggest the code so that the value 9.8754321 is exactly displayed without any truncation? Dont use manipulator or printf function. It is expected to use only formatting flags.
    Code:
    int main()
    {
       float i=9.8754321 ;
       cout<<i;
    }
    This is probably going to come as a total shock, and you might even be tempted to not believe it at first, but the variable "i" does not contain the value 9.8754321 in that piece of code. A float is an approximation which only holds about 6 significant figures. You can try and assign it a number with as many digits to the right of the decimal point as you like but the actual number it holds will only contain about 6 significant figures. In the above case "i" is 9.8754320 on a typical PC, and that's actually quite lucky, as often the number will be even less accurate than that.
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed