Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Help with roundING!

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
hacksign23
Master Cheater
Reputation: 0

Joined: 26 Nov 2006
Posts: 404

PostPosted: Sat May 30, 2009 7:22 pm    Post subject: [C++] Help with roundING! Reply with quote

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
View user's profile Send private message AIM Address
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Sat May 30, 2009 7:38 pm    Post subject: Reply with quote

http://www.cplusplus.com/forum/general/4011/

Or you could use the ceil or floor functions.
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Sun May 31, 2009 10:52 am    Post subject: Re: [C++] Help with roundING! Reply with quote

hacksign23 wrote:
Code:

   input *= 10^place;
Are you sure how to use powers in C/C++?
Back to top
View user's profile Send private message
hacksign23
Master Cheater
Reputation: 0

Joined: 26 Nov 2006
Posts: 404

PostPosted: Mon Jun 01, 2009 7:45 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Mon Jun 01, 2009 7:46 pm    Post subject: Reply with quote

^ 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
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
hacksign23
Master Cheater
Reputation: 0

Joined: 26 Nov 2006
Posts: 404

PostPosted: Fri Jun 05, 2009 2:04 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
LolSalad
Grandmaster Cheater
Reputation: 1

Joined: 26 Aug 2007
Posts: 988
Location: Australia

PostPosted: Fri Jun 05, 2009 4:15 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites