本文整理汇总了C#中System.Math.Abs方法的典型用法代码示例。如果您正苦于以下问题:C# Math.Abs方法的具体用法?C# Math.Abs怎么用?C# Math.Abs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Math
的用法示例。
在下文中一共展示了Math.Abs方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: foreach
decimal[] decimals = { Decimal.MaxValue, 12.45M, 0M, -19.69M,
Decimal.MinValue };
foreach (decimal value in decimals)
Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
输出:
Abs(79228162514264337593543950335) = 79228162514264337593543950335 Abs(12.45) = 12.45 Abs(0) = 0 Abs(-19.69) = 19.69 Abs(-79228162514264337593543950335) = 79228162514264337593543950335
示例2: foreach
double[] doubles = { Double.MaxValue, 16.354e-17, 15.098123, 0,
-19.069713, -15.058e18, Double.MinValue };
foreach (double value in doubles)
Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
输出:
Abs(1.79769313486232E+308) = 1.79769313486232E+308 Abs(1.6354E-16) = 1.6354E-16 Abs(15.098123) = 15.098123 Abs(0) = 0 Abs(-19.069713) = 19.069713 Abs(-1.5058E+19) = 1.5058E+19 Abs(-1.79769313486232E+308) = 1.79769313486232E+308
示例3: foreach
short[] values = { Int16.MaxValue, 10328, 0, -1476, Int16.MinValue };
foreach (short value in values)
{
try {
Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
}
catch (OverflowException) {
Console.WriteLine("Unable to calculate the absolute value of {0}.",
value);
}
}
输出:
Abs(32767) = 32767 Abs(10328) = 10328 Abs(0) = 0 Abs(-1476) = 1476 Unable to calculate the absolute value of -32768.
示例4: foreach
int[] values = { Int32.MaxValue, 16921, 0, -804128, Int32.MinValue };
foreach (int value in values)
{
try {
Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
}
catch (OverflowException) {
Console.WriteLine("Unable to calculate the absolute value of {0}.",
value);
}
}
输出:
Abs(2147483647) = 2147483647 Abs(16921) = 16921 Abs(0) = 0 Abs(-804128) = 804128 Unable to calculate the absolute value of -2147483648.
示例5: foreach
long[] values = { Int64.MaxValue, 109013, 0, -6871982, Int64.MinValue };
foreach (long value in values)
{
try {
Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
}
catch (OverflowException) {
Console.WriteLine("Unable to calculate the absolute value of {0}.",
value);
}
}
输出:
Abs(9223372036854775807) = 9223372036854775807 Abs(109013) = 109013 Abs(0) = 0 Abs(-6871982) = 6871982 Unable to calculate the absolute value of -9223372036854775808.
示例6: foreach
sbyte[] values = { SByte.MaxValue, 98, 0, -32, SByte.MinValue };
foreach (sbyte value in values)
{
try {
Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
}
catch (OverflowException) {
Console.WriteLine("Unable to calculate the absolute value of {0}.",
value);
}
}
输出:
Abs(127) = 127 Abs(98) = 98 Abs(0) = 0 Abs(-32) = 32 Unable to calculate the absolute value of -128.
示例7: foreach
float[] values= { Single.MaxValue, 16.354e-12F, 15.098123F, 0F,
-19.069713F, -15.058e17F, Single.MinValue };
foreach (float value in values)
Console.WriteLine($"Abs({value}) = {Math.Abs(value)}");
输出:
Abs(3.402823E+38) = 3.402823E+38 Abs(1.6354E-11) = 1.6354E-11 Abs(15.09812) = 15.09812 Abs(0) = 0 Abs(-19.06971) = 19.06971 Abs(-1.5058E+18) = 1.5058E+18 Abs(-3.402823E+38) = 3.402823E+38
示例8: Main
//引入命名空间
using System;
public class MainClass {
public static void Main( ) {
double epsilon = 1.0e-9;
double guess = 11.0;
double result = 0.0;
double value = 2;
result = ((value / guess) + guess) / 2;
do {
Console.WriteLine( "Guess Value = {0}", guess );
Console.WriteLine( "Result Value = {0}", result );
guess = result;
result = ((value / guess) + guess) / 2;
} while( Math.Abs(result - guess) > epsilon );
Console.WriteLine("The approx sqrt of {0} is {1}", value, result );
}
}