# Problem with rounding when dividing

• (2 Pages)
• 1
• 2

## 16 Replies - 2948 Views - Last Post: 25 January 2009 - 05:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=82465&amp;s=5c503c7910f2a8f07072731165dde774&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Kirbya

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

# Problem with rounding when dividing

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

• D.I.C Regular

Reputation: 19
• 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?

### #3 Psionics

Reputation: 13
• 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

### #4 Kirbya

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

## Re: Problem with rounding when dividing

Posted 23 January 2009 - 04:52 PM

Pwn, 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.

### #5 Kirbya

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

## Re: Problem with rounding when dividing

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

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

### #6 kwest

Reputation: 0
• 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;
}

```

### #7 Psionics

Reputation: 13
• 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.

### #8 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• 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!

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

### #9 Kirbya

Reputation: 0
• 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

### #10 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: Problem with rounding when dividing

Posted 23 January 2009 - 05:32 PM

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

### #11 Psionics

Reputation: 13
• 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.

```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

### #12 Kirbya

Reputation: 0
• 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.

### #13 tommod

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

## Re: Problem with rounding when dividing

Posted 24 January 2009 - 01:15 PM

Quote

```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;

```

### #14 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• 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);
```

### #15 matthew180

Reputation: 51
• 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