constexpr exp,日志,战俘 [英] constexpr exp, log, pow
问题描述
我想以可移植的方式使用标准<cmath>
函数的constexpr
版本,例如exp
,log
,pow
.我目前有一个非便携式解决方案 g++
将这些功能视为constexpr
-C ++的不兼容扩展,但是我担心可移植性和面向未来的发展(我想也许有一天可以从g++
中删除此扩展).
I'd like to use constexpr
versions of standard <cmath>
functions like exp
, log
, pow
in a portable way. I currently have a non-portable solution g++
treats these functions as constexpr
- a non-compliant extension of C++, but I am concerned about portability and future-proofing (I imagine that this extension might one day be removed from g++
).
我对这些函数的constexpr
版本感兴趣,而不是模板元程序-我希望在编译时和运行时都可以使用相同的功能.我不需要C兼容性,但是我需要快速实现-像Taylor Series扩展这样的幼稚实现会太慢.
I am interested in constexpr
versions of these functions, not template metaprograms - I want the same functionality to be available both at compile time and runtime. I do not need C compatibility, but I do need fast implementations - naive implementations such as Taylor Series expansions would be too slow.
如何实现这些功能?我对exp
,log
和pow
我从研究中学到的一些切线相关的东西
- 这些函数的标准兼容版本在技术上不是
constexpr
,因为它们必须具有副作用(例如,设置errno
)才能保持C兼容性 - 在C ++ 11中,允许实现使这些功能成为
constexpr
,但是从C ++ 14开始,这被禁止(根据此问题的答案).这是部分原因,我担心在g++
的未来版本中功能可能不是constexpr
每个计算机函数 -
g++
实现仅调用内置函数__builtin_foo
,该函数被视为constexpr
.我也许可以开始调用__builtin_foo
函数而不是foo
函数-即使相应的foo
函数符合标准,它们也可能在g++
的未来版本中保留为constexpr
-但这仅对将来有所帮助-证明,不具有便携性.
foo
的- The standard-compliant versions of these functions aren't technically
constexpr
because they must have side-effects (e.g. settingerrno
) to maintain C compatibility - In C++11, an implementation was allowed to make these functions
constexpr
, but as of C++14, this is prohibited (per the first answer to this question and the answer to this question). This is part of the reason that I am concerned that the functions may not beconstexpr
in future versions ofg++
g++
's implementation of each math functionfoo
just calls a built-in function__builtin_foo
, which is treated asconstexpr
. I could perhaps start calling the__builtin_foo
functions rather than thefoo
functions - these might remainconstexpr
in future versions ofg++
even if the correspondingfoo
functions are made compliant - but this only helps with future-proofing, not with portability.
推荐答案
您曾经看过发芽的实现?
Sprout是仅标头的库,它提供基于C ++ 11/14 constexpr的容器,算法,随机数,解析,光线跟踪,合成器等.
Sprout is header-only library that provide C++11/14 constexpr based Containers, Algorithms, Random numbers, Parsing, Ray tracing, Synthesizer, and others.
https://github.com/bolero-MURAKAMI/Sprout/树/主/新芽/数学
这篇关于constexpr exp,日志,战俘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!