Rounding problem

Hello, how do I prevent rounding number ? "m" is supposed to be 1.73620019, but instead im getting 1.7115.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double s=10,k=1.17;
m=s/3.4825-k;
cout << m;
return 0;
}
where have you defined 'm'?
And do you think you are subtracting k once the division is done or subtracting k from your denominator before the division is carried out?

also note that something like 1.17 might actually be something like 1.17000000001, or something like that. shouldn't affect a calculation like this though.
closed account (48T7M4Gy)
Your code equates to 1.7015
Hand calculation gives 1.70150035894

Your two figures are wrong unfortunately. Also m in your program is not declared so I made it a double.
I accidently took it out while copying code here, i was deleting some off-topic lines :P. A bit more code here, and I need to get n=0.1, but instead im getting 0.18, or 0.2, probably because of rounding problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double s=10, // money you have in your x currency
       k=1.16, // money item costs in other y currency
       ge=1.73, // return in y currency
       n, // loss because of convertations
       m; // random number ;p
m=s/3.4825-k;
n=ge-m;
cout << setprecision(1) << n;
return 0;
}


Also, im diving s/3.4825 to convert it to other currency.
Last edited on
closed account (48T7M4Gy)
To more significant figures the answer this time with your latest input is 0.01849964106

There is no error in what the computer is doing.
Is there any way I can round it to 0.01 then ?
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/ios/fixed/
You could probably pull it off with some combination of setprecision and round. Note the several functions found near the bottom of the round reference. trunc() would probably be what you're looking for.

http://www.cplusplus.com/reference/cmath/round/?kw=round
closed account (48T7M4Gy)
Return Value
The value of x rounded to the nearest integral (as a floating-point value).


that's the problem with round

Often with money, any calculation generally is carried to at least 4 decimal places and then displayed at 2 places (obvious). This has cropped many times in the recent past and an easy way to display the 2 places for x is cout<< (float)((int)( x * 100 ) / 100 );
Topic archived. No new replies allowed.