gpt4 book ai didi

c - 函数返回-1.#IND00 + 合并函数

转载 作者:行者123 更新时间:2023-11-30 16:25:19 25 4
gpt4 key购买 nike

所以我基本上试图将以下 3 个函数合并为 1 个(这就是我的老师想要的,但我无法让它工作,所以我做了 3 个)

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define TOTAL_DEG (180.0)


double berechnung_length(double a, double b, double c, double gamma)
{
c = sqrt(a * a + b * b - (2 * a * b * cos(gamma)));
return c;
}

double berechnung_alpha(double a, double b, double c, double gamma, double alpha)
{
alpha = gamma * acos((double )(b * b + c * c - a * a) / (2.0 * b * c));
return alpha;
}

double berechnung_beta(double gamma, double alpha, double beta)
{
beta = - gamma - alpha + TOTAL_DEG;
return beta;
}

soo,如果它只有 1 个函数,我怎样才能让函数返回值(指针是一个选项,但我对它们还不太满意)

感谢您的帮助!

最佳答案

to merge following 3 functions in 1

使用OP现有的函数调用:

void berechnung_all(double a, double b, double c, double gamma,
double *length, double *alpha, double *beta) {
*length = berechnung_length(a,b,c,gamma);
*alpha = berechnung_alpha(a,b,c,gamma, 0);
*beta = berechnung_beta(a,b,c,gamma, *alpha, 0);
}
<小时/>

更紧密的集成

#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif

#define R2D(r) ((r)*180.0/M_PI)
#define D2R(r) ((r)/180.0*M_PI)

void berechnung_all(double a, double b, double c, double gamma,
double *length, double *alpha, double *beta) {

*length = sqrt(a * a + b * b - (2 * a * b * cos(D2R(gamma))));

// Avoid direct call to `acos()`: prevent `double` math oddities just outside [-1.0...1.0]
double x = (b * b + c * c - a * a) / (2.0 * b * c);
if (x > 1.0) x = 1.0;
if (x < -1.0) x = -1.0;
*alpha = R2D(gamma * acos(x));

#define TOTAL_DEG (180.0)
*beta = - gamma - alpha + TOTAL_DEG;
}

关于c - 函数返回-1.#IND00 + 合并函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53432349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com