Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
存储精确小数的最佳方法 我需要在C++中存储任意长度的十进制。以下是我们正在考虑的结构: <NumberAsString(char*), DecimalAt(int32)>_C++_Decimal - Fatal编程技术网

存储精确小数的最佳方法 我需要在C++中存储任意长度的十进制。以下是我们正在考虑的结构: <NumberAsString(char*), DecimalAt(int32)>

存储精确小数的最佳方法 我需要在C++中存储任意长度的十进制。以下是我们正在考虑的结构: <NumberAsString(char*), DecimalAt(int32)>,c++,decimal,C++,Decimal,这方面的一个例子是: 1.00003940400 <"100003940400", 11> 0.0004 <"0004", 4> 10 <"10", 0> 1.00003940400 0.0004 10 这似乎是存储小数点的好方法吗?有更好的方法吗 另一种可能的方法类似于javascript中的“货币模块”通常的工作方式,例如,将数字表示为“美分”的整数-$102.25存储为10225。我们会像这样存储它: <int64, int64> #

这方面的一个例子是:

1.00003940400
<"100003940400", 11>

0.0004
<"0004", 4>

10
<"10", 0>
1.00003940400
0.0004
10
这似乎是存储小数点的好方法吗?有更好的方法吗

另一种可能的方法类似于javascript中的“货币模块”通常的工作方式,例如,将数字表示为“美分”的整数-$102.25存储为10225。我们会像这样存储它:

<int64, int64> # the second int64 would be the decimal places
#第二个int64是小数位
例如,如果我们允许“精度的小数点后5位”,那么我们将存储:

12.250
<12, .250E5> // or <12, 25000>
12.250
//或

哪种方法更好?或者完全使用其他方法?

好吧,您需要在运行计算时将
char*
转换为
int
float
double
。但我的意思是,这看起来很酷。你可能会发现另一个问题很有用,它的答案是:@Chipster那么第二种方法呢?那可能有效。第一种方法的好处在于,如果你的大脑是这样工作和思考的,那么它更像是科学符号(有点,不完全是这样)。另一种方式允许不需要转换,这很好。老实说,我认为这个问题是主观的,哪一个更好。它们都有各自的优点和缺点。有几十个这样的库,您确定需要创建另一个库吗?