2

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?

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Ziddiri
  • 23
  • 1
  • 3
  • 7
    You would be changing the semantics of the underlying language if you could do that - not usually a good idea. Maybe you'd be happier with a completely different programming language that has semantics for int/float expressions which are more to your taste ? – Paul R Mar 15 '10 at 18:49
  • 1
    BTW: in the second case, only one of the constants `1` and `3` need to be float. The other constant and the `5` will automatically promote. Also you do not need to define them to be `float`, doubles will be implicitly demoted to float: `float f = (1.0 / 3)*5` will make the trick (or `5.0/3`). – David Rodríguez - dribeas Mar 15 '10 at 19:03
  • 1
    @David: "the 5 will automatically promote", I was under the impression that when integral constants smaller than int are converted to int then we say they are "promoted", but in all other cases we just say "converted". – Alexandros Gezerlis Mar 15 '10 at 19:07
  • 1
    @Alexandros Gezerlis: You are right. I misused the word 'promote'. There are integral promotions and floating point promotions (from `float` to `double`) but when both integral and floating points are involved they are called 'conversions' – David Rodríguez - dribeas Mar 15 '10 at 19:13
  • if you don't like the extra .0f, just use f. (1f/3f)*5f - it obvious but not in the way. And like others have said, (1f/3)*5 will do it too. – plinth Mar 15 '10 at 19:24

4 Answers4

3

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
  • 56,304
  • 9
  • 91
  • 158
2

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.

Yes - that Jake.
  • 16,725
  • 14
  • 70
  • 96
  • 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 Mar 15 '10 at 19:26
  • 1
    @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 Mar 15 '10 at 20:07
  • 3
    Your project doesn't use a single int? Not even in for loops? – Peter Alexander Mar 15 '10 at 20:09
  • @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 Mar 15 '10 at 20:44
2

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
  • 8,009
  • 1
  • 33
  • 44
1

No, those two options are the best you have.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168