zoukankan      html  css  js  c++  java
  • C# 如何判断数据是否为 NaN

    double a = 0 / 0d;

    if (double.IsNaN(a)){ //do } 在浮点数计算中,

    0除以0将得到NaN

    ,正数除以0将得到PositiveInfinity

    ,负数除以0将得到NegativeInfinity

    。 浮点数运算从不引发异常。

     C#语言中,对于 int,long 和 decimal类型的数,任何数除以 0 所得的结果是无穷大,不在int,long 和 decimal 类型的范围之内,所以计算 6/0 之类的表达式会出错。

     但是,double 和 float 类型实际上有一个可以表示无穷大的特殊值:5.0/0.0 = Infinity (无穷大),这个规则唯一的例外是0.0/0.0 = NaN (Not a Number)。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace Infinity_NaN
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             Console.WriteLine("5 / 2 = {0}", 5 / 2);            // 2
    13             Console.WriteLine("5.0 / 2.0 = {0}", 5.0 / 2.0);    // 2.5
    14             Console.WriteLine("5.0 / 2 = {0}", 5.0 / 2);        // 2.5
    15             Console.WriteLine("5 / 2.0 = {0}", 5 / 2.0);        // 2.5
    16             Console.WriteLine("5.0 / 0.0 = {0}", 5.0 / 0.0);    // Infinity
    17             Console.WriteLine("5.0 / 0 = {0}", 5.0 / 0);      // Infinity
    18             Console.WriteLine("0.0 / 0.0 = {0}", 0.0 / 0.0);    // NaN
    19             Console.WriteLine("5 / 0.0 = {0}", 5 / 0.0);        // Infinity
    20             Console.WriteLine("0.0 / 0 = {0}", 0.0 / 0);        // NaN
    21 
    22             //Console.WriteLine("5 / 0 = {0}", 5 / 0); // Err: Division by constant zero
    23             //Console.WriteLine("0 / 0 = {0}", 0 / 0); // Err: Division by constant zero
    24             
    25             // Infinity + 10 = Infinity
    26             // Infinity * 0 = 0
    27             // NaN + 10 = NaN
    28             // NaN * 0 = NaN
    29         }
    30     }
    31 }
  • 相关阅读:
    unity, texture import settings
    unity, 最简单的additive shader
    unity, shader input and output
    unity, multi pass shader中的surface pass
    unity, 荧光效果(bloom)
    unity, 查看内置shader源码
    unity, Find References In Scene
    unity 主循环
    unity 显示帧率
    HttpUrlConnection的setDoOutput与setDoInput的区别
  • 原文地址:https://www.cnblogs.com/CharlesGrant/p/3640289.html
Copyright © 2011-2022 走看看