Alan Clifford
Alan Clifford

Reputation: 161

How to write a C++ float literal?

result is float and I could code this three ways:

As I understand it,

I'd prefer to use the first method since it is clear and simple but am I forcing a type conversion by using it?

Upvotes: 13

Views: 7181

Answers (1)

Bathsheba
Bathsheba

Reputation: 234695

Conceptually yes, conversions are made.

But you should defer such micro-considerations to the compiler and write what's clearest which, for me is

if (result < 0)

If you are ever in any doubt, check the generated assembly (very easy with https://gcc.godbolt.org/).

Finally, when deciding to use a float over a double, consider double or float, which is faster?

Upvotes: 8

Related Questions