c# 中的 Math常用方法

Math常用方法

1.基本数学运算
1. Abs:返回指定数字的绝对值
int number = -1;
//返回指定数字的绝对值
Math.Abs(number)
2.Ceiling:向上取整
double number = 0.4;
//返回大于或等于指定双精度浮点数的最小整数
Math.Ceiling(number)//1
3.Floor:向下取整
double number = 0.8;
//返回小于或等于指定双精度浮点数的最大整数 
Math.Floor(number)//0
4.Round:四舍五入
double number = 0.8;
//将双精度浮点数四舍五入到最接近的整数或指定的小数位数
Math.Round(number)//1
2.三角函数
1*.Math.Sin*:正弦函数
double radians = Math.PI / 4; // 45度的弧度值  
double sineValue = Math.Sin(radians);  
Console.WriteLine("The sine of " + radians + " radians is " + sineValue);
2.Math.Cos:余弦函数
double radians = Math.PI / 2; // 90度的弧度值  
double cosineValue = Math.Cos(radians);  
Console.WriteLine("The cosine of " + radians + " radians is " + cosineValue);
3.Math.Tan:正切函数
double radians = Math.PI / 4; // 45度的弧度值  
double tangentValue = Math.Tan(radians);  
Console.WriteLine("The tangent of " + radians + " radians is " + tangentValue);
4.Math.Asin:反正弦函数
double sineValue = 0.5; // 假设这是某个角的正弦值  
double radians = Math.Asin(sineValue);  
Console.WriteLine("The angle (in radians) with sine of " + sineValue + " is " + radians);
5.Math.Acos:反余弦函数
double cosineValue = 0.5; // 假设这是某个角的余弦值  
double radians = Math.Acos(cosineValue);  
Console.WriteLine("The angle (in radians) with cosine of " + cosineValue + " is " + radians);
6.Math.Atan和 Math.Atan2:反正切函数
double tangentValue = 1.0; // 假设这是某个角的正切值  
double radians = Math.Atan(tangentValue);  
Console.WriteLine("The angle (in radians) with tangent of " + tangentValue + " is " + radians);  
  
// Math.Atan2用于确定从X轴到点的角度,由Y坐标和X坐标指定  
double y = 1.0;  
double x = 1.0;  
double atan2Radians = Math.Atan2(y, x);  
Console.WriteLine("The angle (in radians) from the X-axis to point (" + x + ", " + y + ") is " + atan2Radians);
3.指数和对数函数
1.Exp: 返回e的指定次幂的值
double baseValue = Math.E; // e 的值,自然对数的底数  
double exponent = 2.0;  
double result = Math.Exp(exponent); // 计算 e 的 2 次方  
Console.WriteLine($"e raised to the power of {exponent} is {result}");
2.Pow:返回指定数字的第一个参数的第二个参数次幂的值
double baseValue = 2.0;  
double exponent = 3.0;  
double result = Math.Pow(baseValue, exponent); // 计算 2 的 3 次方  
Console.WriteLine($"{baseValue} raised to the power of {exponent} is {result}");
3.Log:返回指定数的自然对数(底数为e)
double value = Math.E; // e 的值  
double result = Math.Log(value); // 计算 e 的自然对数  
Console.WriteLine($"The natural log of {value} is {result}");
4.Log10:返回指定数的以10为底的对数
double value = 1000.0;  
double result = Math.Log10(value); // 计算 1000 的以 10 为底的对数  
Console.WriteLine($"The base-10 log of {value} is {result}");
5.Sqrt:返回指定数字的平方根
double  number = 9.0
Math.Sqrt(number)//3
4.其他常用方法
1.Max:返回两个参数中的较大值
int count = 1;
int count1 = 2;
Math.Max(count, count1)//2
2.Min:返回两个参数中的较小值
int count = 1;
int count1 = 2;
Math.Max(count, count1)//1
3.Sign:返回指定数字的符号
//正数返回-1,0返回0,负数返回-1
//对于 NaN,Math.Sign 返回 0,因为 NaN 不被视为正数或负数。对于正无穷大,它返回 1,对于负无穷大,它返回 -1
double positiveNumber = 5.0;
double zero = 0.0;
double negativeNumber = -5.0;
Math.Sign(positiveNumber);//1
Math.Sign(zero);//0
Math.Sign(negativeNumber);//-1
4.PI:获取表示π(圆周率)的常量的值
Math.PI //输出:3.141592653589793
5.E:获取表示自然对数的底数e的常量的值
Math.E //输出:2.718281828459045
``
  • 11
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值