小编Nob*_*non的帖子

Cube的一个数字的根,C++

我想问一个非常简短的问题,它如下:在C++中找到一个数字的立方根(包括neg.和pos.),如何仅将输出限制为实数解?我目前正在编写一个用Cardano公式解决立方体的程序,我正在使用的一个中间变量随机输出复杂和真实的立方根 - 我只需要真正的根.

(例如,在评估-0.0127378的立方根时,三个根将是0.11677095 + 0.202253218i,-0.2335419,0.11677095-0.202253218i - 我希望忽略复杂的替换为后面的公式)

谢谢!

编辑:解决了!:)我创建了一个signum函数,并在获取SPrime和TPrime的绝对值的力量后调整了符号,所以现在它只转发真正的立方根.

/* ... */

#include <iostream>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cassert>

using namespace std;

int signum(std::complex<double> z)
{
    if (z.real() < 0 || z.imag() < 0) return -1;
    else if (z.real() >= 0 || z.imag() >= 0) return 1;
}

// POST: The function is intended to solve a cubic equation with       coefficients a, b, c and d., such that
//      ax^3 + bx^2 + cx + d …
Run Code Online (Sandbox Code Playgroud)

c++ math

5
推荐指数
1
解决办法
1585
查看次数

标签 统计

c++ ×1

math ×1