C# Math.Sqrt()用法及代码示例 | 您所在的位置:网站首页 › 如何使用计算器开三次方根函数 › C# Math.Sqrt()用法及代码示例 |
在C#中,Math.Sqrt()是Math类方法,用于计算指定数字的平方根。Sqrt是较慢的计算。可以将其缓存以提高性能。 用法: public static double Sqrt(double d)参数: d:要计算其平方根的数字,此参数的类型为System.Double。返回类型:此方法返回d的平方根。如果d等于NaN,NegativeInfinity或PositiveInfinity,则返回该值。此方法的返回类型为System.Double。 例子: Input : Math.Sqrt(81) Output : 9 Input : Math.Sqrt(-81) Output : NaN Input : Math.Sqrt(0.09) Output : 0.3 Input : Math.Sqrt(0) Output : 0 Input : Math.Sqrt(-0) Output : 0下面的C#程序说明了Math.Sqrt()的工作: 示例1:当参数为正双精度值时,此方法将返回给定值的平方根。 // C# program to illustrate the // Math.Sqrt() method using System; class GFG { // Main Method public static void Main() { double x = 81; // Input positive value, Output square root of x Console.Write(Math.Sqrt(x)); } } 输出: 9 示例2:当参数为Negative时,此方法将返回NaN。 // C# program to illustrate the Math.Sqrt() // method when the argument is Negative using System; class GFG { // Main method public static void Main() { double x = -81; // Input Negative value, Output square root of x Console.Write(Math.Sqrt(x)); } } 输出: NaN 示例3:如果参数是带小数位的双精度值,则此方法将返回给定值的平方根。 // C# program to illustrate the Math.Sqrt() // method when the argument is double value // with decimal places using System; class GFG { // Main Method public static void Main() { double x = 0.09; // Input value with decimal places, // Output square root of x Console.Write(Math.Sqrt(x)); } } 输出: 0.3 示例4:当参数为正零或负零时,它将返回结果为零。 // C# program to illustrate the Math.Sqrt() // method when the argument is positive // or negative Zero using System; class GFG { // Main Method public static void Main() { double x = 0; // Input value positive Zero, Output // square root of x Console.WriteLine(Math.Sqrt(x)); double y = -0; // Input value Negative Zero, // Output square root of y Console.Write(Math.Sqrt(y)); } } 输出: 0 0注意:如果该值太大,则会给出编译时间错误,如错误CS1021:积分常数太大。 参考: https://msdn.microsoft.com/en-us/library/system.math.sqrt
|
CopyRight 2018-2019 实验室设备网 版权所有 |