Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constexpr Math Functions

So noticed from this page that none of the math functions in c++11 seems to make use of constexpr, whereas I believe all of them could be. So that leaves me with two questions, one is why did they choose not to make the functions constexpr. And two for a function like sqrt I could probably write my own constexpr, but something like sin or cos would be trickier so is there a way around it.

like image 437
aaronman Avatar asked Jun 27 '13 16:06

aaronman


People also ask

Can a function return constexpr?

A constexpr function is a function that can be invoked within a constant expression. A constexpr function must satisfy the following conditions: It is not virtual. Its return type is a literal type.

Can std :: function be constexpr?

Constexpr constructors are permitted for classes that aren't literal types. For example, the default constructor of std::unique_ptr is constexpr, allowing constant initialization.

What does constexpr mean in C++?

constexpr specifies that the value of an object or a function can be evaluated at compile-time and the expression can be used in other constant expressions. Example: CPP.


3 Answers

Actually, because of old and annoying legacy, almost none of the math functions can be constexpr, since they all have the side-effect of setting errno on various error conditions, usually domain errors.

like image 154
Sebastian Redl Avatar answered Oct 20 '22 16:10

Sebastian Redl


From "The C++ Programming Language (4th Edition)", by B. Stroustrup, describing C++11:

"To be evaluated at compile time, a function must be suitably simple: a constexpr function must consist of a single return-statement; no loops, and no local variables are allowed. Also, a constexpr function may not have side effects."

Which means that it must be inline, without for, while and if statements and local variables. Side effects are also forbidden (ex: changing of errno). Another problem is that most of math functions are FPU instructions which are not represented in pure c/c++ (they are written in assembler code). That's why non of cmath function is declared as constexpr.

like image 36
Adam Szaj Avatar answered Oct 20 '22 18:10

Adam Szaj


So noticed from this page that none of the math functions in c++11 seems to make use of constexpr, whereas I believe all of them could be. So that leaves me with two questions, one is why did they choose not to make the functions constexpr.

This part is very well answered by Sebastian Redl and Adam Szaj so won't be adding anything to it.

And two for a function like sqrt I could probably write my own constexpr, but something like sin or cos would be trickier so is there away around it.

Yes, you can write your own version of constexpr sin, cos by using the taylor series expansions of these functions. Have a look at this super cool github repo which implements several mathematical functions as constexpr functions Morwenn/static_math

like image 5
lakshayg Avatar answered Oct 20 '22 18:10

lakshayg