Problem with rounding when dividing

cout is rounding, and setprecision isn't helping

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2582 Views - Last Post: 25 January 2009 - 05:01 PM Rate Topic: -----

#1 Kirbya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-January 09

Problem with rounding when dividing

Post icon  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.

#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

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with rounding when dividing

#2 Pwn  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 458
  • Joined: 25-November 07

Re: Problem with rounding when dividing

Posted 23 January 2009 - 04:48 PM

Isn't there some IOS flags that have to be set for showing the decimal place also?
Was This Post Helpful? 0
  • +
  • -

#3 Psionics  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 158
  • Joined: 06-September 08

Re: Problem with rounding when dividing

Posted 23 January 2009 - 04:51 PM

Try this:

     // 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 :)
Was This Post Helpful? 0
  • +
  • -

#4 Kirbya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-January 09

Re: Problem with rounding when dividing

Posted 23 January 2009 - 04:52 PM

View PostPwn, on 23 Jan, 2009 - 03:48 PM, said:

Isn't there some IOS flags that have to be set for showing the decimal place also?


I'm not sure, I'm still somewhat new to C++. I've had previous exposure to it but its slowly trickled away.
Was This Post Helpful? 0
  • +
  • -

#5 Kirbya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-January 09

Re: Problem with rounding when dividing

Posted 23 January 2009 - 05:01 PM

View PostPsionics, 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 :)


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

Was This Post Helpful? 0
  • +
  • -

#6 kwest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-November 08

Re: Problem with rounding when dividing

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.


#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;
}


Was This Post Helpful? 0
  • +
  • -

#7 Psionics  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 158
  • Joined: 06-September 08

Re: Problem with rounding when dividing

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.
Was This Post Helpful? 0
  • +
  • -

#8 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Problem with rounding when dividing

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 .

// simply change this line

double a = 22.0 / 7.0;


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

Hope this helps! :D

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

Was This Post Helpful? 1
  • +
  • -

#9 Kirbya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-January 09

Re: Problem with rounding when dividing

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.

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

Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Problem with rounding when dividing

Posted 23 January 2009 - 05:32 PM

Kirbya, read my post. That'll fix your 3.000... problem.

;) :D

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

Was This Post Helpful? 0
  • +
  • -

#11 Psionics  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 158
  • Joined: 06-September 08

Re: Problem with rounding when dividing

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
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 ;)
Was This Post Helpful? 0
  • +
  • -

#12 Kirbya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-January 09

Re: Problem with rounding when dividing

Posted 23 January 2009 - 06:15 PM

Wow it was simple enough as a typecast, even I should've thought of trying that.

Thanks to everyone that helped.
Was This Post Helpful? 0
  • +
  • -

#13 tommod  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 32
  • Joined: 21-July 08

Re: Problem with rounding when dividing

Posted 24 January 2009 - 01:15 PM

Quote

To fix what you already had in your function above, use this
float f = (float)22/7;



That is a C style typecast. You would be much better off using a C++ static cast.
float f = static_cast<float>22 / 7;


Was This Post Helpful? 0
  • +
  • -

#14 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Problem with rounding when dividing

Posted 24 January 2009 - 10:35 PM

Sorry...but that's also wrong.

// notice the ()

float a = static_cast<float>(22 / 7);

Was This Post Helpful? 0
  • +
  • -

#15 matthew180  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 202
  • Joined: 07-January 09

Re: Problem with rounding when dividing

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
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2