C ++的平方根算法? [英] square root algorithm for c++?

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

问题描述

我需要一种非常有效的方法来计算最多100个小数位的平方根(并且100%正确).我一直在互联网上寻找平方根算法.实际上,我想要长除法的c ++适应,我们在高中学习.到目前为止,这对我来说似乎最有希望.但这对我的编程技能来说实在是太多了(我只知道非常基础的c ++,实际上,我想测试一下我用计算机制作的数学公式,以便它产生很高的值.我什至测试了估计值并取平均值的方法,但仅10位数字就失败了.

i needed a pretty much efficient way to calculate square roots upto 100 decimal places(and 100% correct).i have been searching for square root algorithms on the internet.actually, i want a c++ adaptation of long division method that we learn in high school.by far,it seems most promising to me.but it''s too much for my programming skills(i know only very basic c++,actually,i want to test the mathematical formula i made using a computer so that it produces high values.i even tested the method in which a value is estimated and average is taken,but it fails after only 10 digits).

推荐答案

您在这里有两种选择-一种是使用一位数字方法(如Richard提到的页面上所示)滚动自己的代码,但从您所说的开始并不是那么有经验,并且实施起来如果不是根本性的困难的话,看起来也有些怪异.

另一种选择是使用任意精度的算术库.此页面 [ ^ ]有一些您可能要考虑的内容.由于您不具备C ++的经验,您最终可能会以为您已经将问题从如何计算高精度平方根"更改为如何构建和使用此Sodding库?"

如果您想继续使用C ++,那么研究如何使用库可能是一个更好的选择.如果要保持使用该语言进行数值编程,那么现在自己滚动可能会更好.

并且...(最后一点可能会使整个事情变得多余)它一定是C ++吗?相当多的语言都支持内置的任意数字.例如,Python有一个类(Decimal),可以将根平方根提高到任意精度.可能值得学习足够的Python,以便您可以使用它而不是与C ++对抗.
You''ve got two choices here - one is to roll your own using a single digit method (as shown on the page Richard mentioned) but from what you''ve said you''re not that experienced and implementing it looks a bit fiddly if not fundamentally difficult.

Another option would be to use an arbitrary precision arithmetic library. This page [^] has some you might like to consider. As you''re not that experienced with C++ you could end up thinking you''ve changed the problem from "How do I calculate a high precision square root" to "how do I build and use this sodding library?"

If you want to keep using C++ then working out how to use libraries is probably a better bet. If you want to keep numerical programming in the language then rolling your own might be better for now.

And... (final point that might make the whole thing redundant) does it have to be C++? A fair number of languages have support for arbitrary numbers built in. Python, for example, has a class (Decimal) that can can crank you square roots out to an arbitrary precision. It might be worth learning enough Python so you can use that rather than fighting with C++.


我的建议是首先确定算法(无需担心代码)并编写步骤用自然语言.只有当您完成该操作并且理解了每个步骤后,您才能将其转换为代码.
My suggestion would be to work out the algorithm first (without worrying about code) and get the steps written down in natural language. Only when you have that done and you understand each step can you turn it into code.


@Richard MacCutchan,谢谢!我想我已经找到了解决方案.i必须处理set从1到2的数,所以,我首先将一个名为int的对,然后将等于它的数放进去.该对将包含整数x(要平方的数字)的整数值,我将找到答案(a)的第一位数字.然后我将x =(x-pair)* 100,pair = x,这将给我下一对.继续计算,我将计算答案并将a =( 10 * a)+ ans
@Richard MacCutchan, thanks!!i think i have found the solution .i have to deal with set of numbers from 1 to 2.so,first i''ll take an int named pair,and put the number equal to it.the pair would contain integral value to the number x(the number to be square rooted),and i will find the first digit of answer(a).then i will put x=(x-pair)*100,and pair=x,which would give me next pair.continuing the calculation,i would calculate the answer and put a=(10*a)+ans


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

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