ethz-asl / programming_guidelines

This repository contains style-guides, discussions, eclipse/emacs auto-formatter for commonly used programming languages
139 stars 38 forks source link

Floating point constants should always be written with decimal point and at least one decimal. #14

Closed markusachtelik closed 11 years ago

markusachtelik commented 11 years ago

for 32 bit floating point values, writing float x = 0.0 can lead to performance issues especially for embedded platforms, since apparently a double is assumed (someone told me it happened on a cortex), so float x = 0.0f should be preferred.

stephanemagnenat commented 11 years ago

Why not allowing the decimal point without the trailing decimal, like float x = 0.f; ?

markusachtelik commented 11 years ago

ok foe me, all I wanted to point out is the f indicating a float

stephanemagnenat commented 11 years ago

I agree, although for templates that can be float or double it is better left out, maybe in these cases we should request explicit cast, like:

template<typename FloatingType>
struct A
{
    FloatingType getZero() const { return FloatingType(0.); }
};
HannesSommer commented 11 years ago

Wow, I can't believe that in Markus' example the conversion isn't done at compile time. Can someone explain that to me? (0.0 is obviously a constexpr and the conversion double to float should be one as well. So why is it done at runtime?) Maybe that was a buggy / old compiler?