精华内容
下载资源
问答
  • cmath头文件包含什么
    2020-12-05 15:14:11

    cmath头文件

    指的是数学函数库
    sin cos exp log pow sqrt fabs ceil floor 的形参为double类型返回值也为double类型

    更多相关内容
  • cmath头文件常用函数

    2022-04-27 23:10:13
    c++ cmath头文件常用数学函数
    //常用数学函数 
    #include <cmath>
    using namespace std;
    
    int main(){
    	/*求绝对值*/
    	//浮点数  double fabs(double),float fabs(float)
    	//整数 int abs(int),long labs(long)
    	
    	/*求幂*/ 
    	//double pow(double ,double)
    	
    	/*开根号*/ 
    	//double sqrt(double)
    	
    	/*小数舍入处理*/ 
    	//double floor(double x) 将一个小数向下取整
    	//double ceil(double x) 将一个小数向上取整
    	//double round(double x) 将一个小数四舍五入
    	
    	/*求对数值 log函数*/ 
    	//printf("%f\n",log(10)); //以e为底的对数函数 
    	//printf("%f\n",log10(100)); //以10为底的对数函数 
    	//printf("%f\n",log(8)/log(2)); //计算log2 8,运用换底公式 
    	
    	/*自然常数相关*/ 
    	//exp()函数返回e的n次方
    	//printf("%f\n",exp(1)); //计算自然常数e
    	
    	/*正弦余弦正切 均采用弧度制*/
    	//	double sin(double);
    	//  double cos(double);
    	//  double tan(double);
    	return 0; 
    } 

     

    展开全文
  • cmath头文件

    千次阅读 多人点赞 2019-06-23 18:45:38
      cmath头文件中常用函数有: 取绝对值,x需为double型,若为int型可乘一个1.0 1.double fabs(double x) 向上取整,返回小于或等于x的最大整数,如floor(10.5) == 10.0 floor(-10.5) == -11.0 2.double floor...

      cmath头文件中常用函数有:

    取绝对值,x需为double型,若为int型可乘一个1.0
    1.double fabs(double x)		
    
    向上取整,返回小于或等于x的最大整数,如floor(10.5) == 10.0  floor(-10.5) == -11.0
    2.double floor(double x)	
    
    向下取整,返回大于x的最小整数,如ceil(10.5) == 11.0  ceil(-10.5) == -10.0
    3.double ceil(double x)
    
    计算幂值,返回r^p
    4.double pow(double r,double p)
    
    返回e^x次方
    5.double exp(double x)
    
    计算以e为底的对数,若要计算以10为底的对数:log10(x)
    若要计算以其他数为底的对数:log a(b) = log(b)/log(a)
    6.double log(double x)
    
    三角函数计算
    7.double sin(double x)	cos(x) tan(x)
    
    反三角函数计算
    8.double asin(double x) acos(x) atan(x)
    
    四舍五入
    9.double round(double x)
    
    展开全文
  • C++11中头文件cmath

    千次阅读 2020-09-24 23:26:39
    cmath>。此头文件中声明了一系列函数来计算常见的数学运算和变换: std::abs: 计算绝对值,包括整数类型; std::fabs: 计算绝对值,不包括整数类型; std::fma(x,y,z):x*y+z; std::sin: 正弦; std::asin: ...

    <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:检测两个浮点数是否是无序的.

    #include "cmath.hpp"
    #include <cmath>
    #include <iostream>
    #include <fenv.h> // fegetround, FE_*
    #include <float.h> // FLT_RADIX
     
    // reference: http://www.cplusplus.com/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;
    	y = 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
    }
     
    { // std::fmod: Returns the floating-point remainder of numer/denom(rounded towards zero)
    	printf("fmod of 5.3 / 2 is %f\n", std::fmod(5.3, 2)); // fmod of 5.3 / 2 is 1.3
    	printf("fmod of 18.5 / 4.2 is %f\n", std::fmod(18.5, 4.2)); // fmod of 18.5 / 4.2 is 1.7
    }
     
    { // std::trunc(x): Rounds x toward zero, returning the nearest integral value that is not larger in magnitude than x.
      // std::round(x): Returns the integral value that is nearest to x
    	const char * format = "%.1f \t%.1f \t%.1f \t%.1f \t%.1f\n";
    	printf("value\tround\tfloor\tceil\ttrunc\n");
    	printf("-----\t-----\t-----\t----\t-----\n");				 // round  floor  ceil  trunc
    	printf(format, 2.3, std::round(2.3), std::floor(2.3), std::ceil(2.3), std::trunc(2.3));	 // 2.0     2.0    3.0   2.0
    	printf(format, 3.8, std::round(3.8), std::floor(3.8), std::ceil(3.8), std::trunc(3.8));	 // 4.0     3.0    4.0   3.0
    	printf(format, 5.5, std::round(5.5), std::floor(5.5), std::ceil(5.5), std::trunc(5.5));	 // 6.0     5.0    6.0   5.0
    	printf(format, -2.3, std::round(-2.3), std::floor(-2.3), std::ceil(-2.3), std::trunc(-2.3)); // -2.0    -3.0   -2.0  -2.0
    	printf(format, -3.8, std::round(-3.8), std::floor(-3.8), std::ceil(-3.8), std::trunc(-3.8)); // -4.0    -4.0   -3.0  -3.0
    	printf(format, -5.5, std::round(-5.5), std::floor(-5.5), std::ceil(-5.5), std::trunc(-5.5)); // -6.0    -6.0   -5.0  -5.0
    }
     
    { // std::lround: Returns the integer value that is nearest in value to x
    	printf("lround (2.3) = %ld\n", std::lround(2.3)); // 2
    	printf("lround (3.8) = %ld\n", std::lround(3.8)); // 4
    	printf("lround (-2.3) = %ld\n", std::lround(-2.3)); // -2
    	printf("lround (-3.8) = %ld\n", std::lround(-3.8)); // -4
    }
     
    { // std::llround(x): Returns the integer value that is nearest in value to x
    	printf("llround (2.3) = %lld\n", std::llround(2.3)); // 2
    	printf("llround (3.8) = %lld\n", std::llround(3.8)); // 4
    	printf("llround (-2.3) = %lld\n", std::llround(-2.3)); // -2
    	printf("llround (-3.8) = %lld\n", std::llround(-3.8)); // -4
    }
     
    { // std::nearbyint: Round to nearby integral value
    	printf("rounding using ");
    	switch (fegetround()) {
    	case FE_DOWNWARD: printf("downward"); break;
    	case FE_TONEAREST: printf("to-nearest"); break; // to-nearest
    	case FE_TOWARDZERO: printf("toward-zero"); break;
    	case FE_UPWARD: printf("upward"); break;
    	default: printf("unknown");
    	}
    	printf(" rounding:\n");
     
    	printf("nearbyint (2.3) = %.1f\n", std::nearbyint(2.3)); // 2.0
    	printf("nearbyint (3.8) = %.1f\n", std::nearbyint(3.8)); // 4.0
    	printf("nearbyint (-2.3) = %.1f\n", std::nearbyint(-2.3)); // -2.0
    	printf("nearbyint (-3.8) = %.1f\n", std::nearbyint(-3.8)); // -4.0
    }
     
    { // std::remainder: Returns the floating-point remainder of numer/denom(rounded to nearest)
    	printf("remainder of 5.3 / 2 is %f\n", std::remainder(5.3, 2)); // remainder of 5.3 / 2 is -0.7
    	printf("remainder of 18.5 / 4.2 is %f\n", std::remainder(18.5, 4.2)); // remainder of 18.5 / 4.2 is 1.7
    }
     
    { // std::remquo: Returns the same as remainder, but it additionally stores the quotient
      // internally used to determine its result in the object pointed by quot
    	double numer = 10.3;
    	double denom = 4.5;
    	int quot;
    	double result = std::remquo(numer, denom, ");
    	printf("numerator: %f\n", numer); // 10.3
    	printf("denominator: %f\n", denom); // 4.5
    	printf("remainder: %f\n", result); // 1.3
    	printf("quotient: %d\n", quot); // 2
    }
    { // std::rint: Round to integral value
    	printf("rounding using ");
    	switch (fegetround()) {
    		case FE_DOWNWARD: printf("downward"); break;
    		case FE_TONEAREST: printf("to-nearest"); break; // to-nearest
    		case FE_TOWARDZERO: printf("toward-zero"); break;
    		case FE_UPWARD: printf("upward"); break;
    		default: printf("unknown");
    	}
    	printf(" rounding:\n");
    	printf("rint (2.3) = %.1f\n", std::rint(2.3)); // 2.0
    	printf("rint (3.8) = %.1f\n", std::rint(3.8)); // 4.0
    	printf("rint (-2.3) = %.1f\n", std::rint(-2.3)); // -2.0
    	printf("rint (-3.8) = %.1f\n", std::rint(-3.8)); // -4.0
    }
    { // std::lrint: Rounds x to an integral value, and returns it as a value of type long int.
    	printf("rounding using ");
    	switch (fegetround()) {
    		case FE_DOWNWARD: printf("downward"); break;
    		case FE_TONEAREST: printf("to-nearest"); break; // to-nearest
    		case FE_TOWARDZERO: printf("toward-zero"); break;
    		case FE_UPWARD: printf("upward"); break;
    		default: printf("unknown");
    	}
    	printf(" rounding:\n");
    	printf("lrint (2.3) = %ld\n", std::lrint(2.3)); // 2
    	printf("lrint (3.8) = %ld\n", std::lrint(3.8)); // 4
    	printf("lrint (-2.3) = %ld\n", std::lrint(-2.3)); // -2
    	printf("lrint (-3.8) = %ld\n", std::lrint(-3.8)); // -4
    }
    { // std::llrint: Rounds x to an integral value,returns it as a value of type long long int
    	printf("rounding using ");
    	switch (fegetround()) {
    		case FE_DOWNWARD: printf("downward"); break;
    		case FE_TONEAREST: printf("to-nearest"); break; // to-nearest
    		case FE_TOWARDZERO: printf("toward-zero"); break;
    		case FE_UPWARD: printf("upward"); break;
    		default: printf("unknown");
    	}
    	printf(" rounding:\n");
    	printf("llrint (2.3) = %lld\n", std::llrint(2.3)); // 2
    	printf("llrint (3.8) = %lld\n", std::llrint(3.8)); // 4
    	printf("llrint (-2.3) = %lld\n", std::llrint(-2.3)); // -2
    	printf("llrint (-3.8) = %lld\n", std::llrint(-3.8)); // -4
    }
    	return 0;
    }
    int test_cmath_exp()
    {
    { // std::exp: Returns the base-e exponential function of x, e^x
    	double param, result;
    	param = 1.0;
    	result = std::exp(param);
    	printf("The exponential value of %f is %f.\n", param, result); // 1.0 2.718282
    }
    { // std::frexp(x, int* exp):Breaks the floating point number x into its binary significand
      // (a floating point with an absolute value between 0.5(included) and 1.0(excluded)) and an integral exponent for 2
      // x = significand * (2 ^ exponent)
    	double param, result;
    	int n;
    	param = 8.0;
    	result = std::frexp(param, &n);
    	printf("%f = %f * 2^%d\n", param, result, n); // 8.0 = 0.5 * 2^4
    }
    { // std::ldexp: Returns the result of multiplying x (the significand) by 2 raised to the power of exp (the exponent)
    	double param, result;
    	int n;
    	param = 0.95;
    	n = 4;
    	result = std::ldexp(param, n);
    	printf("%f * 2^%d = %f\n", param, n, result); // 0.95 * 2^4 = 15.2
    }
    { // std::exp2: Returns the base-2 exponential function of x
    	double param, result;
    	param = 8.0;
    	result = std::exp2(param);
    	printf("2 ^ %f = %f.\n", param, result); // 2^8 = 256
    }
    { // std::expm1: Compute exponential minus one
    	double param, result;
    	param = 1.0;
    	result = std::expm1(param);
    	printf("expm1 (%f) = %f.\n", param, result); // expm1(1.0) = 1.718282
    }
    { // std::scalbn: Scales x by FLT_RADIX raised to the power of n
    	double param, result;
    	int n;
    	param = 1.50;
    	n = 4;
    	result = std::scalbn(param, n);
    	printf("%f * %d^%d = %f\n", param, FLT_RADIX, n, result); // 1.5 * 2^4 = 24.0
    }
    { // std::scalbln: Scales x by FLT_RADIX raised to the power of n
    	double param, result;
    	long n;
    	param = 1.50;
    	n = 4L;
    	result = std::scalbln(param, n);
    	printf("%f * %d^%d = %f\n", param, FLT_RADIX, n, result); // 1.5 * 2^4 = 24.0
    }
    	return 0;
    }
    int test_cmath_log()
    {
    { // std::log: Returns the natural logarithm of x
      // The natural logarithm is the base-e logarithm: the inverse of the natural exponential function (exp)
    	double param, result;
    	param = 5.5;
    	result = std::log(param);
    	printf("log(%f) = %f\n", param, result); // ln(5.5) = 1.704748
    }
    { // std::log10: Returns the common (base-10) logarithm of x
    	double param, result;
    	param = 1000.0;
    	result = std::log10(param);
    	printf("log10(%f) = %f\n", param, result); // log10(1000.0) = 3.0
    }
    { // std::modf: Breaks x into an integral and a fractional part
    	double param, fractpart, intpart;
    	param = 3.14159265;
    	fractpart = std::modf(param, &intpart);
    	printf("%f = %f + %f \n", param, intpart, fractpart); // 3.14159265 = 3.0 + 0.141593
    }
    { // std::ilogb: Returns the integral part of the logarithm of |x|, using FLT_RADIX as base for the logarithm.
    	double param;
    	int result;
    	param = 10.0;
    	result = std::ilogb(param);
    	printf("ilogb(%f) = %d\n", param, result); // ilogb(10.0) = 3
    }
    { // std::log1p: Returns the natural logarithm of one plus x
    	double param, result;
    	param = 1.0;
    	result = std::log1p(param);
    	printf("log1p (%f) = %f.\n", param, result); // log1p(1.0) = 0.693147
    }
    { // std::log2: Returns the binary (base-2) logarithm of x.
    	double param, result;
    	param = 1024.0;
    	result = std::log2(param);
    	printf("log2 (%f) = %f.\n", param, result); // log2(1024.0) = 10.0
    }
    { // std::logb: Returns the logarithm of |x|, using FLT_RADIX as base for the logarithm
    	double param, result;
    	param = 1024.0;
    	result = std::logb(param);
    	printf("logb (%f) = %f.\n", param, result); // logb(1024.0) = 10.0
    }
    	return 0;
    }
    int test_cmath_error()
    {
    { // std::erf: Returns the error function value for x.
    	double param, result;
    	param = 1.0;
    	result = std::erf(param);
    	printf("erf (%f) = %f\n", param, result); // erf(1.0) = 0.842701
    }
    { // std::erfc: Returns the complementary error function value for x
    	double param, result;
    	param = 1.0;
    	result = std::erfc(param);
    	printf("erfc(%f) = %f\n", param, result); // erfc(1.0) = 0.157299
    }
    { // std::tgamma: Compute gamma function
    	double param, result;
    	param = 0.5;
    	result = std::tgamma(param);
    	printf("tgamma(%f) = %f\n", param, result); // tgamma(0.5) = 1.772454
    }
    { // std::lgamma: Compute log-gamma function
    	double param, result;
    	param = 0.5;
    	result = std::lgamma(param);
    	printf("lgamma(%f) = %f\n", param, result); // lgamma(0.5) = 0.572365
    }
    	return 0;
    }
    int test_cmath_1()
    {
    { // std::copysign: Returns a value with the magnitude of x and the sign of y
    	printf("copysign ( 10.0,-1.0) = %f\n", std::copysign(10.0, -1.0)); // -10.0
    	printf("copysign (-10.0,-1.0) = %f\n", std::copysign(-10.0, -1.0)); // -10.0
    	printf("copysign (-10.0, 1.0) = %f\n", std::copysign(-10.0, 1.0)); // 10.0
    }
    { // std::nan: Returns a quiet NaN (Not-A-Number) value of type double.
    }
    { // std::nextafter: Returns the next representable value after x in the direction of y
    	printf("first representable value greater than zero: %e\n", std::nextafter(0.0, 1.0)); // 4.940656e-324
    	printf("first representable value less than zero: %e\n", std::nextafter(0.0, -1.0)); // -4.940656e-324
    }
    { // std::nexttoward: Returns the next representable value after x in the direction of y
    	printf("first representable value greater than zero: %e\n", std::nexttoward(0.0, 1.0L)); // 4.940656e-324
    	printf("first representable value less than zero: %e\n", std::nexttoward(0.0, -1.0L)); // -4.940656e-324
    }
    	return 0;
    }
    int test_cmath_2()
    {
    { // std::fdim: The function returns x-y if x>y, and zero otherwise.
    	printf("fdim (2.0, 1.0) = %f\n", std::fdim(2.0, 1.0)); // 1.0
    	printf("fdim (1.0, 2.0) = %f\n", std::fdim(1.0, 2.0)); // 0.0
    	printf("fdim (-2.0, -1.0) = %f\n", std::fdim(-2.0, -1.0)); // 0.0
    	printf("fdim (-1.0, -2.0) = %f\n", std::fdim(-1.0, -2.0)); // 1.0
    }
    { // std::fmax: Returns the larger of its arguments: either x or y
    	printf("fmax (100.0, 1.0) = %f\n", std::fmax(100.0, 1.0)); // 100.0
    	printf("fmax (-100.0, 1.0) = %f\n", std::fmax(-100.0, 1.0)); // 1.0
    	printf("fmax (-100.0, -1.0) = %f\n", std::fmax(-100.0, -1.0)); // -1.0
    }
    { // std::fmin: Returns the smaller of its arguments: either x or y
    	printf("fmin (100.0, 1.0) = %f\n", std::fmin(100.0, 1.0)); // 1.0
    	printf("fmin (-100.0, 1.0) = %f\n", std::fmin(-100.0, 1.0)); // -100.0
    	printf("fmin (-100.0, -1.0) = %f\n", std::fmin(-100.0, -1.0)); // -100.0
    }
    	return 0;
    }
    int test_cmath_classify()
    {
    { // std::fpclassify: Returns a value of type int that matches one of the classification
      // macro constants, depending on the value of x
    	double d = std::sqrt(-1.0); // 1.0 / 0.0;
    	switch (std::fpclassify(d)) {
    		case FP_INFINITE:  printf("infinite");  break;
    		case FP_NAN:       printf("NaN");       break; // NaN
    		case FP_ZERO:      printf("zero");      break;
    		case FP_SUBNORMAL: printf("subnormal"); break;
    		case FP_NORMAL:    printf("normal");    break;
    	}
    	if (std::signbit(d)) printf(" negative\n"); // negative
    	else printf(" positive or unsigned\n");
    }
    { // std::isfinite: Returns whether x is a finite value
    	printf("isfinite(0.0)       : %d\n", std::isfinite(0.0)); // 1
    	//printf("isfinite(1.0/0.0)   : %d\n", std::isfinite(1.0 / 0.0));
    	//printf("isfinite(-1.0/0.0)  : %d\n", std::isfinite(-1.0 / 0.0));
    	printf("isfinite(sqrt(-1.0)): %d\n", std::isfinite(std::sqrt(-1.0))); // 0
    }
    { // std::isinf: Returns whether x is an infinity value 
    	printf("isinf(0.0)       : %d\n", std::isinf(0.0)); // 0
    	//printf("isinf(1.0/0.0)   : %d\n", std::isinf(1.0 / 0.0));
    	//printf("isinf(-1.0/0.0)  : %d\n", std::isinf(-1.0 / 0.0));
    	printf("isinf(sqrt(-1.0)): %d\n", std::isinf(std::sqrt(-1.0))); // 0
    }
    { // std::isnan: Returns whether x is a NaN (Not-A-Number) value.
    	printf("isnan(0.0)       : %d\n", std::isnan(0.0)); // 0
    	//printf("isnan(1.0/0.0)   : %d\n", std::isnan(1.0 / 0.0));
    	//printf("isnan(-1.0/0.0)  : %d\n", std::isnan(-1.0 / 0.0));
    	printf("isnan(sqrt(-1.0)): %d\n", std::isnan(std::sqrt(-1.0))); // 1
    }
    { // std::isnormal: Returns whether x is a normal value
      // i.e., whether it is neither infinity, NaN, zero or subnormal
    	printf("isnormal(1.0)    : %d\n", std::isnormal(1.0)); // 1
    	printf("isnormal(0.0)    : %d\n", std::isnormal(0.0)); // 0
    	//printf("isnormal(1.0/0.0): %d\n", std::isnormal(1.0 / 0.0));
    }
    { // std::signbit: Returns whether the sign of x is negative
    	printf("signbit(0.0)       : %d\n", std::signbit(0.0)); // 0
    	//printf("signbit(1.0/0.0)   : %d\n", std::signbit(1.0 / 0.0));
    	//printf("signbit(-1.0/0.0)  : %d\n", std::signbit(-1.0 / 0.0));
    	printf("signbit(sqrt(-1.0)): %d\n", std::signbit(std::sqrt(-1.0))); // 1
    }
    	return 0;
    }
    int test_cmath_compare()
    {
    	double result;
    	result = std::log(10.0);
    { // std::isgreater: Returns whether x is greater than y
    	if (std::isgreater(result, 0.0))
    		printf("log(10.0) is positive\n"); // log(10.0) is positive
    	else
    		printf("log(10.0) is not positive\n");
    }
    { // std::isgreaterequal: Returns whether x is greater than or equal to y
    	if (std::isgreaterequal(result, 0.0))
    		printf("log(10.0) is not negative\n"); // log(10.0) is not negative
    	else
    		printf("log(10.0) is negative\n");
    }
    { // std::isless: Returns whether x is less than y
    	if (std::isless(result, 0.0))
    		printf("log(10.0) is negative\n");
    	else
    		printf("log(10.0) is not negative\n"); // log(10.0) is not negative
    }
    { // std::islessequal: Returns whether x is less than or equal to y
    	if (std::islessequal(result, 0.0))
    		printf("log(10.0) is not positive\n");
    	else
    		printf("log(10.0) is positive\n"); // log(10.0) is positive
    }
    { // std::islessgreater: Returns whether x is less than or greater than y
    	if (islessgreater(result, 0.0))
    		printf("log(10.0) is not zero\n"); // log(10.0) is not zero
    	else
    		printf("log(10.0) is zero\n");
    }
    { // std::isunordered: Returns whether x or y are unordered values
    	double result;
    	result = std::sqrt(-1.0);
    	if (std::isunordered(result, 0.0))
    		printf("sqrt(-1.0) and 0.0 cannot be ordered\n"); // sqrt(-1.0) and 0.0 cannot be ordered
    	else
    		printf("sqrt(-1.0) and 0.0 can be ordered\n");
    }
    	return 0;
    }
    } // namespace cmath_

     

    展开全文
  • C++中有对应相同作用的头文件cmath”,当然C++中两个头文件都可以使用,C++向C兼容。2. 面试必掌握的内容 关于数学函数的内容有很多,需要注意使用的地方就是,输入参数绝大多数要求的是double双浮点类型。
  • 【C++】cmath文件名详解

    千次阅读 2020-02-23 13:57:34
    【C++】cmath文件名详解 cmath是c++语言中的标准库头文件。其中的 “c” 表示其中的函数是...其使包含math.h的头文件。 绝对值函数 int abs(int i) 返回整型参数i的绝对值 double fabs(double x) 返回双精度参数x...
  • 输入仅一行,包括n,x和y(均为整数)。 【输出】 输出也仅一行,剩下的苹果个数。 【输入样例】 10 4 9 【输出样例】 7 【来源】 No   #include #include <cmath> ...
  • C++/C++11中头文件cmath的使用

    万次阅读 多人点赞 2017-10-07 21:56:10
    cmath>。此头文件中声明了一系列函数来计算常见的数学运算和变换: std::abs: 计算绝对值,包括整数类型; std::fabs: 计算绝对值,不包括整数类型; std::fma(x,y,z):x*y+z; std::sin: 正弦; std::asin: ...
  • C++ cmath头文件

    2021-05-31 21:14:45
    C++中cmath头文件 <math.h>是C标准函数库中的头文件。在C++中一般用。此头文件中声明了一系列函数来计算常见的数学运算和变换: std::abs: 计算绝对值,包括整数类型; std::fabs: 计算绝对值,不包括整数类型...
  • 【C++】cmath库函数详解

    千次阅读 2021-10-30 20:15:46
    其使包含math.h的头文件。 绝对值函数。C++中fabs()函数位于< cmath >中,其余绝对值函数位于< cstdlib >中。 int abs(int i) 返回整型参数i的绝对值 double fabs(double x) 返回双精度参数x的绝对值...
  • 几个常用的头文件

    2022-05-27 16:31:54
    【输入】 一行,包含两个浮点数A,B,中间用单个空格隔开。A和B均在浮点数范围内。 【输出】 一个浮点数,即A+B的值。保证结果在浮点型范围内。 【输入样例】 1.1 2.5 【输出样例】 3.6 代码 #include #include ...
  • 关于cmath.h头文件

    千次阅读 2011-09-06 10:25:21
    虽然 cmath 大部分的函数和 math.h 都是一样的,但是math.h是C语言的函数库,cmath是C++ 的的函数库,两者并不等同 C++程序推荐使用 #include C程序推荐使用 #include
  • iomanip 包含:dec 置基数为10 相当于"%d"hex 置基数为16 相当于"%X"oct 置基数为8 相当于"%o"setfill(c) 设填充字符为csetprecision(n) 设显示小数精度为n位setw(n) 设域宽为n个字符setioflags(ios::fixed) 固定的...
  • math 头文件的用法 math头文件可以调用一些math标准库里数学函数,比如说fabs()、sqrt()、pow()等等,下面我结合具体的代码实现这些函数的应用。 特别注意:调用math函数库里的函数后,值自动转换成double型 fabs()...
  • python-cmath模块

    2020-12-15 13:52:32
    cmath ---关于复数的数学函数这一模块提供了一些关于复数的数学函数。该模块的函数的参数为整数、浮点数或复数。这些函数的参数也可为一个拥有__complex__()或__float__()方法的Python对象,这些方法分别用于将对象...
  • c++——万能头文件

    2022-04-09 12:45:38
    万能头文件就是一个包含几乎所有的头文件,如: #include #include #include #include #include <cmath> #include <deque> #include <vector> #include <queue> #include #include <cstring> #include ...
  • 转载C++标准头文件 IOSTRERAM,...如果头文件中需要用到cin,cout,则必须要包含iostream,而在包含了iostream以后,cmath, cstdlib, stdexcept, exception, climits 等头文件都不需要二次包含,iostream已经包含了。 ...
  • 标签:cmath.h常用函数int abs (int x);double acos (double x);double asin (double x);double atan (double x);double atan2 (double y, double x);double atof (const char *s);double ceil (double x);do...
  • 主要介绍了C++常用的#include头文件,对初学者理解C++程序设计大有好处的相关资料
  • c语言头文件大全.doc

    2021-01-09 20:40:43
    本文档列出了c语言中的大多数头文件,可用于日常函数调用查找
  • 收集整理得到的C++\C头文件资源包,包含470个文件,比较常见的Windows.h、Winnt.h、Specstrings.h等头文件均有包含
  • #include<bits/stdc++.h>...这个头文件包含以下等等C++中包含的所有头文件: #include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #inclu...
  • c++各个头文件包含常用函数

    千次阅读 多人点赞 2018-10-30 16:42:11
    c++各个头文件包含常用函数 目录: 1.&lt;cstdlib&gt; 2.&lt;cctype&gt; 3.&lt;cstring&gt; 4.&lt;cmath&gt; 5.&lt;string&gt; 6.&lt;algorithm&gt; 7.&...
  • 一个包含所有c++的头文件头文件

    万次阅读 2017-01-18 17:16:21
    一个包含所有c++的头文件头文件 #include 做CF看见别人用这个函数,然后就能直接用vector,set,string那些函数了,摸不着头脑,感觉特神奇就百度了一下,才发现这个是C++版本升级,然后文件自带的,方便了程序员...
  • C语言头文件包含的函数,让c更深入,更上一层楼。
  • c和c++头文件

    2017-12-03 23:31:46
    这里包含了c和c++的头文件,便于深入学习c和c++。其实c和c++需要深入学习
  • 该函数头文件:math.h;该函数功能: 计算一个非负实数的平方根;说明:sqrt系Square Root Calculations(平方根计算C语言CSS布局HTML小编今天和大家分享平方根?欢迎来到CSS布局HTML,我上学的时候...
  • C++常用头文件

    万次阅读 多人点赞 2021-06-29 11:47:51
    一个完整的C++程序,总会包含那么几个头文件,那么C++里有哪些常用的头文件呢?让我们来看一下吧! 1.include<iostream> 输入输出流,得调用这个头文件才能使用cin,cout 2.#include<iomanip> 可以调用...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 5,093
精华内容 2,037
关键字:

cmath头文件包含什么