哪些编程语言支持任意精度算术?
哪些编程语言支持任意精度算术,您能否给出一个简短的示例来说明如何打印任意数量的数字?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
哪些编程语言支持任意精度算术,您能否给出一个简短的示例来说明如何打印任意数量的数字?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(18)
有些语言内置了这种支持。例如,请查看 java.math.BigDecimal,或 Python 中的decimal.Decimal。
其他语言通常有一个库可以提供此功能。 例如,在 C 中,您可以使用 GMP 或其他选项。
这篇文章的“任意精度软件”部分提供了一个很好的概述您的选择。
Some languages have this support built in. For example, take a look at java.math.BigDecimal in Java, or decimal.Decimal in Python.
Other languages frequently have a library available to provide this feature. For example, in C you could use GMP or other options.
The "Arbitrary-precision software" section of this article gives a good rundown of your options.
数学。
mathematica 不仅具有任意精度,而且默认情况下它具有无限精度。 它将 1/3 之类的东西保留为有理数,甚至涉及 Sqrt[2] 之类的东西的表达式,它都会以符号方式维护,直到您要求一个数字近似值,您可以将其保留到任意位数的小数。
Mathematica.
Not only does mathematica have arbitrary precision but by default it has infinite precision. It keeps things like 1/3 as rationals and even expressions involving things like Sqrt[2] it maintains symbolically until you ask for a numeric approximation, which you can have to any number of decimal places.
在 Common Lisp 中,
printf 格式的“~D~%”将是“%d\n”。 Common Lisp 内置了任意精度算术。
In Common Lisp,
"~D~%" in printf format would be "%d\n". Arbitrary precision arithmetic is built into Common Lisp.
Smalltalk 从一开始就支持任意精度的整数和分数。
请注意,gnu Smalltalk 实现确实在底层使用了 GMP。
我还在为各种方言(Squeak/Pharo Visualworks 和 Dolphin)开发 ArbitraryPrecisionFloat,请参阅 http://www.ArbitraryPrecisionFloat。 squeaksource.com/ArbitraryPrecisionFl.html
Smalltalk supports arbitrary precision Integers and Fractions from the beginning.
Note that gnu Smalltalk implementation does use GMP under the hood.
I'm also developping ArbitraryPrecisionFloat for various dialects (Squeak/Pharo Visualworks and Dolphin), see http://www.squeaksource.com/ArbitraryPrecisionFl.html
Python就有这样的能力。 此处有一个很好的示例。
来自文章:
另外,python 快速入门教程讨论了任意精度: https://docs.python.org/3/library/decimal.html#quick-start-tutorial
并描述了 getcontext:
编辑:添加了有关 getcontext 的说明。
Python has such ability. There is an excellent example here.
From the article:
Also, the python quick start tutorial discusses the arbitrary precision: https://docs.python.org/3/library/decimal.html#quick-start-tutorial
and describes getcontext:
Edit: Added clarification on getcontext.
许多人推荐 Python 的十进制模块,但我建议在任何严肃的数字用途中使用 mpmath 而不是十进制。
Many people recommended Python's decimal module, but I would recommend using mpmath over decimal for any serious numeric uses.
COBOL
一个有 4 位小数的有符号变量。
PL/1
:-) 我不记得其他旧东西了...
除了笑话之外,正如我的示例所示,我认为您不应该根据单个功能来选择编程语言。 事实上,所有像样的和最新的语言都在一些专用类中支持固定精度。
COBOL
a signed variable witch 4 decimals.
PL/1
:-) I can't remember the other old stuff...
Jokes apart, as my example show, I think you shouldn't choose a programming language depending on a single feature. Virtually all decent and recent language support fixed precision in some dedicated classes.
Scheme(lisp 的变体)有一个称为“bignum”的功能。 有许多好的方案实现可用于完整的语言环境和嵌入式脚本选项。
提供一些保证
我可以为MitScheme(也称为 gnu 方案)
PLT方案
切兹计划
Guile(也是一个 gnu 项目)
方案48
Scheme (a variation of lisp) has a capability called 'bignum'. there are many good scheme implementations available both full language environments and embeddable scripting options.
a few I can vouch for
MitScheme (also referred to as gnu scheme)
PLTScheme
Chezscheme
Guile (also a gnu project)
Scheme 48
默认情况下,Ruby 整数和浮点数(从数学上来说:有理数)并不严格依赖于经典的 CPU 相关限制。 在 Ruby 中,如果大小超过经典大小的最大值,整数和浮点数会自动、透明地切换为某些“bignum 类型”。
人们可能想要使用一些合理优化的、“完整的”、多种多样的、使用“bignums”的数学库。 这就是类似 Mathematica 的软件真正发挥其功能的地方。
截至 2011 年,Mathematica 非常昂贵,并且从黑客攻击和重新发布的角度来看受到严重限制,特别是如果您想将数学软件作为小型、低价端、Web 应用程序或开源项目的组件来发布。 如果只需要进行原始数字运算,而不需要可视化,那么存在一种非常可行的 Mathematica 和 Maple 替代方案。 另一种选择是 REDUCE 计算机代数系统,它基于 Lisp,开源且成熟(数十年),并且正在积极开发中(2011 年)。 与 Mathematica 一样,REDUCE 使用符号计算。
对于对 Mathematica 的认可,我想说的是,截至 2011 年,在我看来,Mathematica 在交互式可视化方面是最好的,但我认为从编程的角度来看,即使 Mathematica 是一个开源项目,也有更方便的替代方案。 对我来说,Mahtematica 似乎也有点慢,不适合处理巨大的数据集。 在我看来,Mathematica 的定位是理论数学,而不是现实生活中的数字运算。 另一方面,Mathematica 的出版商 Wolfram Research 正在托管和维护地球上质量最高(如果不是最高质量)的免费使用的数学参考网站之一:http://mathworld.wolfram.com/
Mathematica 附带的在线文档系统也非常好。
当谈到速度时,值得一提的是,据说 REDUCE 甚至可以在 Linux 路由器上运行。 REDUCE 本身是用 Lisp 编写的,但它附带了 2 个它自己的、特定的 Lisp 实现。 其中一个 Lisp 是用 Java 实现的,另一个是用 C 实现的。它们都工作得很好,至少从数学的角度来看是这样。 REDUCE 有 2 种模式:传统的“数学模式”和“程序员模式”,允许通过 REDUCE 自行编写的语言 Lisp 完全访问所有内部结构。
所以,我的观点是,如果人们看看编写数学例程所需的工作量,更不用说所有在 REDUCE 中都成熟的符号计算,那么人们可以节省大量时间(数十年,字面意义上的)。 )通过在REDUCE中完成大部分数学部分,特别是考虑到它已经经过专业数学家长时间的测试和调试,用于在旧时代的超级计算机上进行真正的专业任务的符号计算,并且工作得非常好,真正快,在现代低端计算机上。 它也没有对我造成影响,不像至少一个我不想在这里命名的商业包。
http://www.reduce-algebra.com/
为了说明,符号计算在以下情况中至关重要:实践中,我举了一个通过矩阵求逆求解线性方程组的例子。 要反转矩阵,需要找到行列式。 使用 CPU 直接支持的浮点类型进行的舍入可以将理论上具有逆的矩阵渲染为没有逆的矩阵。 这反过来又引入了一种情况,在大多数情况下,软件可能运行得很好,但如果数据有点“不幸”,那么应用程序就会崩溃,尽管从算法上来说,除了舍入之外,软件没有任何问题。的浮点数。
绝对精度有理数确实有一个严重的限制。 它们执行的计算越多,消耗的内存就越多。 截至 2011 年,我不知道该问题的任何解决方案,除了小心并跟踪对数字执行的操作数,然后对数字进行舍入以节省内存,但必须在非常精确的计算阶段可以避免上述问题。 如果可能,则应在计算最后作为最后一个操作进行舍入。
Ruby whole numbers and floating point numbers (mathematically speaking: rational numbers) are by default not strictly tied to the classical CPU related limits. In Ruby the integers and floats are automatically, transparently, switched to some "bignum types", if the size exceeds the maximum of the classical sizes.
One probably wants to use some reasonably optimized and "complete", multifarious, math library that uses the "bignums". This is where the Mathematica-like software truly shines with its capabilities.
As of 2011 the Mathematica is extremely expensive and terribly restricted from hacking and reshipping point of view, specially, if one wants to ship the math software as a component of a small, low price end, web application or an open source project. If one needs to do only raw number crunching, where visualizations are not required, then there exists a very viable alternative to the Mathematica and Maple. The alternative is the REDUCE Computer Algebra System, which is Lisp based, open source and mature (for decades) and under active development (in 2011). Like Mathematica, the REDUCE uses symbolic calculation.
For the recognition of the Mathematica I say that as of 2011 it seems to me that the Mathematica is the best at interactive visualizations, but I think that from programming point of view there are more convenient alternatives even if Mathematica were an open source project. To me it seems that the Mahtematica is also a bit slow and not suitable for working with huge data sets. It seems to me that the niche of the Mathematica is theoretical math, not real-life number crunching. On the other hand the publisher of the Mathematica, the Wolfram Research, is hosting and maintaining one of the most high quality, if not THE most high quality, free to use, math reference sites on planet Earth: the http://mathworld.wolfram.com/
The online documentation system that comes bundled with the Mathematica is also truly good.
When talking about speed, then it's worth to mention that REDUCE is said to run even on a Linux router. The REDUCE itself is written in Lisp, but it comes with 2 of its very own, specific, Lisp implementations. One of the Lisps is implemented in Java and the other is implemented in C. Both of them work decently, at least from math point of view. The REDUCE has 2 modes: the traditional "math mode" and a "programmers mode" that allows full access to all of the internals by the language that the REDUCE is self written in: Lisp.
So, my opinion is that if one looks at the amount of work that it takes to write math routines, not to mention all of the symbolic calculations that are all MATURE in the REDUCE, then one can save enormous amount of time (decades, literally) by doing most of the math part in REDUCE, specially given that it has been tested and debugged by professional mathematicians over a long period of time, used for doing symbolic calculations on old-era supercomputers for real professional tasks and works wonderfully, truly fast, on modern low end computers. Neither has it crashed on me, unlike at least one commercial package that I don't want to name here.
http://www.reduce-algebra.com/
To illustrate, where the symbolic calculation is essential in practice, I bring an example of solving a system of linear equations by matrix inversion. To invert a matrix, one needs to find determinants. The rounding that takes place with the directly CPU supported floating point types, can render a matrix that theoretically has an inverse, to a matrix that does not have an inverse. This in turn introduces a situation, where most of the time the software might work just fine, but if the data is a bit "unfortunate" then the application crashes, despite the fact that algorithmically there's nothing wrong in the software, other than the rounding of floating point numbers.
The absolute precision rational numbers do have a serious limitation. The more computations is performed with them, the more memory they consume. As of 2011 I don't know any solutions to that problem other than just being careful and keeping track of the number of operations that has been performed with the numbers and then rounding the numbers to save memory, but one has to do the rounding at a very precise stage of the calculations to avoid the aforementioned problems. If possible, then the rounding should be done at the very end of the calculations as the very last operation.
在 PHP 中你有 BCMath。 您不需要加载任何 dll 或编译任何模块。
支持任意大小和精度的数字,以字符串形式表示
In PHP you have BCMath. You not need to load any dll or compile any module.
Supports numbers of any size and precision, represented as string
显然 Tcl 也有它们,从版本 8.5 开始,由 LibTomMath 提供:
Apparently Tcl also has them, from version 8.5, courtesy of LibTomMath:
有几个 Javascript 库可以处理任意精度算术。
例如,使用我的 big.js 库:
There are several Javascript libraries that handle arbitrary-precision arithmetic.
For example, using my big.js library:
在 R 中,您可以使用 Rmpfr 包:
您可以在此处找到小插图:使用 R 进行任意精确计算:
Rmpfr 套餐
In R you can use the Rmpfr package:
You can find the vignette here: Arbitrarily Accurate Computation with R:
The Rmpfr Package
Java 本身可以使用 BigDecimal 进行 bignum 运算。 GMP 是使用 C/C++ 的 bignum 事实上的标准库。
Java natively can do bignum operations with BigDecimal. GMP is the defacto standard library for bignum with C/C++.
如果您想在 .NET 世界中工作,您仍然可以使用 java.math.BigDecimal 类。 只需添加对 vjslib 的引用(在框架中),然后您就可以使用 java 类。
最棒的是,它们可以在任何 .NET 语言中使用。 例如在 C# 中:
If you want to work in the .NET world you can use still use the java.math.BigDecimal class. Just add a reference to vjslib (in the framework) and then you can use the java classes.
The great thing is, they can be used fron any .NET language. For example in C#:
(免费)基本程序 x11 basic ( http://x11-basic.sourceforge.net/ ) 具有任意整数精度。 (还有一些有用的命令,例如 nextprime(abcd...pqrs))
The (free) basic program x11 basic ( http://x11-basic.sourceforge.net/ ) has arbitrary precision for integers. (and some useful commands as well, e.g. nextprime( abcd...pqrs))
IBM 的解释脚本语言 Rexx 提供了数字的自定义精度设置。 https://www.ibm.com/docs/ en/zos/2.1.0?topic=instructions-numeric。
该语言运行在大型机和个人电脑操作系统上,具有非常强大的解析和变量处理以及扩展包。 Object Rexx 是最新的实现。 来自 https://en.wikipedia.org/wiki/Rexx 的链接
IBM's interpreted scripting language Rexx, provides custom precision setting with Numeric. https://www.ibm.com/docs/en/zos/2.1.0?topic=instructions-numeric.
The language runs on mainframes and pc operating systems and has very powerful parsing and variable handling as well as extension packages. Object Rexx is the most recent implementation. Links from https://en.wikipedia.org/wiki/Rexx
Haskell 对内置的任意精度算术有很好的支持,并且使用它是默认行为。 在 REPL 上,无需导入或设置:(
请自行尝试 https://tryhaskell.org/)
如果您正在编写存储在文件中的代码并且想要打印一个数字,则必须首先将其转换为字符串。
show
函数可以做到这一点。(在 code.world 上亲自尝试:https://www.code.world/haskell#Pb_gPCQuqY7r77v1IHH_vWg)
此外,Haskell 的
Num
抽象可让您尽可能推迟决定使用哪种类型。Haskell has excellent support for arbitrary-precision arithmetic built in, and using it is the default behavior. At the REPL, with no imports or setup required:
(try this yourself at https://tryhaskell.org/)
If you're writing code stored in a file and you want to print a number, you have to convert it to a string first. The
show
function does that.(try this yourself at code.world: https://www.code.world/haskell#Pb_gPCQuqY7r77v1IHH_vWg)
What's more, Haskell's
Num
abstraction lets you defer deciding what type to use as long as possible.