| View previous topic :: View next topic |
| Author |
Message |
hacksign23 Master Cheater
Reputation: 0
Joined: 26 Nov 2006 Posts: 404
|
Posted: Sat May 30, 2009 7:22 pm Post subject: [C++] Help with roundING! |
|
|
help with this:
| Code: |
double round(double input, int place){//rounds to place's digit
input *= 10^place;
int i2;
double istack;
i2 = (int)floor(input);
istack = input - (double)i2;
if (istack <= 0.4){
input = floor(input);
input /= 10^place;
return input;
}
if (istack >= 0.5) {
input = ceil(input);
input /= 10^place;
return input;
}
return input;
}
|
there's a problem.
ex:
input:
double chicken = round(222.33333,2);
double is 222.375
got any ideas? thanks.
when debugging, i get chicken as 222.33333999999999
_________________
|
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sun May 31, 2009 10:52 am Post subject: Re: [C++] Help with roundING! |
|
|
| Are you sure how to use powers in C/C++?
|
|
| Back to top |
|
 |
hacksign23 Master Cheater
Reputation: 0
Joined: 26 Nov 2006 Posts: 404
|
Posted: Mon Jun 01, 2009 7:45 pm Post subject: |
|
|
shiat, that's not right, is it o_o?
lol yup. thanks. silly me.
_________________
Last edited by hacksign23 on Mon Jun 01, 2009 7:54 pm; edited 1 time in total |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Mon Jun 01, 2009 7:46 pm Post subject: |
|
|
^ is the xor operator, not the power operator.
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
hacksign23 Master Cheater
Reputation: 0
Joined: 26 Nov 2006 Posts: 404
|
Posted: Fri Jun 05, 2009 2:04 am Post subject: |
|
|
okay, i have a function for rounding.
it rounds to place's place.
ex:
input = 22.312
place = 1
output = 22.3
any help?
thanks
double round(double input, int place){//rounds to place's place. haha :3
int iinput;
int iinput2;
double dinput;
double dstack;
iinput = (int)floor(input);
dinput = input-iinput;
dinput *= pow((double)10,place);
iinput2 = (int)floor(dinput);
dstack = dinput;
dstack -= iinput2;
if (dstack <=0.4) {
dinput = floor(dinput);
dinput /= pow((double)10,place);
input = iinput + dinput;
}
if (dstack >=0.5) {
dinput = ceil(dinput);
dinput /= pow((double)10,place);
input = iinput + dinput;
}
return input;
}
_________________
|
|
| Back to top |
|
 |
LolSalad Grandmaster Cheater
Reputation: 1
Joined: 26 Aug 2007 Posts: 988 Location: Australia
|
Posted: Fri Jun 05, 2009 4:15 am Post subject: |
|
|
| Code: | #include <cmath>
double round(double input, int places) {
double mul = pow(10.0, places);
double m = floor(input * mul);
if (floor(input * mul * 10) - m * 10 > 4)
return (m + 1) / mul;
else
return m / mul;
} | That is what I would do.
| Code: | for (int i = 6; i > 0; i--) {
cout << round(274.285327, i) << '\n';
} | Outputs
| Code: | 274.285327
274.28533
274.2853
274.285
274.29
274.3 |
_________________
|
|
| Back to top |
|
 |
|