Announcement: Be excellent to each other.


Caravel Forum : Other Boards : Anything : A bit of help with C++ (Floating point variables to integers)
New Topic New Poll Post Reply
Poster Message
Ravon
Level: Master Delver
Avatar
Rank Points: 220
Registered: 02-19-2004
IP: Logged
icon A bit of help with C++ (0)  
I'm programming a game (if you could call it that) and need to convert a floating point number into an integer, if someone could give me an easy way to do that that would be great!!

Also, to post something on the development board, does it have to be DROD related?
05-04-2005 at 03:40 PM
View Profile Send Private Message to User Send Email to User Show all user's posts This architect's holds Quote Reply
stigant
Level: Smitemaster
Avatar
Rank Points: 1182
Registered: 08-19-2004
IP: Logged
icon Re: A bit of help with C++ (+2)  
Well, it depends on if you want to round or truncate.

if you want to truncate, you can just do this:

float f = whatever;
int i = (int)f;

if you want to round, there are some rounding functions, but they essentially do this:

float f = whatever;
int i = (int)(f + 0.5);


____________________________
Progress Quest Progress
05-04-2005 at 03:46 PM
View Profile Send Private Message to User Show all user's posts Quote Reply
AlefBet
Level: Smitemaster
Rank Points: 979
Registered: 07-16-2003
IP: Logged
icon Re: A bit of help with C++ (+1)  
As a matter of style, I prefer to use static_cast. I.e.:
float f=g();
int i=static_cast<int>(f); // or int i=static_cast<int>(f+0.5);

It's more verbose, though, but C style casts are a bit too powerful (in a bad way) and I try to avoid using them. A C style cast is harmless in this case, but I try to make it a rule....

____________________________
I was charged with conspiracy to commit jay-walking, and accessory to changing lanes without signaling after the fact :blush.

++Adam H. Peterson
05-04-2005 at 04:13 PM
View Profile Send Private Message to User Send Email to User Visit Homepage Show all user's posts Quote Reply
Ravon
Level: Master Delver
Avatar
Rank Points: 220
Registered: 02-19-2004
IP: Logged
icon Re: A bit of help with C++ (0)  
Thank you both!
05-04-2005 at 09:42 PM
View Profile Send Private Message to User Send Email to User Show all user's posts This architect's holds Quote Reply
SKWERLL
Level: Delver
Avatar
Rank Points: 77
Registered: 02-22-2004
IP: Logged
icon Re: A bit of help with C++ (0)  
Is this that Simple City game you were talking about at school? If it is, could you post a link in this thread once you get it uploaded? (I'm not so good at remembering links, and http://freewebs.com/randomflyingpigs/simplecity.ral doesn't seem to work.)

From what he tells me, the game he's programming is a text-based game that's kinda like a SimCity/Strategy game. Of course, I may be quite wrong.

____________________________
SKWERLL? MORE LIKE THE DORK CHEESE!

The best hold in the world!
05-05-2005 at 01:07 AM
View Profile Send Private Message to User Show all user's posts Quote Reply
wmarkham
Level: Master Delver
Avatar
Rank Points: 125
Registered: 12-06-2004
IP: Logged
icon Re: A bit of help with C++ (+1)  
AlefBet wrote:
As a matter of style, I prefer to use static_cast. I.e.:
float f=g();
int i=static_cast<int>(f); // or int i=static_cast<int>(f+0.5);

It's more verbose, though, but C style casts are a bit too powerful (in a bad way) and I try to avoid using them. A C style cast is harmless in this case, but I try to make it a rule....

In the spirit of one-upmanship, or maybe even two-upmanship:
float f=g();
int i(f);
assert(0<f ? (i-1<f && f<=i) : (i<=f && f<i+1));


I don't actually recall if C++ actually requires rounding toward zero. It may be the case that for some compilers, both positive and negative numbers are rounded downward. Regardless, note that the assert statement protects against out-of-range floating point values.

05-09-2005 at 09:38 PM
View Profile Send Private Message to User Send Email to User Show all user's posts Quote Reply
New Topic New Poll Post Reply
Caravel Forum : Other Boards : Anything : A bit of help with C++ (Floating point variables to integers)
Surf To:


Forum Rules:
Can I post a new topic? No
Can I reply? No
Can I read? Yes
HTML Enabled? No
UBBC Enabled? Yes
Words Filter Enable? No

Contact Us | CaravelGames.com

Powered by: tForum tForumHacks Edition b0.98.8
Originally created by Toan Huynh (Copyright © 2000)
Enhanced by the tForumHacks team and the Caravel team.