如何在 c/c++ 中获得大数的精确二进制表示?
How to get exact binary representation of big numbers in c/c++?
我正在尝试在 C/C++ 和 Java 中将大数转换为二进制,但是如果我以纯十进制形式输入 998446744073709551615
输出是正确的,但如果我将使用科学记数法 ex : 1.7334e+32
那么二进制表示就出错了。
我已经在 C/C++ 和 Java.
中从二进制测试了 double 和 BigDecimals
String to Long Double in C/C++ Test :
https://ideone.com/EeOyNP
字符串到 Java 测试中的大十进制:
https://ideone.com/OAvx7q
问题是超过 64 bits
的数字在某种程度上没有以科学记数法表示。
查看下面的输出。
C/C++ 代码的输出:
Input = 998446744073709551615 Expected Binary =
1101100010000000111011011001111011110011001010110011111111111111111111
Output : Successfully parsed strtold (C-Style): 9.98447e+20 Binary :
1101100010000000111011011001111011110011001010110100000000000000000000
stringstream parsed stringstream (C++ Style): 9.98447e+20 Binary :
1101100010000000111011011001111011110011001010110100000000000000000000
showBitDiff statistics : Total Bits 70 Bits 49 Bits matched 21 Bits
not matched
来自 Java 代码的输出:
Decimal String Part :
decimalString : 998446744073709551615
Scientific notation : 9.984467440737096E20
Decimal-String Radix Info :
Binary :
1101100010000000111011011001111011110011001010110011111111111111111111
Decimal : 998446744073709551615
Hexa : 0x36203B67BCCACFFFFF
Bit length : 70
Exponent String Part :
exponentString : 9.984467440737096E20
Exponent String Radix Info :
Binary :
1101100010000000111011011001111011110011001010110100001011110100000000
Decimal : 998446744073709600000
Hexa : 0x36203B67BCCAD0BD00
Bit length : 70
Both BigInts are not equal
如何解决这个问题并在 C/C++ 中正确表示大数?
我不想要 java 中的解决方案 我只是将 java 用于测试目的,因为它具有 bigDecimal Class 非常大的任意数字谢谢。
如果你先转换成双精度来表示你的指数表示的小数,那当然是错误的。看看 https://en.wikipedia.org/wiki/Floating-point_arithmetic。 TLDR; 如果你的数字比州 (2^128) 多,你就会有差距。这就是 double 从一开始就设计的目的。如果您需要实际数字的精确表示,请不要将其转换为浮点表示。
我正在尝试在 C/C++ 和 Java 中将大数转换为二进制,但是如果我以纯十进制形式输入 998446744073709551615
输出是正确的,但如果我将使用科学记数法 ex : 1.7334e+32
那么二进制表示就出错了。
我已经在 C/C++ 和 Java.
中从二进制测试了 double 和 BigDecimalsString to Long Double in C/C++ Test : https://ideone.com/EeOyNP
字符串到 Java 测试中的大十进制: https://ideone.com/OAvx7q
问题是超过 64 bits
的数字在某种程度上没有以科学记数法表示。
查看下面的输出。
C/C++ 代码的输出:
Input = 998446744073709551615 Expected Binary = 1101100010000000111011011001111011110011001010110011111111111111111111
Output : Successfully parsed strtold (C-Style): 9.98447e+20 Binary : 1101100010000000111011011001111011110011001010110100000000000000000000
stringstream parsed stringstream (C++ Style): 9.98447e+20 Binary : 1101100010000000111011011001111011110011001010110100000000000000000000
showBitDiff statistics : Total Bits 70 Bits 49 Bits matched 21 Bits not matched
来自 Java 代码的输出:
Decimal String Part :
decimalString : 998446744073709551615 Scientific notation : 9.984467440737096E20
Decimal-String Radix Info :
Binary : 1101100010000000111011011001111011110011001010110011111111111111111111 Decimal : 998446744073709551615 Hexa : 0x36203B67BCCACFFFFF Bit length : 70Exponent String Part :
exponentString : 9.984467440737096E20Exponent String Radix Info :
Binary : 1101100010000000111011011001111011110011001010110100001011110100000000 Decimal : 998446744073709600000 Hexa : 0x36203B67BCCAD0BD00 Bit length : 70Both BigInts are not equal
如何解决这个问题并在 C/C++ 中正确表示大数? 我不想要 java 中的解决方案 我只是将 java 用于测试目的,因为它具有 bigDecimal Class 非常大的任意数字谢谢。
如果你先转换成双精度来表示你的指数表示的小数,那当然是错误的。看看 https://en.wikipedia.org/wiki/Floating-point_arithmetic。 TLDR; 如果你的数字比州 (2^128) 多,你就会有差距。这就是 double 从一开始就设计的目的。如果您需要实际数字的精确表示,请不要将其转换为浮点表示。