任意精度小数运算(C / C ++)中的浮点数与有理数 [英] Floats vs rationals in arbitrary precision fractional arithmetic (C/C++)

查看:201
本文介绍了任意精度小数运算(C / C ++)中的浮点数与有理数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于有两种实现AP小数的方式,一种是模拟 double 数据类型的存储和行为,只包含更多字节,另一种是使用现有的整数APA实现将小数表示为有理数,即一对整数,分子和分母,这两种方式中哪一种在性能方面更有可能提供有效的算术? (内存使用确实是个小问题。)

Since there are two ways of implementing an AP fractional number, one is to emulate the storage and behavior of the double data type, only with more bytes, and the other is to use an existing integer APA implementation for representing a fractional number as a rational i.e. as a pair of integers, numerator and denominator, which of the two ways are more likely to deliver efficient arithmetic in terms of performance? (Memory usage is really of minor concern.)

我知道现有的C / C ++库,其中一些库提供带浮点数的小数APA,另一些库则提供理性(但是它们都没有定点APA),当然我可以将依赖于浮动实现的库与利用理性实现的库进行基准测试,但是结果在很大程度上取决于那些特定库的实现细节我将不得不从几乎十个可用的中随机选择。因此,这是我感兴趣的两种方法(或三种,如果考虑定点APA)的理论的优缺点。

I'm aware of the existing C/C++ libraries, some of which offer fractional APA with "floats" and other with rationals (none of them features fixed-point APA, however) and of course I could benchmark a library that relies on "float" implementation against one that makes use of rational implementation, but the results would largely depend on implementation details of those particular libraries I would have to choose randomly from the nearly ten available ones. So it's more theoretical pros and cons of the two approaches that I'm interested in (or three if take into consideration fixed-point APA).

推荐答案

问题是标题中提到的任意精度是什么意思。它的意思是任意的,但在编译时预先确定并在运行时固定吗?还是说无限,即在运行时可扩展以表示任何有理数?

The question is what you mean by arbitrary precision that you mention in the title. Does it mean "arbitrary, but pre-determined at compile-time and fixed at run-time"? Or does it mean "infinite, i.e. extendable at run-time to represent any rational number"?

在前一种情况下(精度可在编译时自定义,但此后固定)我想说,最有效的解决方案之一实际上是定点算法(即您提到的两个都不是)。

In the former case (precision customizable at compile-time, but fixed afterwards) I'd say that one of the most efficient solutions would actually be fixed-point arithmetic (i.e. none of the two you mentioned).

首先,定点算术不需要任何专用库即可进行基本算术运算。这只是覆盖整数算术的概念。这意味着,如果您确实需要在点后添加很多数字,则可以使用任何大整数库,将所有数据乘以2 ^ 64,然后您基本上可以立即得到定点运算符,该运算符为64位二进制数点(至少与算术运算有关,对乘法和除法进行一些额外的调整)。通常,这比浮点或有理表示有效得多。

Firstly, fixed-point arithmetic does not require any dedicated library for basic arithmetic operations. It is just a concept overlaid over integer arithmetic. This means that if you really need a lot of digits after the dot, you can take any big-integer library, multiply all your data, say, by 2^64 and you basically immediately get fixed-point arithmetic with 64 binary digits after the dot (at least as long as arithmetic operations are concerned, with some extra adjustments for multiplication and division). This is typically significantly more efficient than floating-point or rational representations.

还要注意,在许多实际应用中,乘法运算通常伴随除法运算(如 x = y * a / b )彼此补偿,这意味着通常不需要对此类乘法和除法进行任何调整。这也有助于定点算法的效率。

Note also that in many practical applications multiplication operations are often accompanied by division operations (as in x = y * a / b) that "compensate" for each other, meaning that often it is unnecessary to perform any adjustments for such multiplications and divisions. This also contributes to efficiency of fixed-point arithmetic.

第二,定点算法在整个范围内提供统一的精度。对于浮点表示法或有理表示法而言,情况并非如此,在某些应用程序中,这对于后两种方法可能是一个重大缺点(或者是一种好处,具体取决于您的需求)。

Secondly, fixed-point arithmetic provides uniform precision across the entire range. This is not true for either floating-point or rational representations, which in some applications could be a significant drawback for the latter two approaches (or a benefit, depending on what you need).

因此,再次,您为什么只考虑浮点和有理表示。有什么可以阻止您考虑定点表示的吗?

So, again, why are you considering floating-point and rational representations only. Is there something that prevents you from considering fixed-point representation?

这篇关于任意精度小数运算(C / C ++)中的浮点数与有理数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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