简体   繁体   English

如果不能容纳足够多的有效数字,那么双精度数如何代表比小数更高的数字?

[英]How can doubles represent higher numbers than decimals if they can't hold as many significant figures?

I may very well have not the proper understanding of significant figures , but the book 我很可能没有对重要人物的正确理解,但是这本书

C# 6.0 in a Nutshell by Joseph Albahari and Ben Albahari (O'Reilly). Joseph Albahari和Ben Albahari(O'Reilly)撰写的C#6.0概述。

Copyright 2016 Joseph Albahari and Ben Albahari, 978-1-491-92706-9. 版权所有2016 Joseph Albahari和Ben Albahari,978-1-491-92706-9。

provides the table below for comparing double and decimal : 提供下表用于比较doubledecimal

在此处输入图片说明

Is it not counter-intuitive that, on the one hand, a double can hold a smaller quantity of significant figures , while on the other it can represent numbers way bigger than decimal, which can hold a higher quantity of significant figures ? 一方面,双精度数可以容纳较少数量的有效数字 ,另一方面,它可以表示大于十进制数的数字,而后者可以容纳更多数量的有效数字 ,这不是违反直觉吗?

Imagine you were told you can store a value, but were given a limitation: You can only store 10 digits, 0-9 and a negative symbol. 假设您被告知可以存储一个值,但是却受到限制:您只能存储10位数字,0-9和一个负号。 You can create the rules to decode the value, so you can store any value. 您可以创建规则以对值进行解码,因此可以存储任何值。

The first way you store things is simply as the value xxxxxxxxxx , meaning the number 123 is stored as 0000000123 . 存储事物的第一种方法就是将其简单地存储为xxxxxxxxxx值,这意味着数字123被存储为0000000123 Simple to store and read. 易于存储和阅读。 This is how an int works. 这就是int工作方式。

Now you decide you want to store fractional numbers, so you change the rules a bit. 现在,您决定要存储小数,因此可以稍微更改规则。 Now you store xxxxxxyyyy , where x is the integer portion and y is the fractional portion. 现在,您存储xxxxxxyyyy ,其中x是整数部分, y是小数部分。 So, 123.98 would be stored as 0001239800 . 因此,123.98将被存储为0001239800 This is roughly how a Decimal value works. 这大致是Decimal值的工作方式。 You can see the largest value I can store is 9999999999 , which translates to 999999.9999. 您可以看到我可以存储的最大值是9999999999 ,它转换为999999.9999。 This means I have a hard upper limit on the size of the value, but the number of the significant digits is large at 10. 这意味着我对值的大小有严格的上限,但是有效位数为10。

There is a way to store larger values, and that's to store the x and y components for the formula 有一种方法可以存储较大的值,即存储公式的x和y分量 x * 10 ^ y in xxxxxxyyyy . xxxxxxyyyy So, to store 123.98, you need to store 01239800-2 , which I can calculate as 因此,要存储123.98,您需要存储01239800-2 ,我可以将其计算为 12398 * 10 ^ -2 . This means I can store much bigger numbers by changing 'y', but the number of significant digits is basically fixed at 6. This is basically how a double works. 这意味着我可以通过更改“ y”来存储更大的数字,但是有效数字的数量基本上固定为6。这基本上就是double工作方式。

The answer lies in the way that doubles are encoded. 答案在于对double进行编码的方式。 Rather than just being a direct binary representation of a number, they have 3 parts: sign, exponent, and fraction. 它们具有3个部分,而不仅仅是数字的直接二进制表示形式:符号,指数和分数。

  • The sign is obvious, it controls + or -. 该符号很明显,它控制+或-。

  • The fraction part is also obvious. 分数部分也很明显。 It's binary fraction that represents a number in between 0 and 1. 它是二进制分数,表示介于0和1之间的数字。

  • The exponent is where the magic happens. 指数就是魔术发生的地方。 It signifies a scaling factor. 它表示比例因子。

The final float calculation comes out to (-1)^$sign * (1 + $fraction) * 2 ^$exponent 最终的浮动计算结果为(-1)^ $ sign *(1 + $ fraction)* 2 ^ $ exponent

This allows much higher values than a straight decimal number because of the exponent. 由于指数的原因,这允许比直接的十进制数字高得多的值。 There's a lot of reading out there on why this works and how to do addition and multiplication with these encoded numbers. 那里有很多关于为什么这样做以及如何对这些编码数字进行加法和乘法的读物。 Google around for "IEEE floating point format" or whatever topic you need. Google围绕“ IEEE浮点格式”或您需要的任何主题。 Hope that helps! 希望有帮助!

The Range has nothing to do with the precision. 范围与精度无关。 Double has a binary representation (base 2). Double具有二进制表示形式(以2为底)。 Not all numbers can be represented exactly as we humans know them in the decimal format. 并非所有数字都可以完全按照人类所知道的十进制格式表示。 Not to mention and accumulated rounding errors of addition and division. 更不用说和加法和除法的累积舍入误差。 A larger range means a greater MAX VALUE and a smaller MIN VALUE than decimal. 较大的范围表示比十进制更大的MAX VALUE和最小MIN VALUE。

Decimal on the other side is (base 10). 另一边的小数是(以10为底)。 It has a smaller range (smaller MAX VALUE and larger MIN VALUE). 它具有较小的范围(较小的MAX VALUE和较大的MIN VALUE)。 This has nothing to do with precision, since it is not represented using floating binary point representation , it can represent numbers more precisely and though is recommended for human-made numbers and calculations. 这与精度无关,因为它不是使用浮点二进制点表示法表示的 ,它可以更精确地表示数字,尽管建议将其用于人造数字和计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM