Displaying 9.8754321 exactly using cout and format flags

This is a discussion on Displaying 9.8754321 exactly using cout and format flags within the C++ Programming forums, part of the General Programming Boards category; Pls suggest the code so that the value 9.8754321 is exactly displayed without any truncation? Dont use manipulator or printf ...

  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
    Dae
    Dae is offline
    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
    21,457
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,457
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,457
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,296
    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

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