平方根的任意精度 [英] Arbitrary precision of square roots

查看:171
本文介绍了平方根的任意精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

decimal.Decimal(math.sqrt(2))屈服时我很失望

Decimal('1.4142135623730951454746218587388284504413604736328125')

,小数点后15位后的数字有误. (尽管能为您提供超过15位的数字!)

and the digits after the 15th decimal place turned out wrong. (Despite happily giving you much more than 15 digits!)

在Python中,如何获取sqrt(n)的十进制扩展中的前m 正确个数字?

How can I get the first m correct digits in the decimal expansion of sqrt(n) in Python?

推荐答案

对小数使用sqrt方法

>>> from decimal import *
>>> getcontext().prec = 100
>>> Decimal(2).sqrt()
Decimal('1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573')
>>> 

这篇关于平方根的任意精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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