Mat*_*ory 20 programming-languages bigdecimal arbitrary-precision
哪些编程语言支持任意精度算术,你能举一个简单的例子来说明如何打印任意数量的数字吗?
Log*_*gan 14
有些语言内置了这种支持.例如,查看Java 中的java.math.BigDecimal或Python中的decimal.Decimal.
其他语言经常有一个库可用于提供此功能.例如,在C中,您可以使用GMP或其他选项.
的"任意精度的软件"部分这篇文章给你的选择一个好的破败.
dre*_*ves 11
数学.
N[Pi, 100]
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
Run Code Online (Sandbox Code Playgroud)
mathematica不仅具有任意精度,而且默认情况下它具有无限精度.它保留1/3作为有理数甚至表达式,如Sqrt [2],它保持符号,直到你要求数字近似,你可以有任意数量的小数位.
在Common Lisp中,
(format t "~D~%" (expt 7 77))
Run Code Online (Sandbox Code Playgroud)
printf格式中的"~D~%"将为"%d \n".Common Lisp中内置了任意精度算法.
小智 6
Smalltalk从一开始就支持任意精度的整数和分数.请注意,gnu Smalltalk实现确实使用了GMP.我也在为各种方言(Squeak/Pharo Visualworks和Dolphin)开发ArbitraryPrecisionFloat,参见http://www.squeaksource.com/ArbitraryPrecisionFl.html
Python有这样的能力.有一个很好的例子在这里.
来自文章:
from math import log as _flog
from decimal import getcontext, Decimal
def log(x):
if x < 0:
return Decimal("NaN")
if x == 0:
return Decimal("-inf")
getcontext().prec += 3
eps = Decimal("10")**(-getcontext().prec+2)
# A good initial estimate is needed
r = Decimal(repr(_flog(float(x))))
while 1:
r2 = r - 1 + x/exp(r)
if abs(r2-r) < eps:
break
else:
r = r2
getcontext().prec -= 3
return +r
Run Code Online (Sandbox Code Playgroud)
此外,python快速入门教程讨论了任意精度:http://docs.python.org/lib/decimal-tutorial.html
并描述了getcontext:
getcontext()函数访问当前上下文并允许更改设置.
编辑:添加了关于getcontext的说明.
归档时间: |
|
查看次数: |
9967 次 |
最近记录: |