Public Group

# Type Casting

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Every time I want to do something simple, like multiply a int by a float, I have to change this:
myInt *= myFloat;
To this:
myInt = (int)((float)myInt * myFloat);
I definitely don't want to turn off compiler warnings entirely; those are helpful when I make a mistake. I'm just wanting to know if there is a easier way to convert from int to float? The above code snippet is fairly simple, but here's a copy + paste of something almost as basic that I just wrote in my code:
damage = (int)((float)damage * (1.0f - ((float)distanceFromEnemy / (float)(weapon->range+1))));
The nested parentheses irritate me. Would you break it up? How would you write it?
float damageModifyer = (1.0f - ((float)distanceFromEnemy / (float)(weapon->range+1)));
damage = (int)((float)damage * damageModifyer);
Without the need of typecasting, it looks very clean:
damage *= (1.0f - (distanceFromEnemy / (weapon->range+1)));
Mainly, it's the inability to use *=, /=, +=, and -=, when typecasting that makes it look most messy. It'd be great if I could auto typecast for a single line only. Something like:
(typecast) damage *= (1.0f - (distanceFromEnemy / (weapon->range+1)));

##### Share on other sites
Indeed it might. [lol]

I'm using C++. Dev C++ as my IDE, but I'd want something that works across most compilers, as I'm going to force myself to migrate to either Code::Blocks or Visual C++ 9.0 one my next projects.

##### Share on other sites
It won't solve all problems, but you shouldn't need to cast every variable in the calculations: if one of the values involved is a float, all others should be cast to float implicitly (and without a warning?):

myInt = int(myInt * myFloat);

The cast back is needed (to silence the warnings), since you will be losing data here.

As a whole, you might just try to assign floats to int in as few places as possible.

##### Share on other sites
make the int a float as well?

##### Share on other sites
Warning are inherently compiler dependent in C++, which means that ways around them are also compiler dependent. In MSVC you can mark a block of code to not give you conversion warnings by surrounding it with the right set of pragmas. Ex:
#pragma warning(push)#pragma warning(disable:4244)  int i = 1;  float f = 2.0f;  i *= f;#pragma warning(pop)

##### Share on other sites
Quote:
 Original post by visitorIt won't solve all problems, but you shouldn't need to cast every variable in the calculations: if one of the values involved is a float, all others should be cast to float implicitly (and without a warning?):myInt = int(myInt * myFloat);
That's very helpful, I thought I had to manually cast each int to a float and then cast the entire result back to a int afterward. Thank you! That alone makes the code more readable.
Quote:
 Original post by SiCraneWarning are inherently compiler dependent in C++, which means that ways around them are also compiler dependent. In MSVC you can mark a block of code to not give you conversion warnings by surrounding it with the right set of pragmas.
Is there some library online, or just a header file, that creates macroes for easily disabling/re-enabling warnings on most of the major compilers and platforms? If you aren't aware of one just offhand, don't bother (I'm just curious for future needs). Visitor's post gave me what I needed. [smile]

Thanks guys.

##### Share on other sites
Have you considered just using floats for all the variables in question? What problems would it cause?

##### Share on other sites
One alternative is to just rework your equation:

damage -= (damage * distanceFromEnemy) / (weapon->range + 1);

##### Share on other sites
Quote:
 Original post by ZahlmanHave you considered just using floats for all the variables in question? What problems would it cause?

Well, I'll have to typecast them somewhere along the line. distanceFromEnemy is passed as a argument to the function, but that argument could be made a float. Both weapon->range and damage are retrieved from a structure that holds information about the weapon being used. This wont be the only function that uses them, and I don't want to make them floats in the structure for just this block of code.
Quote:
 Original post by dmatterOne alternative is to just rework your equation:damage -= (damage * distanceFromEnemy) / (weapon->range + 1);

That's a much sleeker way of writing it, I'll use that. This wasn't the only time where I was needing alot of typecasting though, and visitor's example of how to use typecasting will cut down enough of the manual typecasting to satisfy me for any other cases I might encounter.

1. 1
2. 2
frob
20
3. 3
Rutin
17
4. 4
5. 5

• 13
• 10
• 9
• 18
• 9
• ### Forum Statistics

• Total Topics
632555
• Total Posts
3007035

×