为什么不实施理性数字并将其存储为零信息损失的分数? [英] Why aren't rational numbers implemented and stored as fractions with zero loss of information?

查看:131
本文介绍了为什么不实施理性数字并将其存储为零信息损失的分数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这有点假设,但我想知道为什么没有语言我知道它。

I know this is a bit hypothetical but I am wondering why no language I know does it.

例如,你想存储1/3。给程序员一个选项,指定它为1/3,存储1和3.像

For example, you want to store 1/3. Give the programmer an option to specify it as 1/3, and store 1 and 3. Something like

struct float {
    int numerator;
    int denominator;
};

理性数字运算变得非常简单,更准确!

Rational number arithmetic becomes really easy and considerably more accurate!

这将解决与浮点数的精度和存储限制相关的许多问题,我不会看到它引入任何新的问题。

This would solve so many problems related to the precision and storage limitations of floating point numbers, and I dont see it introducing any new problems as well!

因此,我的问题:为什么不实施理性数字并将其存储为零信息丢失的分数?

正如Joe问,而其他人也可能指出,我并不是要取代现有的系统,而是补充它。

As Joe asked, and others might also point out, I do not mean this to replace existing system, but to complement it.

存储 pi

A:很多时候,我只是存储 1/3 ,而不是 pi pi 可以以旧的方式存储, 1/3 以新的方式存储。

A: So many times, I am just storing 1/3 and not pi. pi can be stored the old way, and 1/3 in the new way.

推荐答案

默认情况下,它们不以这种方式存储的原因是,可以适应固定位集合的有效值范围较小。您的 float 类可以存储1 / MAXINT和MAXINT(加或减)之间的数字。 C / C ++ float 可以表示1E + 37和1E-37(加或减)之间的数字。换句话说,标准 float 可以代表值26个数量级和26个数量级小于你的,尽管采取一半的位数。通常,能够表示非常大和非常小的值比更精确地更加方便。这是特别真实的,因为舍入往往给我们正确的答案与小分数,如1/3。在g ++中,下面给出1:

The reason they are not stored this way by default is that the range of valid values that can fit in a fixed set of bits is smaller. Your float class can store numbers between 1/MAXINT and MAXINT (plus or minus). A C/C++ float can represent numbers between 1E+37 and 1E-37 (plus or minus). In other words, a standard float can represent values 26 orders of magnitude bigger and 26 orders of magnitude smaller then yours despite taking half the number of bits. In general, it's more convenient to be able to represent very large and very small values than to be perfectly precise. This is especially true since rounding tends to give us the right answers with small fractions like 1/3. In g++, the following gives 1:

std::cout << ((1.0/3.0) * 3.0) << std::endl; 

请记住,C ++中的类型具有固定大小的位。因此,32位数据类型最多具有MAX_UINT个值。如果你改变它的表示方式,你只是改变哪些值可以精确表示,而不是增加它们。你不能挤在更多的,因此不能更精确。你的交易能够精确地表示1/3,因为不能精确地表示其他值,例如5.4235E + 25。

Remember that types in C++ have a fixed size in bits. Thus a datatype in 32 bits has at most MAX_UINT values. If you change the way it is represented, you're just changing which values can be precisely represented, not increasing them. You can't cram more in, and thus can't be "more precise". You trade being able to represent 1/3 precisely for not being able to represent other values precisely, like 5.4235E+25.

这是真的,你的 float 可以更准确地表示1E-9和1E + 9之间的值(假设为32位整数),但代价是完全无法表示此范围之外的值。更糟糕的是,标准 float 总是有6位数的精度,你的 float 为零的值。 (注意,你使用的是 float 的两倍。)

It is true that your float can represent values more precisely between 1E-9 and 1E+9 (assuming 32 bit ints) but at a cost of being completely unable to represent values outside of this range. Worse, while the standard float always has 6 digits of precision, your float would have precision that varied depending on how close to zero the values were. (And note that you are using twice the bits that float does.)

int s。同样的参数适用于64位 int s。)

(I'm assuming 32 bit ints. Same argument applies for 64 bit ints.)

编辑:另请注意,大多数数据人使用 float s不是精确的。如果你正在读一个传感器的数据,你已经不精确,所以即将完美表示的价值是无意义的。如果你在任何类型的计算上下文中使用 float ,这都没关系。如果你的目的是在屏幕上显示一点文字的1/3,那么完美描述1/3没有意义。

Edit: Also note that most data people use floats for is not precise anyway. If you are reading data off of a sensor, you've already got imprecision, so being about to "perfectly" represent the value is pointless. If you are using a float in any sort of computing context, it's not going to matter. There is no point in perfectly describing '1/3' if your purpose is to display a bit of text 1/3rd of the way across the screen.

谁真正需要完美的精度是数学家,他们通常有软件,给他们这个。很少有其他人需要超越 double 给出的精度。

The only people who really need perfect precision are mathematicians, and they generally have software that gives them this. Very few others need precision beyond what double gives.

这篇关于为什么不实施理性数字并将其存储为零信息损失的分数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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