【发布时间】:2021-02-10 08:44:38
【问题描述】:
我喜欢在我的许多 C# 程序中使用decimal,我想在其他 C 语言中尝试它。
我用 C# 编写了这段代码:
using System;
decimal get_pi();
int main() {
Console.WriteLine("Give a number ranging from 1-100: ");
decimal num = Console.ReadLine();
Console.Write("Algebraic Equivalent: ");
if (num >= get_pi()) {
Console.WriteLine("π");
} else {
Console.WriteLine(num);
}
// Currently updating the program for giving algebraic equivalents
}
我想用 C++ 重写它。
我意识到在 C 和 C++ 中,它们没有完全等同于 decimal 类型,您可以使用的最好的是 double,但事实上在 C 中实际上支持 decimal 类型(基于在Is there a C equivalent of C#'s decimal type?) 上,我很好奇 C++ 中是否也有等价物。
在 C++ 中是否有任何等效的 C# decimal 类型或者 double 是最佳选择?
【问题讨论】:
-
那么不,没有直接的等价物 - 取决于使用情况。
long double将“更接近”,具体取决于实现。 (这可能是一个扩展......) -
c# 中的十进制是浮点十进制而不是浮点二进制类型。检查 c++ 中是否存在浮点十进制类型。这可能是等价的。
-
Boost 多精度具有可以使用的更高精度浮点类型:boost.org/doc/libs/1_72_0/libs/multiprecision/doc/html/…
-
@janzen C++ supports everything in C's math.h 包括 FLT_RADIX,因此兼容的实现可以有十进制
float和double