在C中计算时使用余弦时出错 [英] Error using cosine when calculating in C

查看:76
本文介绍了在C中计算时使用余弦时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须计算角度测量和n-gon的周长。我必须使用余弦来计算剩余的一面,我不断收到一个错误,说这是对cosf的一个未定义的引用,我已经做了一些编辑,我老实说无法解决这个问题





I'm having to calculate the angle measure and perimeter of a n-gon. I have to use cosine to calculate the remaining side and I keep getting an error saying that it's an undefined reference to cosf, and i've done some editting around, I honestly can't figure this out


#include <stdio.h>
#include <math.h>

int main(void)
{
	float sides, radius, side, perimeter, perimeterFeet, rads;
	float sideLength = 0;
	double x, angle;
	
	printf("Enter the number of sides on the polygon: ");
	scanf("%f", &sides);
	printf("Enter the radius of the polygon: ");
	scanf("%f", &radius);
	
	angle = (sides-2)/sides * 180;
	rads = angle *  0.01745329252;
	
	x = double cos(angle);
	sideLength = radius*radius + radius*radius - 2 * radius * radius * x;
	sideLength = sqrtf(sideLength);
	
	perimeter - sideLength* sides;
	perimeterFeet = perimeter/12;
	
	printf("Angle is %f radians (%f degrees) \n", rads, angle);
	printf("The side is %f inches \n", sideLength);
	printf("The perimeter is %f inches and %f feet", perimeter, perimeterFeet);
	
}





我的尝试:



我试过改变cos到cosf,双cos(双角),cos(双角),cos(角)我在这里难倒



What I have tried:

I've tried changing around cos to cosf, double cos(double angle), cos(double angle), cos(angle) I'm stumped here

推荐答案

缺少库:你如何解决这个问题取决于你使用的编译器,但对于 gcc 添加 -lm 到编译选项的末尾:

Missing library: how you fix this depends on what compiler you are using, but for gcc add "-lm" to the end of the compilation options:
gcc -o main *.c -lm



并在此行中将 double 转换为演员(double)


And either convert double to a cast (double) in this line:

x = double cos(angle);

或者删除它。

你可能想要从 sqrtf 中删除​​'f':

Or remove it.
You probably want to remove the 'f' from sqrtf as well:

sideLength = sqrt(sideLength);


这篇关于在C中计算时使用余弦时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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