8个字节怎么能容纳302个十进制数字?(欧拉挑战16) [英] How can 8 bytes hold 302 decimal digits? (Euler challenge 16)

查看:53
本文介绍了8个字节怎么能容纳302个十进制数字?(欧拉挑战16)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c++ pow(2,1000)对于双倍来说是正常的,但它正在起作用.为什么?

所以我已经学习 C++ 几个星期了,但数据类型仍然让我感到困惑.

首先是一件小事:0xbadc0de 在另一个线程中发布的代码对我不起作用.首先 pow(2,1000) 给了我 这个重载函数pow"的多次实例匹配参数列表.

我通过更改 pow(2,1000) -> pow(2.0,1000) 修复了它看起来不错,我运行它并得到这个:

http://i.stack.imgur.com/bbRat.png

代替

<预> <代码> 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

它缺少很多值,可能是什么原因造成的?

但现在是真正的问题.我想知道 302 位长的数字如何适合双精度(8 个字节)?0xFFFFFFFFFFFFFFFF = 18446744073709551616 那么这个数字怎么会比这个大呢?

我认为它与浮点数编码的东西有关.如果不是 0xFFFFFFFFFFFFFFFF,那么 最大 可能存储在 8 个字节中的数字是多少?

解决方案

8 个字节包含 64 位信息,因此您可以使用这些位存储 2^64 ~ 10^20 个唯一项.这些项目可以很容易地解释为从 02^64 - 1 的整数.所以你不能在 8 个字节中存储 302 个十进制数字;010^303 - 1 之间的大多数数字不能这样表示.

浮点数可以容纳 302 位十进制数字的近似值;这是因为它们分别存储尾数和指数.这种表示中的数字存储一定数量的有效数字(双打为 15-16,如果我没记错的话)和指数(可以进入数百个,记忆服务).然而,如果一个十进制数是 X 字节长,那么它只能区分 2^(8X) 不同的值......不太可能完全代表具有 302 个十进制数字的整数.

要表示这样的数字,您必须使用更多位:实际上大约 1000 或 125 个字节.

c++ pow(2,1000) is normaly to big for double, but it's working. why?

So I've been learning C++ for couple weeks but the datatypes are still confusing me.

One small minor thing first: the code that 0xbadc0de posted in the other thread is not working for me. First of all pow(2,1000) gives me this more than once instance of overloaded function "pow" matches the argument list.

I fixed it by changing pow(2,1000) -> pow(2.0,1000) Seems fine, i run it and get this:

http://i.stack.imgur.com/bbRat.png

Instead of

10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376

it is missing a lot of the values, what might be cause that?

But now for the real problem. I'm wondering how can 302 digits long number fit a double (8 bytes)? 0xFFFFFFFFFFFFFFFF = 18446744073709551616 so how can the number be larger than that?

I think it has something to do with the floating point number encoding stuff. Also what is the largest number that can possibly be stored in 8 bytes if it's not 0xFFFFFFFFFFFFFFFF?

解决方案

Eight bytes contain 64 bits of information, so you can store 2^64 ~ 10^20 unique items using those bits. Those items can easily be interpreted as the integers from 0 to 2^64 - 1. So you cannot store 302 decimal digits in 8 bytes; most numbers between 0 and 10^303 - 1 cannot be so represented.

Floating point numbers can hold approximations to numbers with 302 decimal digits; this is because they store the mantissa and exponent separately. Numbers in this representation store a certain number of significant digits (15-16 for doubles, if I recall correctly) and an exponent (which can go into the hundreds, of memory serves). However, if a decimal is X bytes long, then it can only distinguish between 2^(8X) different values... unlikely enough for exactly representing integers with 302 decimal digits.

To represent such numbers, you must use many more bits: about 1000, actually, or 125 bytes.

这篇关于8个字节怎么能容纳302个十进制数字?(欧拉挑战16)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆