首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::cos

Defined in header <cmath>

?

?

float cos( float arg );

(1)

?

double cos( double arg );

(2)

?

long double cos( long double arg );

(3)

?

double cos( Integral arg );

(4)

(since C++11)

计算余弦arg以弧度计算的百分比为28%,为29%。

4%29一组过载或接受任意参数的函数模板积分型等于2%29%28double29%。

参数

arg

-

value representing angle in radians, of a floating-point or Integral type

返回值

如果没有错误发生,则余弦arg范围内28 cos%28 arg%29%29-1;+1,被归还。

The result may have little or no significance if the magnitude of arg is large.

(until C++11)

如果发生域错误,则返回支持%29的实现定义值%28 NaN。

如果由于下流而发生范围错误,则返回舍入%29后的正确结果%28。

错误处理

如果实现支持ieee浮点算法%28IEC 60559%29,

  • 如果参数为±0,则结果为1.0。
  • 如果参数为±∞,则返回NaNFE_INVALID提出来
  • 如果参数为nan,则返回nan。

注记

在C中,参数为无穷大的情况不是指定为域错误,而是将其定义为POSIX中的域错误...

二次

代码语言:javascript
复制
#include <iostream>
#include <cmath>
#include <cerrno>
#include <cfenv>
 
#pragma STDC FENV_ACCESS ON
const double pi = std::acos(-1);
int main()
{
    // typical usage
    std::cout << "cos(pi/3) = " << std::cos(pi/3) << '\n'
              << "cos(pi/2) = " << std::cos(pi/2) << '\n'
              << "cos(-3*pi/4) = " << std::cos(-3*pi/4) << '\n';
    // special values
    std::cout << "cos(+0) = " << std::cos(0.0) << '\n'
              << "cos(-0) = " << std::cos(-0.0) << '\n';
    // error handling 
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "cos(INFINITY) = " << std::cos(INFINITY) << '\n';
    if(std::fetestexcept(FE_INVALID)) std::cout << "    FE_INVALID raised\n";
}

二次

可能的产出:

二次

代码语言:javascript
复制
cos(pi/3) = 0.5
cos(pi/2) = 6.12323e-17
cos(-3*pi/4) = -0.707107
cos(+0) = 1
cos(-0) = 1
cos(INFINITY) = -nan
    FE_INVALID raised

二次

另见

sin

computes sine (sin(x)) (function)

tan

computes tangent (tan(x)) (function)

acos

computes arc cosine (arccos(x)) (function)

cos(std::complex)

computes cosine of a complex number (cos(z)) (function template)

cos(std::valarray)

applies the function std::cos to each element of valarray (function template)

c关于COS的文件

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com