关于开平方的算法 求助

huhuhu5 2003-11-26 02:38:30
最近想做一个高精度计算,需要做一个高精度浮点数开平方的函数
麻烦问一下 谁有好的开平方的算法 介绍介绍 手开方太浪费资源了 不太想用
呵呵 如果真的没办法的化 我只能那么做了

不用源程序 说说思路就好 谢谢先
...全文
446 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jeckyz 2003-11-26
  • 打赏
  • 举报
回复
不知精度要高到什么程度,要快的话有个近似工式,我在一本数据结构的书上看过,具体就不记得了,好像叫《C++算法描述》,大概300页的那本。
kuangjingbo 2003-11-26
  • 打赏
  • 举报
回复
楼上的:麦克劳林展开式不可以吧!
里面会出现1/x的形式,麦克劳林是泰勒公式基于0的展开式,由于1/0没有意义,所以不可以。
那么就使用泰勒展开式吧,随便选一个点。精度可以随展开项的多少自己来定义。
孩皮妞野 2003-11-26
  • 打赏
  • 举报
回复
用麦克劳林展开式吧。 应该比迭代快一些。
liem 2003-11-26
  • 打赏
  • 举报
回复
用迭代行不行?
x1=(a/x0+x0)/2
xiaoyunet 2003-11-26
  • 打赏
  • 举报
回复
sqrt()
xiaoyunet 2003-11-26
  • 打赏
  • 举报
回复
不知道标准库里的开平方函数能不能满足你的要求。
xujunfeng008 2003-11-26
  • 打赏
  • 举报
回复
计算器不是有开平方的那个按扭吗
program2100 2003-11-26
  • 打赏
  • 举报
回复
高等数学中有相关的资料,可以用多项表达式来替换开方函数。
gladiatorcn 2003-11-26
  • 打赏
  • 举报
回复
同意楼上,不过奔腾有自己的浮点开方指令,速度当然没得说,比库函数高效。以下是P4的fsqrt的instruction:

FSQRT—Square Root
Description
Computes the square root of the source value in the ST(0) register and stores the result in ST(0).
The following table shows the results obtained when taking the square root of various classes of
numbers, assuming that neither overflow nor underflow occurs.
NOTES:
F Means finite floating-point value.
* Indicates floating-point invalid-arithmetic-operand (#IA) exception.
Operation
ST(0) ← SquareRoot(ST(0));
FPU Flags Affected
C1 Set to 0 if stack underflow occurred.
Indicates rounding direction if inexact-result exception (#P) is generated:
0 ← not roundup; 1 ← roundup.
C0, C2, C3 Undefined.
Opcode Instruction Description
D9 FA FSQRT Computes square root of ST(0) and stores the result in
ST(0)
SRC (ST(0)) DEST (ST(0))
−∞ *
−F *
−0 −0
+0 +0
+F +F
+∞ +∞
NaN NaN
INSTRUCTION SET REFERENCE
FSQRT—Square Root (Continued)
Floating-Point Exceptions
#IS Stack underflow occurred.
#IA Source operand is an SNaN value or unsupported format.
Source operand is a negative value (except for −0).
#D Source operand is a denormal value.
#P Value cannot be represented exactly in destination format.
Protected Mode Exceptions
#NM EM or TS in CR0 is set.
Real-Address Mode Exceptions
#NM EM or TS in CR0 is set.
Virtual-8086 Mode Exceptions
#NM EM or TS in CR0 is set.
liangbch 2003-11-26
  • 打赏
  • 举报
回复
1。几乎所有的 c 运行时刻库都包含开平方的函数sqrt(double)
2. double型 可表示的数的范围大致是-10^308 -- 10^308,精度为10进制 15-16位有效数字,并不是30位。
3。在 x86 系列中,大部分cpu(486以上),是采用浮点处理器来实现的,速度很快。
4。如果自己写一个计算任意精度的开平方程序,尤其是在精度很高的情况下,应该先用查表法,求得初值,在使用牛顿迭代公式逐步求精,不要使用泰勒公式,泰勒公式收敛的很慢。
假如,泰勒公式每求一项,增加8位精度,如果需要达到256位精度,需要计算256/8项,而使用牛顿迭代公式时,如果初值达到8位精度,只需要5次,每次有效数字增加1倍。
5。此类问题,最好到专题开发/数据结构与算法版块 提问,而且此类问题多半有人问过,查一查就可以找到答案,给出一个链接http://expert.csdn.net/Expert/TopicView1.asp?id=2392406 (求开平方的算法)
daily1980 2003-11-26
  • 打赏
  • 举报
回复
sqrt()
北极猩猩 2003-11-26
  • 打赏
  • 举报
回复
用泰勒公式可以算到任意精度,不过要数据表示上支持才行.
双精度浮点数的精度大概是接近30位有效数字吧

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧