编写多维数据集根函数有问题 [英] Trouble writing cube root function

查看:70
本文介绍了编写多维数据集根函数有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下伪代码编写名为 double my_cbrt_1(double n)的多维数据集根函数:

I'm trying to write a cube root function named double my_cbrt_1(double n) using the following pseudocode:

x = 1
repeat 10 times:  x = (2x + n / x2) / 3
return x

然后编写一个主函数,它打印n,cbrt(n)和my_cbrt_1(n)的n = 3.14159乘以10的k的k次幂,k = -100,-10,-1,0,1,10,和100.使用此C ++ 11代码(仅适用于linux2):

and then write a main which prints n, cbrt(n), and my_cbrt_1(n) for n = 3.14159 times 10 to the kth power for k = -100, -10, -1, 0, 1, 10, and 100. Use this C++11 code (which only works on linux2):

for(auto k : {-100, -10, -1, 0, 1, 10, 100}){
    n = 3.14159 * pow(10.0, k);
    //cout goes here
}

我在入门上遇到了麻烦.如果有人可以帮助我,那就太好了!

I'm having trouble getting started on this. If anyone could help me that would be great!

推荐答案

要开始使用,您可能应该一次计算单个数字的立方根,然后可以将相同的函数用于新数字

To get started, you should probably calculate the cubed root of a single number once, and then you can use that same function for new numbers

简单地实现您所获得的功能

To simply implement the function that you were given

double my_cbrt_1(double n)
{
    double x = 1.0;
    for(int i=0; i<10; i++)
    {
        x = (2.0*x + n / (x*x)) / 3.0;
    }
    return x;
}

这篇关于编写多维数据集根函数有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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