std::expf and std::logf not recognized by gcc 7.2.0

后端 未结 3 1822
感动是毒
感动是毒 2020-12-20 20:44

It seems that gcc (tried 7.2.0 and 5.4.0) does not have std::expf and std::logf - see coliru sample. Since cppreference says they were added in C++11 is there some gcc speci

3条回答
  •  盖世英雄少女心
    2020-12-20 21:25

    If you

    #include 
    

    you will get

    float       exp ( float arg );
    double      exp ( double arg );
    long double exp ( long double arg );
    double      exp ( IntegralType arg );
    
    float       log ( float arg );
    double      log ( double arg );
    long double log ( long double arg );
    double      log ( IntegralType arg );
    

    And hence you can call just std::exp/std::log and let the compiler figure out the overload for you. If you want to call a mismatching overload (e.g. the float overload on a double variable), I find it much more explicit and clear to add a static_cast in those cases:

    double bla = ...;
    return std::exp(static_cast(bla));
    

    This is a strange construct any way you write it (e.g. why is bla not a float to begin with?), and hiding it in a single-letter function name suffix isn't helping anyone.

提交回复
热议问题