views:

327

answers:

4

Is there any compiler that has a directive or a parameter to cast integer calculation to float implicitly. For example:

float f = (1/3)*5;
cout << f;

the "f" is "0", because calculation's constants(1, 3, 10) are integer. I want to convert integer calculation with a compiler directive or parameter. I mean, I won't use explicit casting or ".f" prefix like that:

float f = ((float)1/3)*5;

or

float f = (1.0f/3.0f)*5.0f;

Do you know any c/c++ compiler which has any parameter to do this process without explicit casting or ".f" thing?

+1  A: 

If you don't like either of the two methods you mentioned, you're probably out of luck.

What are you hoping to accomplish with this? Any specialized operator that did "float-division" would have to convert ints to floats at some point after tokenization, which means you're not going to get any performance benefit on the execution.

Jekke
Thanks for answer, but it isn't about liking or disliking issue. Our project is huge and we have no interest in any of integer operations. We can do mistakes ussually. So, we have to run a macro that converts integer constants to floating point constants(.f), after every code submit to the repository. If we have a compiler that does this job instead of us, we can eliminate a step(running the macro) of building the project.
Ziddiri
@Ziddiri - if you genuinely never in your code need an integer constant, I'd have thought it would make more sense to run that check *before* committing to the repository, not afterwards. Reject the code (or warn the programmer) if it uses integer literals. Of course most uses aren't harmful `float f = 1; f += 5; f = 1/f;`. It's just occasionally that it's wrong, so I see that it's easy to make mistakes unless you just avoid integer literals altogether. So, consistently write `float f = 1.0; f += 5.0; f = 1.0/f;`, and then wrong code like `f *= (3/2);` will *look* wrong.
Steve Jessop
Your project doesn't use a single int? Not even in for loops?
Peter Alexander
@Poita, our macro scripts can handle "for" loops. But interesting thing that most of code(most of formula and calculations with that formulas) uses float,double, etc. except "for" loops.
Ziddiri
A: 

No, those two options are the best you have.

Peter Alexander
+1  A: 

In C++ it's a bit odd to see a bunch of numeric values sprinkled through the code. Generally it is considered best practice to move any 'magic numbers' like these to their own static const float value, which removes this problem.

tloach
The same practice could and should be applied to C#.
kenny
+1  A: 

Any compiler that did what you want would no longer be a conforming C++ compiler. The semantics of integer division are well specified (at least for positive numbers), and you're proposing to change that.

It would also be dangerous since it would wind up applying to everything, and you might at some point have code that relies on standard integer arithmetic, which would silently be invalid. (After all, if you had tests that would catch that, you presumably would have tests that would catch the undesired integer arithmetic.)

So, the only advice I've got is to write unit tests, have code reviews, and try to avoid magic numbers (instead defining them as const float).

David Thornley