常用math.h数学函数以及其他函数(吉林大学 孙立鑫)

这篇具有很好参考价值的文章主要介绍了常用math.h数学函数以及其他函数(吉林大学 孙立鑫)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  

目录

  1.math.h 头文件的常用函数

   a.signbit(求浮点数是否含有符号)

   b.三角函数汇总

   c.双曲函数

   d.指数函数对数函数

   e.分解浮点数(详解如下)frexp

   f.取浮点数的指数部分ilogb

   g.反分解浮点数(详解如下)ldexp

   h.浮点数的取整和取小 modf

   i.四舍五入(round)

   j.浮点数砍去小数部分(trunc)

   k.向上取整(ceil)

   l.向下取整(floor)

   m.浮点数取余fmod

   n.x的y次方pow函数

   o.平方根sqrt

   p.立方根cbrt

   q.绝对值fabs

   r.已知直角边求斜边hypot

   s.两数取大小 fmax fmin

  2.其余常用函数

   a.全排列

   b.最大公因数 最小公倍数


1.math.h 头文件的常用函数

 a.signbit(求浮点数是否含有符号)

#include<stdio.h>
#include<math.h>
int main(void)
{
	float a = 1, b = -1;
	int c,d;
	c = signbit(a);
	d = signbit(b);
	printf("%d %d", c,d);
	return 0;
}

  由于signbit返回值是布尔类型,因此我们可以用整形来存储值。

 输出结果如下

0 1

 在二进制补码中 0开头是代表真值是正数,1代表真值是负数。

 b.三角函数汇总

 有

一个输入:正弦sin sinf sinl 余弦cos cosf cosl 正切tan tanf tanl 反正弦asin asinf asinl 反余弦acos acosf acosl 反正切atan atanf atanl

 两个输入:求偏转角 atan2 atan2f atan2l(原点与输入点的连线与y轴正半轴夹角的弧度值)

一个输入:例

#include<stdio.h>
#include<math.h>
#define pi 3.1415926
int main(void)
{
	double a = sin(0.2 * pi);
	double b = cos(0.2 * pi);
	double c = tan(0.2 * pi);
	double d = asin(0.2 * pi);
	double e = acos(0.2 * pi);
	double f = atan(0.2 * pi);
	float a1 = sinf(0.2 * pi);
	float b1 = cosf(0.2 * pi);
	float c1 = tanf(0.2 * pi);
	float d1 = asinf(0.2 * pi);
	float e1 = acosf(0.2 * pi);
	float f1 = atanf(0.2 * pi);
	long double a2 = sinl(0.2 * pi);
	long double b2 = cosl(0.2 * pi);
	long double c2 = tanl(0.2 * pi);
	long double d2 = asinl(0.2 * pi);
	long double e2 = acosl(0.2 * pi);
	long double f2 = atanl(0.2 * pi);
	printf("%f %f %f %f %f %f\n", a1, b1, c1, d1, e1, f1);
	printf("%lf %lf %lf %lf %lf %lf\n", a, b, c, d, e, f);
	printf("%llf %llf %llf %llf %llf %llf", a2, b2, c2, d2, e2, f2);
	return 0;
}

 输出如下

0.587785 0.809017 0.726543 0.679390 0.891406 0.560982
0.587785 0.809017 0.726543 0.679390 0.891406 0.560982
0.587785 0.809017 0.726543 0.679390 0.891406 0.560982

 两个输入:例

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a=atan2(1,-1);
	double b = a / 3.1415926;
	printf("%lf*pi\n", b);
	float a1 = atan2f(1, -1);
	float b1 = a1 / 3.1415926;
	printf("%f*pi\n", b1);
	long double a2 = atan2l(1, -1);
	long double b2 = a2 / 3.1415926;
	printf("%lf*pi", b2);
	return 0;
}

 输出如下

0.750000*pi
0.750000*pi
0.750000*pi

 注:atan2 atan2f atan2l 求的是原点与输入点所连线与y轴正半轴的夹角。

 c.双曲函数

 有

 双曲正弦sinh sinhf sinhl  双曲余弦cosh coshf coshl 双曲正切tanh tanhf tanhl 反双曲正弦asinh asinhf asinhl 反双曲余弦acosh acoshf scoshl

反双曲正切atanh atanhf atanhl

#include<stdio.h>
#include<math.h>
#define pi 3.1415926
int main(void)
{
	double a = sinh(0.3 * pi);
	double b = cosh(0.3 * pi);
	double c = tanh(0.3 * pi);
	double d = asinh(0.3 * pi);
	double e = acosh(pi);
	double f = atanh(0.3 * pi);
	float a1 = sinhf(0.3 * pi);
	float b1 = coshf(0.3 * pi);
	float c1 = tanhf(0.3 * pi);
	float d1 = asinhf(0.3 * pi);
	float e1 = acoshf(pi);
	float f1 = atanhf(0.3 * pi);
	long double a2 = sinhl(0.3 * pi);
	long double b2 = coshl(0.3 * pi);
	long double c2 = tanhl(0.3 * pi);
	long double d2 = asinhl(0.3 * pi);
	long double e2 = acoshl( pi);
	long double f2 = atanhl(0.3 * pi);
	printf("%lf %lf %lf %lf %lf %lf\n", a, b, c, d, e, f);
	printf("%f %f %f %f %f %f\n", a1, b1, c1, d1, e1, f1);
	printf("%llf %llf %llf %llf %llf %llf", a2, b2, c2, d2, e2, f2);
	return 0;
}

输出如下

1.088336 1.477997 0.736359 0.840109 1.811526 1.759774
1.088336 1.477997 0.736359 0.840109 1.811526 1.759774
1.088336 1.477997 0.736359 0.840109 1.811526 1.759774

 d.指数函数 对数函数

 有

e的x次方 exp explf expl 2的x次方 exp2 exp2f exp2l e的x次方减一 expm1 expm1f expm1l

ln(x):log logf logl   log2(x): log2 log2f log2l  log10(x): log10 log10f log10l

log2(x)取整logb logbf logbl

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = exp(2);
	double b = exp2(2);
	double c = expm1(2);
	double d = log(2);
	double e = log2(2);
	double f = log10(2);
	double g = logb(2);
	float a1 = expf(2);
	float b1= exp2f(2);
	float c1 = expm1f(2);
	float d1 = logf(2);
	float e1 = log2f(2);
	float f1 = log10f(2);
	float g1 = logbf(2);
	long double a2 = expl(2);
	long double b2 = exp2l(2);
	long double c2 = expm1l(2);
	long double d2 = logl(2);
	long double e2 = log2l(2);
	long double f2 = log10l(2);
	long double g2 = logbl(2);
	printf("%lf %lf %lf %lf %lf %lf %lf\n", a, b, c, d, e, f, g);
	printf("%f %f %f %f %f %f %f\n", a1, b1, c1, d1, e1, f1, g1);
	printf("%llf %llf %llf %llf %llf %llf %llf\n", a2, b2, c2, d2, e2, f2, g2);
	return 0;
}

输出如下

7.389056 4.000000 6.389056 0.693147 1.000000 0.301030 1.000000
7.389056 4.000000 6.389056 0.693147 1.000000 0.301030 1.000000
7.389056 4.000000 6.389056 0.693147 1.000000 0.301030 1.000000

e.分解浮点数(详解如下)frexp

有 frexp frexpf frexpl

若a为一个浮点数 令b为整形  c为一个浮点数 若a=c*exp2(b)

c=frexp(a,&b);

 例:

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = 123.4567;
	int b = 10;
	double c;
	c = frexp(a, &b);
	float d;
	d = frexpf(a, &b);
	long double e;
	e = frexpl(a, &b);
	printf("%lf %f %llf\n", c, d, e);
	/*验算*/
	double f = c * exp2(b);
	printf("%lf", f);
	return 0;
}

输出如下

0.964505 0.964505 0.964505
123.456700

验算正确

f.取浮点数的指数部分ilogb

有 ilogb ilogbf ilogbl

若 a为一个浮点数 b为一个整数 

a=exp2(b);

b=ilogb(a);

(向下取) ilogb(256.000000)=8;

                   ilogb(257.000000)=8;

                   ilogb(255.000000)=7;

例:

#include<stdio.h>
#include<math.h>
int main(void)
{
	int b;
	double a = 255;
	b = ilogb(a);
	printf("%d ", b);
	int b1;
	float a1 = 255;
	b1 = ilogbf(a1);
	printf("%d ", b1);
	int b2;
	long double a2 = 255;
	b2 = ilogbl(a2);
	printf("%d", b2);
	return 0;
}

输出如下

7 7 7

 g.反分解浮点数(详解如下)ldexp

有ldexp ldexpf ldexpl

 若a为一个浮点数 令b为整形  c为一个浮点数 若a=c*exp2(b)

 c=frexp(a,&b);

  q=ldexp(c,b);

 例

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a;
	float a1;
	long double a2;
	double b = 4;
	int c = 3;
	a = ldexp(b, c);
	a1 = ldexpf(b, c);
	a2 = ldexp(b, c);
	printf("%lf %f %llf", a, a1, a2);
	return 0;
}

输出如下

32.000000 32.000000 32.000000

h.浮点数的取整和取小 modf

有modff modfl

若a为浮点数 b为整数 c为小于1的浮点数 a=b+c

那么 有c=modf(a,&b);

例:

#include<stdio.h>
#include<math.h>
int main(void)
{
	double c;
	float c1;
	long double c2;
	double b;
	float b1;
	long double b2;
	double a = 1.2345;
	float a1 = 1.2345;
	long double a2 = 1.2345;
	c = modf(a, &b);
	c1 = modff(a1, &b1);
	c2= modfl(a2, &b2);
	printf("%lf %lf\n", b, c);
	printf("%f %f\n", b1, c1);
	printf("%llf %llf", b2, c2);
	return 0;
}

输出如下

1.000000 0.234500
1.000000 0.234500
1.000000 0.234500

i.四舍五入(round)

round为传统的四舍五入

返回值为浮点型:有 round roundf roundl

返回值为整形:有lroung lrounff lroundl llround llrounff llroundl

返回值为浮点形:

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = 1.234;
	float a1 = 1.234;
	long double a2 = 1.234;
	double b;
	float b1;
	long double b2;
	b = round(a);
	b1 = round(a1);
	b2 = round(a2);
	printf("%lf %f %llf", b, b1, b2);
	return 0;
}

输出为

1.000000 1.000000 1.000000

返回值为整形:

#include<stdio.h>
#include<math.h>
int main(void)
{
	long a, a1, a2;
	long long b, b1, b2;
	double c = 1.234;
	float c1 = 1.234;
	long double c2 = 1.234;
	a = lround(c);
	a1 = lroundf(c1);
	a2 = lround(c2);
	b = llround(c);
	b1 = llround(c1);
	b2 = llround(c2);
	printf("%ld %ld %ld\n", a, a1, a2);
	printf("%lld %lld %lld", b, b1, b2);
	return 0;
}

输出如下

1 1 1
1 1 1

   j.浮点数砍去小数部分(trunc)

  有

trunc truncf truncl

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = 1.234;
	float a1 = 1.234;
	long double a2 = 1.234;
	double b;
	float b1;
	long double b2;
	b = trunc(a);
	b1 = truncf(a1);
	b2 = truncl(a2);
	printf("%lf %f %llf", b, b1, b2);
	return 0;
}

输出如下

1.000000 1.000000 1.000000

k.向上取整(ceil)

有ceilf ceill

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = 1.234;
	float a1 = 1.234;
	long double a2 = 1.234;
	double b;
	float b1;
	long double b2;
	b = ceil(a);
	b1 = ceilf(a1);
	b2 = ceill(a2);
	printf("%lf %f %llf", b, b1, b2);
	return 0;
}

输出如下

2.000000 2.000000 2.000000

l.向下取整(floor)

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = 1.234;
	float a1 = 1.234;
	long double a2 = 1.234;
	double b;
	float b1;
	long double b2;
	b = floor(a);
	b1 = floorf(a1);
	b2 = floorl(a2);
	printf("%lf %f %llf", b, b1, b2);
	return 0;
}

输出如下

1.000000 1.000000 1.000000

m.浮点数取余fmod

有 fmodf fmodl

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = fmod(5.5, 3.3);
	float b = fmodf(5.5, 3.3);
	long double c = fmod(5.5, 3.3);
	printf("%lf %f %llf", a, b, c);
	return 0;
}

输出如下

2.200000 2.200000 2.200000

n.x的y次方pow函数

有 powf powl形式

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = pow(2, 2);
	float b = powf(3, 3);
	long double c = powl(4, 4);
	printf("%lf %f %llf", a, b, c);
	return 0;
}

  输出如下

4.000000 27.000000 256.000000

   o.平方根sqrt

   有sqrtf sqrtl的形式

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = sqrt(3);
	float b = sqrtf(3);
	long double c = sqrtl(3);
	printf("%lf %f %llf", a, b, c);
	return 0;
}

   输出为

1.732051 1.732051 1.732051

   p.立方根cbrt

   有cbrtf cbrtl的形式

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = cbrt(3);
	float b = cbrtf(3);
	long double c = cbrtl(3);
	printf("%lf %f %llf", a, b, c);
	return 0;
}

    输出为:

   

1.442250 1.442250 1.442250

   q.绝对值fabs

   有fabsf fabsl的形式

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = fabs(-3);
	float b = fabsf(-3);
	long double c = fabsl(-3);
	printf("%lf %f %llf", a, b, c);
	return 0;
}

输出为

3.000000 3.000000 3.000000

r.已知直角边求斜边hypot

有 hypotf hypotl的形式

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = hypot(3, 4);
	float b = hypotf(6, 8);
	long double c = hypotl(4, 3);
	printf("%lf %f %llf", a, b, c);
	return 0;
}

输出如下

5.000000 10.000000 5.000000

s.两数取大小 fmax fmin

有fmaxf fmaxl fminf fminl的形式

#include<stdio.h>
#include<math.h>
int main(void)
{
	double a = fmax(1.1, 2.2);
	float b = fmaxf(1.1, 2.2);
	long double c = fmaxl(1.2, 2.2);
	double a1 = fmin(1.1, 2.2);
	float b1 = fminf(1.1, 2.2);
	long double c1 = fminl(1.2, 2.2);
	printf("%lf %f %llf\n", a, b, c);
	printf("%lf %f %llf", a1, b1, c1);
	return 0;
}

输出如下

2.200000 2.200000 2.200000
1.100000 1.100000 1.200000

2.其余常用函数

   a.全排列

int quan(int i)
{
    if (i == 1)
    {
        return 1;
    }
    else
    {
        return i * quan(i - 1);
    }
}

   b.最大公因数 最小公倍数

 最大公因数

int gcd(int a, int b)
{
    if (a % b == 0)
    {
        return b;
    }
    else
    {
        return gcd(b, a % b);
    }
}

最小公倍数(两数之积除以最大公因数)

int gcd(int a, int b)
{
	if (a % b == 0)
	{
		return b;
	}
	else
	{
		return gcd(b, a % b);
	}
}
int bei(int a, int b)
{
	int c;
	c = (a * b) / gcd(a, b);
	return c;
}

总结:本文内容为偏向初学者的基础性知识,是解决复杂问题的基础性工具,希望本篇文章对你的生活和学习有所帮助。文章来源地址https://www.toymoban.com/news/detail-752048.html

到了这里,关于常用math.h数学函数以及其他函数(吉林大学 孙立鑫)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 校企联盟-触觉智能正式成为吉林大学实习实践基地

    近日,触觉智能与吉林大学正式达成了实习实践基地合作,这个合作的达成标志着双方在教育与企业之间搭建了一座连接的桥梁,让高校老师科研项目更贴近市场需要,让优秀的科研成果得到有效转化,让优秀应届大学生获得更多理论实践锻炼机会,也为触觉智能注入了新的

    2024年01月24日
    浏览(36)
  • 2022级吉林大学面向对象第一次上机测试

    1、 1)略 2)如果main,f1,g1,g2或更多的函数之间有更为复杂的调用关系,头文件一般按怎样的规律写呢? 一般情况下,头文件应按照以下规律编写: 头文件名应与包含的函数或类名有关 在头文件中应包含必要的预处理指令,例如#ifndef、#define和#endif,以避免重复包含 应包含函

    2024年02月03日
    浏览(43)
  • 2022级吉林大学面向对象第三次上机测试

    运算符重载、动态内存管理 1.已知字符串类MyString的定义为: 全局函数: const MyString operator + (const MyString ,const MyString );//字符串连接 ostream operator(ostream os, const MyString str); //定向输出 请完整实现MyString类和指定的全局函数。(可以使用new,delete运算以及strcpy,strlen,…等库函数

    2024年02月06日
    浏览(39)
  • 每章一篇博客带你拿下吉林大学JAVAEE期末(一)

    1)JDBC(Java Database Connectivity) 是一种用于执行SQL语句的JavaAPI,可为访问不同的关系型数据库提供一种统一的途径。 2) JNDI(Java Name and Directory Interface,Java 命名和目录接口) 被用于执行名字和目录服务。它提供了一致的模型来存取和操作企业级的资源,如DNS,LDAP,本地文件系统或应

    2024年02月12日
    浏览(27)
  • [保研/考研机试] KY180 堆栈的使用 吉林大学复试上机题 C++实现

    堆栈的使用_牛客题霸_牛客网     堆栈是一种基本的数据结构。堆栈具有两种基本操作方式,push 和 pop。其中 push一个值会将其压入栈顶,而 pop 则会将栈顶的值弹出。现在我们就来验证一下堆栈的使用。 输入描述:     对于每组测试数据,第一行是一个正整数 n(0 n = 1

    2024年02月13日
    浏览(37)
  • 每章一篇博客带你拿下吉林大学JAVAEE期末(七:会话Bean)

    1) 无状态 会话Bean 无状态会话Bean 不维持 和客户端的 会话状态 。 当方法结束的时候,客户端特定的状态就不会被保持。 允许EJB容器将一个实例分配给任意一个客户端。 2) 有状态 会话Bean 有状态会话Bean是一种保持会话状态的服务,每个实例都与特定的客户端相关联,在与

    2024年02月13日
    浏览(59)
  • Python 数学函数和 math 模块指南

    Python 提供了一组内置的数学函数,包括一个广泛的数学模块,可以让您对数字执行数学任务。 内置数学函数。min() 和 max() 函数可用于在可迭代对象中查找最低或最高值: 示例 :查找可迭代对象中的最低或最高值: abs() 函数返回指定数字的绝对值(正数): 示例 :返回

    2024年02月07日
    浏览(32)
  • 【Java基础教程】(三十六)常用类库篇 · 第六讲:数学运算类——全面讲解Java数学计算支持类库,BigDecimal、Math、Random、DecimalFormat...~

    在现代软件开发中,数学计算是不可或缺的一部分。为了满足企业及开发人员对数学运算的需求,Java 提供了一系列强大而丰富的数学计算相关类,其中包括 Math 、 Random 、 BigDecimal 等等。这些类旨在提供高度精确和可靠的数学操作,使开发人员能够处理任何规模和复杂度的定

    2024年02月16日
    浏览(23)
  • math_常用放缩不等式及其变形@指数@对数@三角函数@一次函数

    x 0 x0 x 0 sin ⁡ x x ( x 0 ) sin{x}x(x0) sin x x ( x 0 ) ln ⁡ x ⩽ x − 1 ( x 0 ) ln{x}leqslant{x-1}(x0) ln x ⩽ x − 1 ( x 0 ) ln ⁡ ( x ) + 1 ⩽ x ln{(x)}+1leqslant{x} ln ( x ) + 1 ⩽ x ln ⁡ ( x + 1 ) ⩽ x ( x 0 ) ln{(x+1)}leqslant{x}(x0) ln ( x + 1 ) ⩽ x ( x 0 ) 令 t = x + 1 , t 1 0 令t=x+1,t10 令 t = x + 1 , t 1 0 ln ⁡ ( t ) ⩽

    2024年02月06日
    浏览(34)
  • nmap常用命令以及在同网段下扫描其他主机

    目录 一、nmap的下载 二、nmap常用命令 三、简单使用 四、总结 操作系统:Ventura (不同操作系统命令可能有所不同,仅供参考) 直接进入官网下载 Download the Free Nmap Security Scanner for Linux/Mac/Windows Official Download site for the FreeNmap Security Scanner. Helps with network security, administration, and

    2024年02月13日
    浏览(25)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包