[英]How to get exact binary representation of big numbers in c/c++?
I am trying to convert big numbers to binary in C/C++ and Java but if i take input in plain decimal like 998446744073709551615
output is correct but if i will use scientific notation ex : 1.7334e+32
then binary representation comes wrong. 我正在尝试在C / C ++和Java中将大数转换为二进制,但如果我采用普通的十进制形式输入,例如
998446744073709551615
输出是正确的,但是如果我将使用科学记数法ex: 1.7334e+32
则二进制表示形式会出错。
I have tested from double and BigDecimals from binary in C/C++ and Java. 我已经在C / C ++和Java中从double和BigDecimals从二进制进行了测试。
String to Long Double in C/C++ Test : https://ideone.com/EeOyNP 在C / C ++测试中将字符串转换为Long Double: https : //ideone.com/EeOyNP
String to Big Decimal in Java Test : https://ideone.com/OAvx7q 在Java测试中将字符串转换为大十进制: https : //ideone.com/OAvx7q
The problem is with numbers which are represented in more than 64 bits
aren't represented in scientific notation somehow . 问题在于用
64 bits
以上的数字表示的数字没有以科学的记号表示。
Check out the output below. 查看下面的输出。
Output from C/C++ Code : C / C ++代码的输出:
Input = 998446744073709551615 Expected Binary = 1101100010000000111011011001111011110011001010110011111111111111111111
输入= 998446744073709551615预期二进制= 1101100010000000111011011001111011110011001010110011111111111111111111111
Output : Successfully parsed strtold (C-Style): 9.98447e+20 Binary : 1101100010000000111011011001111011110011001010110100000000000000000000
输出:成功解析strtold(C样式):9.98447e + 20二进制:1101100010000000111011011001111011110011001010110100000000000000000000
stringstream parsed stringstream (C++ Style): 9.98447e+20 Binary : 1101100010000000111011011001111011110011001010110100000000000000000000
stringstream解析的stringstream(C ++样式):9.98447e + 20 Binary:1101100010000000111011011001111011110011001010110100000000000000000000
showBitDiff statistics : Total Bits 70 Bits 49 Bits matched 21 Bits not matched
showBitDiff统计信息:总位70位49位匹配21位不匹配
Output from Java Code : Java代码的输出:
Decimal String Part :
小数字符串部分:
decimalString : 998446744073709551615 Scientific notation : 9.984467440737096E20
decimalString:998446744073709551615科学计数法:9.984467440737096E20
Decimal-String Radix Info :
十进制字符串基数信息:
Binary : 1101100010000000111011011001111011110011001010110011111111111111111111 Decimal : 998446744073709551615 Hexa : 0x36203B67BCCACFFFFF Bit length : 70二进制:1101100010000000111011011001111011110011001010110011111111111111111111十进制:998446744073709551615十六进制:0x36203B67BCCACFFFFF位长度:70
Exponent String Part :
指数字符串部分:
exponentString : 9.984467440737096E20exponentString:9.984467440737096E20
Exponent String Radix Info :
指数字符串基数信息:
Binary : 1101100010000000111011011001111011110011001010110100001011110100000000 Decimal : 998446744073709600000 Hexa : 0x36203B67BCCAD0BD00 Bit length : 70二进制:1101100010000000111011011001111011110011001010110100001011110100000000十进制:998446744073709600000十六进制:0x36203B67BCCAD0BD00位长度:70
Both BigInts are not equal
两个BigInts不相等
How do i solve this problem and represent big numbers correctly in C/C++? 如何解决此问题并在C / C ++中正确表示大数字? i don't want solution in java i just used java for testing purpose because it has bigDecimal Class for very large arbitrary numbers thanks.
我不想要Java解决方案,我只是将Java用于测试目的,因为它具有针对非常大的任意数字的bigDecimal类,谢谢。
If you first convert to double to represent your decimal in exponent representation then of course it is wrong. 如果您首先转换为double以指数形式表示小数,那当然是错误的。 Have a look at https://en.wikipedia.org/wiki/Floating-point_arithmetic .
看看https://en.wikipedia.org/wiki/Floating-point_arithmetic 。 TLDR;
TLDR; If you have more different numbers than states (2^128) you will have gaps.
如果数字比州(2 ^ 128)多得多,您将有空白。 Thats what double is designed for from the beginning.
那就是从一开始就为double设计的。 If you require an exact representations of your actual number do not convert it into a floating point representation.
如果您需要实际数字的精确表示,请不要将其转换为浮点表示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.