问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

c语言中cos函数的用法21

发布网友 发布时间:2023-10-17 20:58

我来回答

3个回答

热心网友 时间:2023-11-09 05:09

cos函数的输入值为弧度,也就是将cos函数后加上弧度,然后就可以得到想要的结果。我们需要把度化为弧度:
假设度数为d,则对应的弧度为:d * pi / 180

热心网友 时间:2023-11-09 05:09

同学,以下请仔细参详, 不是有意做长, 实在是都有用.

Trigonometry Functions (Sample)
The sample code below illustrates how to use trigonometry (sin, cos, tan, and so on) functions in Visual C++.

Required Header:
<valarray>

Prototype:

// acos
template<class T>
inline valarray<T> acos(const valarray<T>& x);

// asin
template<class T>
inline valarray<T> asin(const valarray<T>& x);

// atan
template<class T>
inline valarray<T> atan(const valarray<T>& x);

// atan2
template<class T>
inline valarray<T> atan2(const valarray<T>& x, const valarray<T>& y);

template<class T>
inline valarray<T> atan2(const valarray<T> x, const T& y);

template<class T>
inline valarray<T> atan2(const T& x, const valarray<T>& y);

// cos
template<class T>
inline valarray<T> cos(const valarray<T>& x);

// cosh
template<class T>
inline valarray<T> cosh(const valarray<T>& x);

// sin
template<class T>
inline valarray<T> sin(const valarray<T>& x);

// sinh
template<class T>
inline valarray<T> sinh(const valarray<T>& x);

// tan
template<class T>
inline valarray<T> tan(const valarray<T>& x);

// tanh
template<class T>
inline valarray<T> tanh(const valarray<T>& x);

Note: The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

Description:
This article illustrates the use of STL trigonometry functions through sample code.

Sample Code:

//////////////////////////////////////////////////////////////////////
//
// Compile options needed: /GX
//
// main.cpp : Illustrates the use of STL trigonometry functions.
//
// Functions:
//
// acos, asin, atan, atan2, cos, cosh, sin, sinh, tan, tanh
//////////////////////////////////////////////////////////////////////

#include <iostream> // for i/o functions
#include <valarray> // for valarray
#include <cmath> // for trigonometry functions

using namespace std ;

#define ARRAY_SIZE 3 // array size

void main()
{
// Initialize val_array to values -1, 0 and 1.
valarray<double> val_array(ARRAY_SIZE);
for (int i = 0; i < ARRAY_SIZE; i++)
val_array[i] = i - 1;

// Display the size of val_array.
cout << "Size of val_array = " << val_array.size() << endl;

// Display the values of val_array before calling any trigonometry
// functions.
cout << "The values in val_array:" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << val_array[i] << " ";
cout << endl << endl;

// Initialize rev_valarray that is the reverse of val_array.
valarray<double> rev_valarray(ARRAY_SIZE);
for (i = 0; i < ARRAY_SIZE; i++)
rev_valarray[i] = val_array[ARRAY_SIZE - i - 1];

// Display the size of rev_valarray.
cout << "Size of rev_valarray = " << rev_valarray.size() << endl;

// Display the values of rev_valarray.
cout << "The values in rev_valarray:" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rev_valarray[i] << " ";
cout << endl << endl;

// rvalue_array to hold the return value from calling the trigonometry
// functions.
valarray<double> rvalue_array;

// ----------------------------------------------------------------
// acos() - display the result of rvalue_array
// ----------------------------------------------------------------
rvalue_array = acos(val_array);
cout << "The result after calling acos():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// ----------------------------------------------------------------
// asin() - display the result of rvalue_array
// ----------------------------------------------------------------
rvalue_array = asin(val_array);
cout << "The result after calling asin():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// ----------------------------------------------------------------
// atan() - display the result of rvalue_array
// ----------------------------------------------------------------
rvalue_array = atan(val_array);
cout << "The result after calling atan():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// ----------------------------------------------------------------
// atan2() - display the result of rvalue_array
// ----------------------------------------------------------------

// This template function returns an object of class valarray<T>,
// each of whose elements at I is the arctangent of x[I] / y[I].
rvalue_array = atan2(val_array, rev_valarray);
cout << "The result after calling atan2(val_array, rev_valarray):"
<< endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// This template function stores in element I the arctangent of
// x[I] / y.
rvalue_array = atan2(val_array, 3.1416);
cout << "The result after calling atan2(val_array, 3.1416):" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// This template function stores in element I the arctangent of
// x / y[I].
rvalue_array = atan2(3.1416, val_array);
cout << "The result after calling atan2(3.1416, val_array):" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// ----------------------------------------------------------------
// cos() - display the result of rvalue_array
// ----------------------------------------------------------------
rvalue_array = cos(val_array);
cout << "The result after calling cos():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// ----------------------------------------------------------------
// cosh() - display the result of rvalue_array
// ----------------------------------------------------------------
rvalue_array = cosh(val_array);
cout << "The result after calling cosh():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// ----------------------------------------------------------------
// sin() - display the result of val_array
// ----------------------------------------------------------------
rvalue_array = sin(val_array);
cout << "The result after calling sin():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// ----------------------------------------------------------------
// sinh() - display the result of val_array
// ----------------------------------------------------------------
rvalue_array = sinh(val_array);
cout << "The result after calling sinh():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// ----------------------------------------------------------------
// tan() - display the result of val_array
// ----------------------------------------------------------------
rvalue_array = tan(val_array);
cout << "The result after calling tan():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// ----------------------------------------------------------------
// tanh() - display the result of val_array
// ----------------------------------------------------------------
rvalue_array = tanh(val_array);
cout << "The result after calling tanh():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
}

Program Output is:

Size of val_array = 3
The values in val_array:
-1 0 1

Size of rev_valarray = 3
The values in rev_valarray:
1 0 -1

The result after calling acos():
3.14159 1.5708 0

The result after calling asin():
-1.5708 0 1.5708

The result after calling atan():
-0.785398 0 0.785398

The result after calling atan2(val_array, rev_valarray):
-0.785398 0 2.35619

The result after calling atan2(val_array, 3.1416):
-0.308168 0 0.308168

The result after calling atan2(3.1416, val_array):
1.87896 1.5708 1.26263

The result after calling cos():
0.540302 1 0.540302

The result after calling cosh():
1.54308 1 1.54308

The result after calling sin():
-0.841471 0 0.841471

The result after calling sinh():
-1.1752 0 1.1752

The result after calling tan():
-1.55741 0 1.55741

The result after calling tanh():
-0.761594 0 0.761594

热心网友 时间:2023-11-09 05:10

是用弧度做单位的
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
苹果电脑电池充不进电苹果电脑充不进去电是怎么回事 苹果电脑不充电没反应苹果电脑充电指示灯不亮充不了电怎么办 狗狗更加忠诚护家、善解人意,养一只宠物陪伴自己,泰迪能长多大... 描写泰迪狗的外形和特点的句子 国外留学有用吗 花钱出国留学有用吗 !这叫什么号 百万医疗赔付后是否可以续保 前一年理赔过医疗险还能续保吗? 医疗住院险理赔后还能购买吗? 抖音官方收取多少钱1 天津定额异形混凝土连锁砌块套什么子目? 昨晚我梦见爸妈挖了好多莲藕1 有谁是做绝味鸭脖的,开店的,一个月可以赚多少钱呢?54 有没有一款菜市场用的 既能称重又能扫二维码支付的设备呢?1 幼儿园惩罚与变相惩罚的区别 菜市场二维码为什么会被调包?5 删了的好友忘记怎么找回来 不喜欢现在的女朋友怎么办?12 变向体罚与体罚的区别 体罚与变相体罚对学生造成怎样的影响? 绝味鸭脖怎么样,好不好的默认点评 绝味鸭脖骗子?5 感觉每天生活很没意思很空洞怎么办,35男,没工作,离异, 为什么大学后会忘掉高中知识?如果说是因为太久没用的话,那为什么出门在... 历史系本科有些什么课程?5 历史系科目,从本科至博士,能列一份清单吗? 《梦开始的地方》 电影 观后感400字120 梦开始的地方观后感809 苹果树需要几年结果 小天才手表屏幕碎了要多少钱22 如何在微信上晒收入证明 合同能源管理有哪几种模式?那种模式较好 怎么把iphone里的视频放到电脑里?382 突然发现自己不爱女朋友了 怎么办35 电脑更新了一下驱动和显卡突然一下关机然后就不能启动了 银川到徐州9月28号的火车票 请问 “上当” 与“受骗” 有什么区别?5 罗马数字从1到100怎么写2 银川至徐州需要多久 罗马数字从1到100怎么写2 旱金莲洗发皂干枯的头发可以用吗女,43岁头发干枯毛糙,脱发断...21 什么是养老金的指数呢?355 东北师大历史系研究生毕业到高中当老师没问题吧? 东师的水深不...12 怎样解决制作小发明时的材料问题????????????17 制作一件最简单的科技小制作或科学小发明,不要用纸作为制作,材...433 为什么运动后可以深度睡眠?3 被骗和被耍,区别是啥?求解求解! 现在还有人民公社吗?78 能修改吗?