C++C++11中头文件cmath的使用

C++C++11中头⽂件cmath的使⽤
<math.h>是C标准函数库中的头⽂件。在C++中⼀般⽤<cmath>。此头⽂件中声明了⼀系列函数来计算常见的数学运算和变换:std::abs: 计算绝对值,包括整数类型;
std::fabs: 计算绝对值,不包括整数类型;
std::fma(x,y,z):x*y+z;
std::sin: 正弦;
std::asin: 反正弦;
std::sinh: 双曲正弦;
std::asinh: 双曲反正弦;
std::cos: 余弦;
std::acos: 反正弦;
std::cosh: 双曲余弦;
std::acosh: 双曲反余弦;
std::tan:正切;
std::atan:反正切;
std::atan2: 反正切;
std::tanh: 双曲正切;
std::atanh: 双曲反正切;
std::sqrt: 计算平⽅根;
std::cbrt: 计算⽴⽅根;
std::hypot: 计算两个数平⽅的和的平⽅根;
std::pow:幂运算;
std::ceil: 不⼩于给定值的最近整数;
std::floor: 不⼤于给定值的最近整数;
住宅室内装饰装修管理办法
std::fmod: 两数除法操作的余数(rounded towards zero);
std::trunc: 不⼤于给定值的最近整数;
std::round: 舍⼊取整;
std::lround: 舍⼊取整, 返回long int;
std::llround: 舍⼊取整, 返回long long int;
std::nearbyint: 使⽤当前的舍⼊模式取整(fegetround());
std::remainder: 两数除法操作的余数(rounded to nearest);
std::remquo: 两数除法操作的余数;
std::rint: 使⽤当前的舍⼊模式取整(fegetround());
std::lrint: 使⽤当前的舍⼊模式取整(fegetround()),返回long int;
std::llrint: 使⽤当前的舍⼊模式取整(fegetround()),返回long longint;
std::exp: ex;
std::frexp: 将⼀个浮点数分解为有效数(significand)及以2为底的幂(x = significand* 2exp); std::ldexp: x *2exp;
std::exp2: 2x;
std::expm1: ex-1;
std::scalbn: x *FLT_RADIXn;
std::scalbln: x* FLT_RADIXn;
std::log: ln(x);
std::log10: log10(x);
std::modf: 将⼀个浮点数分解为整数及⼩数部分;
std::ilogb: 返回以FLT_RADIX为底,|x|的对数值,返回值为整数;
std::log1p: ln(1+x);
std::log2: log2(x);
std::logb: 返回以FLT_RADIX为底,|x|的对数值,返回值为浮点数;
std::erf: 误差函数;
std::erfc: 互补(complementary)误差函数;
std::tgamma: 伽玛函数;
std::lgamma: log-伽玛函数;
std::copysign(x,y):返回x的值及y的正负符号组成的浮点数;
std::nan: Generatequiet NaN;
std::nextafter(x,y): 返回x之后y⽅向上的下⼀个可表⽰值;
std::nexttoward(x,y): 返回x之后y⽅向上的下⼀个可表⽰值;
std::fdim(x,y): Thefunction returns x-y if x>y, and zero otherwise;
std::fmax: 返回较⼤的值;
std::fmin: 返回较⼩的值;
std::fpclassify:为浮点值归类,返回⼀个类型为int的值;
std::isfinite: 检测是否是有限值;
std::isinf: 检测是否是⽆穷⼤值;
std::isnan: 检测是否是⾮数型;
std::isnormal: 检测是否是normal值,neitherinfinity, NaN, zero or subnormal;
std::signbit: 检测是否是负数;
std::isgreater: 检测第⼀个数是否⼤于第⼆个数;
std::isgreaterequal:检测第⼀个数是否⼤于或等于第⼆个数;
std::isless: 检测第⼀个数是否⼩于第⼆个数;
std::islessequal:检测第⼀个数是否⼩于或等于第⼆个数;
std::islessgreater:检测第⼀个数是否不等于第⼆个数;
std::isunordered:检测两个浮点数是否是⽆序的.
下⾯是从其它⽂章中copy的<cmath>测试代码,详细内容介绍可以参考对应的reference:
#include "cmath.hpp"
#include <cmath>
#include <iostream>
#include <fenv.h> // fegetround, FE_*
#include <float.h> // FLT_RADIX
// reference: www.cplusplus/reference/cmath/
#define PI 3.14159265
namespace cmath_ {
int test_cmath_abs()
{
{ // std::abs: double/float/long double/T
std::cout << "abs (3.141611111) = " << std::abs(3.141611111) << '\n'; // 3.14161
std::cout << "abs (-10.6)  = " << std::abs(-10.6f) << '\n'; // 10.6
std::cout << "abs ((int)-10)  = " << std::abs((int)-10) << '\n'; // 10
}
{ // std::fabs: double/float/long double/T
std::cout << "fabs (3.141611111) = " << std::fabs(3.141611111) << '\n'; // 3.14161
std::cout << "fabs (-10.6)  = " << std::fabs(-10.6f) << '\n'; // 10.6
}
{ // std::fma: Returns x*y+z
double x, y, z, result;
x = 10.0, y = 20.0, z = 30.0;
result = std::fma(x, y, z);
printf("10.0 * 20.0 + 30.0 = %f\n", result); // 230.0
}
return 0;
}
int test_cmath_triangle()
{
{ // std::sin: double/float/long double/T
double param, result;
param = 30.0;
result = std::sin(param*PI / 180);
fprintf(stdout, "The sine of %f degrees is %f.\n", param, result); // 0.5
}
虚拟电话{ // std::asin: double/float/long double/T
double param, result;
param = 0.5;
result = std::asin(param) * 180.0 / PI;
fprintf(stdout, "The arc sine of %f is %f degrees\n", param, result); // 30.0
}
{ // std::sinh: double/float/long double/T
double param, result;
param = log(2.0);
result = std::sinh(param);
printf("The hyperbolic sine of %f is %f.\n", param, result); // 0.75
}
{ // std::asinh double/float/long double/T
double param, result;
param = std::exp(2) - std::cosh(2);
result = std::asinh(param);
fprintf(stdout, "The area hyperbolic sine of %f is %f.\n", param, result); // 2.0
}
{ // std::cos double/float/long double/T
double param, result;
param = 60.0;
result = std::cos(param * PI / 180.0);
fprintf(stdout, "The cosine of %f degrees is %f.\n", param, result); // 0.5
}
{// std::acos: double/float/long double/T
double param, result;
param = 0.5;
result = std::acos(param) * 180.0 / PI;
fprintf(stdout, "The arc cosine of %f is %f degrees.\n", param, result); // 60.0
}
{ // std::cosh double/float/long double/T
double param, result;
param = std::log(2.0);
result = std::cosh(param);
fprintf(stdout, "The hyperbolic cosine of %f is %f.\n", param, result); // 1.25
}
青岛老人被特勤打{ // std::acosh: double/float/long double/T
double param, result;
param = std::exp(2) - std::sinh(2);
result = std::acosh(param);
fprintf(stdout, "The area hyperbolic cosine of %f is %f radians.\n", param, result); // 2.0 }
{ // std::tan: double/float/long double/T
double param, result;
param = 45.0;
result = std::tan(param * PI / 180.0);
fprintf(stdout, "The tangent of %f degrees is %f.\n", param, result); // 1.0
}
{ // std::atan: double/float/long double/T
double param, result;
param = 1.0;
福建江夏学院论坛result = std::atan(param) * 180 / PI;
fprintf(stdout, "The arc tangent of %f is %f degrees\n", param, result); // 45.0
}
{ // std::atan2: double/float/long double/T
double x, y, result;
x = -10.0;
r2vy = 10.0;
result = std::atan2(y, x) * 180 / PI;
fprintf(stdout, "The arc tangent for (x=%f, y=%f) is %f degrees\n", x, y, result); // 135.0 }
{ // std::tanh: double/float/long double/T
double param, result;
param = std::log(2.0);
result = std::tanh(param);
fprintf(stdout, "The hyperbolic tangent of %f is %f.\n", param, result); // 0.6
}
{ // std::atanh: double/float/long double/T
double param, result;
param = std::tanh(1);
result = std::atanh(param);
fprintf(stdout, "The area hyperbolic tangent of %f is %f.\n", param, result); // 1
}
return 0;
}
int test_cmath_pow()
{
{ // std::sqrt(x): Returns the square root of x
double param, result;
param = 1024.0;
result = std::sqrt(param);
printf("sqrt(%f) = %f\n", param, result); // 32.0
}
{ // std::cbrt: Compute cubic root
double param, result;
木酢液
param = 27.0;
result = std::cbrt(param);
fprintf(stdout, "cbrt (%f) = %f\n", param, result); // 3.0
}
{ // std::hypot(x, y): sqrt(x^2+y^2)
double leg_x, leg_y, result;
leg_x = 3;
leg_y = 4;
result = std::hypot(leg_x, leg_y);
fprintf(stdout, "%f, %f and %f form a right-angled triangle.\n", leg_x, leg_y, result); // 5.0 }
{ // std::pow(x, y): x^y
fprintf(stdout, "7 ^ 3 = %f\n", std::pow(7.0, 3.0)); // 343.0
fprintf(stdout, "4.73 ^ 12 = %f\n", std::pow(4.73, 12.0)); // 125410439.217423
fprintf(stdout, "32.01 ^ 1.54 = %f\n", std::pow(32.01, 1.54)); // 208.036691
fprintf(stdout, "4 ^ 3 = %f\n", std::pow((int)4, (int)3)); // 64.0
}
return 0;
}
int test_cmath_integer()
{
{ // std::ceil(x): returning the smallest integral value that is not less than x
fprintf(stdout, "ceil of 2.3 is %.1f\n", std::ceil(2.3)); // 3.0
fprintf(stdout, "ceil of 3.8 is %.1f\n", std::ceil(3.8)); // 4.0
fprintf(stdout, "ceil of -2.3 is %.1f\n", std::ceil(-2.3)); // -2.0
fprintf(stdout, "ceil of -3.8 is %.1f\n", std::ceil(-3.8)); // -3.0
}
{ // std::floor returning the largest integral value that is not greater than x
fprintf(stdout, "floor of 2.3 is %.1lf\n", std::floor(2.3)); // 2.0
fprintf(stdout, "floor of 3.8 is %.1lf\n", std::floor(3.8)); // 3.0
fprintf(stdout, "floor of -2.3 is %.1lf\n", std::floor(-2.3)); // -2.0
fprintf(stdout, "floor of -3.8 is %.1lf\n", std::floor(-3.8)); // -3.0
}

本文发布于:2024-09-22 06:39:03,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/23895.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:整数   计算   浮点数   检测   个数   是否   函数
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议