提问人:Code4Fun 提问时间:7/7/2023 最后编辑:AmolgorithmCode4Fun 更新时间:7/7/2023 访问量:94
C++ 中的浮点计算精度
Floating Point Computation Precision in C++
问:
我想实现类似浮点精度的东西,其中模板函数控制 point(.) 之后的位数。(或分数)?
template <typename T, unsigned int PRECISION = 4>
constexpr T precision (T v) noexcept
requires (is_floating_point_v<T>)
{
long digits = static_cast<long>(pow(10, PRECISION));
long temp = v * digits;
return
static_cast<T>(temp)
/ static_cast<T>(digits);
}
这是一个幼稚的方法。我对上面的 pow 函数计算数字不是很满意。还有比这更优雅的吗?喜欢:
1e4, but the number *4* is not substitutable by *PRECISION*
10^PRECISION does not work
在编译时有什么东西可以表达 pow 函数吗?
另一种选择是,但我不喜欢界面。
template <typename T, unsigned int PRECISION = 10000>
constexpr T precision (T v) noexcept
requires (is_floating_point_v<T>)
{
long temp = v * PRECISION;
return
static_cast<T>(temp)
/ static_cast<T>(PRECISION);
}
答:
0赞
Code4Fun
7/7/2023
#1
我对这个问题的解决方案是这样的:
template <unsigned int EXP>
constexpr long pow10x () noexcept
requires (EXP >= 0)
{
long result = 1;
for (unsigned int i=0; i<EXP; ++i)
result*=10;
return result;
}
template <unsigned int PRECISION = 4, typename T>
constexpr T precision (T v) noexcept
requires (is_floating_point_v<T> && PRECISION>=1 && PRECISION<=8)
{
long digits = pow10x<PRECISION>();
long temp = v * digits;
return
static_cast<T>(temp)
/ static_cast<T>(digits);
}
我以为有一个内置的 pow 函数,用于整数作为 constexpr 甚至一个简单的表达式。好吧,编写该函数并不难。
评论
0赞
Code4Fun
7/7/2023
你是对的。我把它拿出来了
评论
constexpr
consteval
pow
constexpr std::pow()
constexpr
T
template <unsigned int PRECISION = 4, typename T>
v * digits
long