I tried to use std::setprecision, but with no success.

#include <iostream> #include <iomanip> int main() { double a = 22 / 7; std::cout << std::setprecision(5); std::cout << a; return 0; }

The output is 3, instead of 3.14285

Posted 23 January 2009 - 04:45 PM

I ran into this problem earlier today at school, while programming during my free period. I'm trying to divide an integer by another, and set that value to a double. I'm sure the double contains the correct number, with decimal places, and that the problem is with std::cout.

I tried to use std::setprecision, but with no success.

The output is 3, instead of 3.14285

I tried to use std::setprecision, but with no success.

#include <iostream> #include <iomanip> int main() { double a = 22 / 7; std::cout << std::setprecision(5); std::cout << a; return 0; }

The output is 3, instead of 3.14285

Posted 23 January 2009 - 04:51 PM

Try this:

Just note that I'm not 100% sure this will work for doubles, as I haven't tested it. It does say cout.setf, so I know that it works for floats, and a double is a floating point type, so I don't see why it wouldn't.

Happy Coding

// use these two lines to set the precision cout.setf( ios_base::fixed, ios_base::floatfield ); cout.precision( 2 ); cout << "whatever you want here: " << a << endl; // Reset cout cout.unsetf( ios_base:: fixed ); cout.precision( 0 );

Just note that I'm not 100% sure this will work for doubles, as I haven't tested it. It does say cout.setf, so I know that it works for floats, and a double is a floating point type, so I don't see why it wouldn't.

Happy Coding

Posted 23 January 2009 - 05:01 PM

Psionics, on 23 Jan, 2009 - 03:51 PM, said:

Try this:

Just note that I'm not 100% sure this will work for doubles, as I haven't tested it. It does say cout.setf, so I know that it works for floats, and a double is a floating point type, so I don't see why it wouldn't.

Happy Coding

...

Just note that I'm not 100% sure this will work for doubles, as I haven't tested it. It does say cout.setf, so I know that it works for floats, and a double is a floating point type, so I don't see why it wouldn't.

Happy Coding

Thanks for the help Psionics, it partially fixed the problem. I took your code and made it into a function, and it does make cout print decimal places. It's just that it's printing out 0's instead of the actual decimal.

I tried changing my double to a float to see if that would work, but that didn't help either.

My code as of now:

#include <iostream> #include <iomanip> using namespace std; void setPrec(int prec = -1) { if (prec == -1) { cout.unsetf(ios_base:: fixed); cout.precision(0); } else { cout.setf(ios_base::fixed, ios_base::floatfield); cout.precision(prec); } } int main() { float a = 22 / 7; setPrec(5); cout << a; setPrec(); return 0; }

The output now is 3.00000 instead of 3.14285

This post has been edited by **Kirbya**: 23 January 2009 - 05:02 PM

Posted 23 January 2009 - 05:11 PM

Here is a few more examples along with the

one you posted hope this helps you out a

little bit.

one you posted hope this helps you out a

little bit.

#include <iostream> #include <iomanip> using namespace std; int main(void) { double a = 22 / 7; cout.setf(ios_base::fixed, ios_base::floatfield); // fixed point float tub = 25.0f / 3.0f; // This is good to about 6 places double mint = 10.0 / 3.0; // This one is good to about 15 places const float million = 1.0e6; cout << "tub = " << tub << endl; cout << "mint = " << mint << " and a million mints = "; cout << million * mint << endl; cout << "a = " << a << endl; cout << setprecision(2) << a << "\n\n"; return 0; }

Posted 23 January 2009 - 05:13 PM

The problem is due to how cout works.

If you perform that operation directly in main before you print, it'll work just fine. To get around this problem is kind of tricky and probably not something you want to get into just yet as a beginner.

If you truly want to utilize a function, make a print function, to which you can send what you want to print as the parameter, and how much precision you want if you'd like. Just be sure to handle the unsetf inside the function also, or you'll get some tricky behavior.

If you perform that operation directly in main before you print, it'll work just fine. To get around this problem is kind of tricky and probably not something you want to get into just yet as a beginner.

If you truly want to utilize a function, make a print function, to which you can send what you want to print as the parameter, and how much precision you want if you'd like. Just be sure to handle the unsetf inside the function also, or you'll get some tricky behavior.

Posted 23 January 2009 - 05:24 PM

If you're looking for a reason that the variable a is not coming up with decimals...it's because you're using integer division. When you do that, NO decimal places show up...it truncates. I.E. 22 / 7 = 3 .

You must have**at least one** of the arguments in the division be a float/double/(non-integer) type.

Hope this helps!

// simply change this line double a = 22.0 / 7.0;

You must have

Hope this helps!

This post has been edited by **Locke**: 23 January 2009 - 05:25 PM

Posted 23 January 2009 - 05:27 PM

Kwest your example did work for the variables you created, and yet a still prints as 3.00000, ~~I have no clue why~~ (Explained by Locke). Thanks for your help though.

To Psionics: Thanks for the help and advice on the print function, which is probably what I'm going to end up making.

To Psionics: Thanks for the help and advice on the print function, which is probably what I'm going to end up making.

This post has been edited by **Kirbya**: 23 January 2009 - 05:31 PM

Posted 23 January 2009 - 05:51 PM

I'm an idiot....it's been wayyyyy too long since I've done this sort of basic stuff.

To fix what you already had in your function above, use this

When you just initialize a float to something the way you did, you're using integer division, which truncates everything after the decimal place.

Basically what you had was fine, and what I told you was wrong...my bad lol. Just typecast it to a float using (float) and it'll work the way you intended. Sorry again

To fix what you already had in your function above, use this

float f = (float)22/7;

When you just initialize a float to something the way you did, you're using integer division, which truncates everything after the decimal place.

Basically what you had was fine, and what I told you was wrong...my bad lol. Just typecast it to a float using (float) and it'll work the way you intended. Sorry again

Posted 24 January 2009 - 11:33 PM

Floating point numbers were not designed for exact precision and should not be used in situations where the result of division needs to be exact (like financial calculations). Rounding error and number representation will vary depending on your CPU, programming language, and / or compiler.

See the WikiPedia: Floating Point page, particularly the part on Representable numbers, conversion and rounding.

Aren't they teaching this stuff in college??

Wikipedia excerpts:

"Some numbers (e.g., 1/3 and 0.1) cannot be represented exactly in binary floating-point no matter what the precision. Software packages that perform rational arithmetic represent numbers as fractions with integral numerator and denominator, and can therefore represent any rational number exactly. Such packages generally need to use "bignum" arithmetic for the individual integers."

"When a number is represented in some format (such as a character string) which is not a native floating-point representation supported in a computer implementation, then it will require a conversion before it can be used in that implementation. If the number can be represented exactly in the floating-point format then the conversion is exact. If there is not an exact representation then the conversion requires a choice of which floating-point number to use to represent the original value. The representation chosen will have a different value to the original, and the value thus adjusted is called the rounded value."

"This means that numbers which appear to be short and exact when written in decimal format may need to be approximated when converted to binary floating-point. For example, the decimal number 0.1 is not representable in binary floating-point of any finite precision; the exact binary representation would have a "1100" sequence continuing endlessly:"

Matthew

See the WikiPedia: Floating Point page, particularly the part on Representable numbers, conversion and rounding.

Aren't they teaching this stuff in college??

Wikipedia excerpts:

"Some numbers (e.g., 1/3 and 0.1) cannot be represented exactly in binary floating-point no matter what the precision. Software packages that perform rational arithmetic represent numbers as fractions with integral numerator and denominator, and can therefore represent any rational number exactly. Such packages generally need to use "bignum" arithmetic for the individual integers."

"When a number is represented in some format (such as a character string) which is not a native floating-point representation supported in a computer implementation, then it will require a conversion before it can be used in that implementation. If the number can be represented exactly in the floating-point format then the conversion is exact. If there is not an exact representation then the conversion requires a choice of which floating-point number to use to represent the original value. The representation chosen will have a different value to the original, and the value thus adjusted is called the rounded value."

"This means that numbers which appear to be short and exact when written in decimal format may need to be approximated when converted to binary floating-point. For example, the decimal number 0.1 is not representable in binary floating-point of any finite precision; the exact binary representation would have a "1100" sequence continuing endlessly:"

Matthew

- Caffeine Lounge
- Corner Cubicle
- Student Campus
- Software Development
- Industry News
- Introduce Yourself
- Nightmare.In.Code

- C and C++
- VB.NET
- Java
- C#
- ASP.NET
- .NET Framework
- VB6
- PHP
- Ruby
- Python
- ColdFusion
- Databases
- Other Languages
- Game Development
- Mobile Development
- 52 Weeks Of Code

- Web Development
- HTML & CSS
- JavaScript
- Graphic Design
- Flash & ActionScript
- Blogging
- SEO & Advertising
- Web Servers & Hosting
- Site Check

- External Sorting with C
- C++ Windows Charting Library Part 6
- Bezier Curves Part 1 [Linear Algebra Series]
- STL Algorithms ~ Tutorial 1: Using sort()
- C++ Windows Charting Library Part 5
- C++ Windows Charting Library Part 4
- C++ Windows Charting Library Part 3
- C++ Windows Charting Library Part 2
- C++ Windows Charting Library Part 1
- Win32 Image Editing Program Part3.
**338 More C++ Tutorials...**

- C Snippets
- C++ Snippets
- Java Snippets
- Visual Basic Snippets
- C# Snippets
- VB.NET Snippets
- PHP Snippets
- Python Snippets
- Ruby Snippets
- ColdFusion Snippets
- SQL Snippets
- Assembly Snippets
- Functional Programming Snippets
- Perl Snippets
- HTML/CSS Snippets
- Javascript Snippets
- Flash/ActionScript Snippets
- Other Languages Snippets

Copyright 2001-2014 **MediaGroup1 LLC**, All Rights Reserved

A**MediaGroup1 LLC** Production - Version 6.0.2.1.36

Server: secure3

A

Server: secure3