在软件中模拟数值运算

发布于 2024-09-29 15:12:09 字数 522 浏览 0 评论 0原文

我们在程序中执行的数字运算受到语言为给定数据类型指定的字节数(或者硬件支持的字节数)的限制。假设我可以使用整数来计算我的薪水(即使是“短”也足以支付一年的收入!;))但不能对比尔·盖茨的财富进行同样的计算。所以,我们追求诸如“长长”之类的东西。但我们不是仍然受制于提供给我们的位数吗?

那么,如果我在软件中模拟数值运算怎么样?假设有一个类可以对 1000 位数字进行抽象并进行数值运算...... 当然它会太慢,但我不太担心复杂性,而是更多地关注可计算性......

也许我可以用它在几个月内计算 PI 到 1000 位精度,或者在几年内计算梅森素数,并采取家里 10 万美元;)

现在我的问题是, 1)是否已经有任何这样的库可以做这种事情(在C/C++中)。 2)如果我开始实施一项,您对我有什么建议吗? (+、-、*、/、%、<<、>> 操作应该足够了)


PS:

  1. 我是 C/C++ 程序员。

  2. 这个限制从我学生时代就开始困扰我。

The numerical operations we do in our programs are limited by the number of bytes that a language specifies for a given datatype (or maybe hardware supports). Say I can use integer to do calculations on my paycheck (even "short" is more than enough for a year's earning!!! ;) ) but can't do the same with Bill Gates wealth. So, we go for things like long long and stuff. But aren't we still at the mercy of number of bits that are given to us.

So, how about if I emulate numerical operations in software? Say a class that abstracts and can do numerical operations on numbers with 1000s of digits...
Of course it will be too too slow, but I am not much worried about complexity but looking more at just computability...

Maybe I can use it to calculate PI to 1000 digit accuracy in a months or a Mersenne Primes in few years and take home $100K ;)

So now my question,
1) Are there already any such libraries to do this kind of stuff out there (In C/C++).
2) If I go about implementing one, do you have any suggestions for me? (+, -, *, /, %, <<, >> operations should enough I guess)


PS:

  1. I am C/C++ programmer.

  2. And this limitation started bugging me from my school days.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倒数 2024-10-06 15:12:09

此类数据类型称为任意精度数。在 Java 中,有类 BigDecimalBigInteger 处理数字级别的基本运算(+、-、*、/)。它们没有“内置”尺寸限制。它们实际上并没有那么慢,并且被用于许多现实世界的领域。

C/C++ 没有内置的,但有很多库。请参阅此处的列表:
http://en.wikipedia.org/wiki/Arbitrary- precision_arithmetic#Libraries

Such datatypes are known as Arbitrary-precision numbers. In Java, there are the classes BigDecimal and BigInteger which handle basic operations (+, -, *, /) on digit level. They have no 'built-in' size limitation. They are actually not that slow and are used in a lot of real-world domains.

C/C++ don't have it built-in but there are a lot of libraries out there. See a list here:
http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic#Libraries

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文