constexpr exp,日志,战俘 [英] constexpr exp, log, pow

查看:90
本文介绍了constexpr exp,日志,战俘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以可移植的方式使用标准<cmath>函数的constexpr版本,例如explogpow.我目前有一个非便携式解决方案 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.

如何实现这些功能?我对explogpow

我从研究中学到的一些切线相关的东西

  • 这些函数的标准兼容版本在技术上不是constexpr,因为它们必须具有副作用(例如,设置errno)才能保持C兼容性
  • 在C ++ 11中,允许实现使这些功能成为constexpr,但是从C ++ 14开始,这被禁止(根据
  • The standard-compliant versions of these functions aren't technically constexpr because they must have side-effects (e.g. setting errno) 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 be constexpr in future versions of g++
  • g++'s implementation of each math function foo just calls a built-in function __builtin_foo, which is treated as constexpr. I could perhaps start calling the __builtin_foo functions rather than the foo functions - these might remain constexpr in future versions of g++ even if the corresponding foo 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆