当前位置: 首页>>代码示例>>C#>>正文


C# Math.Abs方法代码示例

本文整理汇总了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)}");
开发者ID:.NET开发者,项目名称:System,代码行数:4,代码来源:Math.Abs

输出:

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)}");
开发者ID:.NET开发者,项目名称:System,代码行数:4,代码来源:Math.Abs

输出:

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);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:Math.Abs

输出:

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);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:Math.Abs

输出:

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);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:Math.Abs

输出:

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);
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:11,代码来源:Math.Abs

输出:

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)}");
开发者ID:.NET开发者,项目名称:System,代码行数:4,代码来源:Math.Abs

输出:

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 );
    }
}
开发者ID:C#程序员,项目名称:System,代码行数:22,代码来源:Math.Abs


注:本文中的System.Math.Abs方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。