C# 中的 Infinity 和 NaN
2011-08-04 16:02
323 查看
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)。
运行后结果如下图所示:
但是,double 和 float 类型实际上有一个可以表示无穷大的特殊值:5.0/0.0 = Infinity (无穷大),这个规则唯一的例外是0.0/0.0
= NaN (Not a Number)。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Infinity_NaN { class Program { static void Main(string[] args) { Console.WriteLine("5 / 2 = {0}", 5 / 2); // 2 Console.WriteLine("5.0 / 2.0 = {0}", 5.0 / 2.0); // 2.5 Console.WriteLine("5.0 / 2 = {0}", 5.0 / 2); // 2.5 Console.WriteLine("5 / 2.0 = {0}", 5 / 2.0); // 2.5 Console.WriteLine("5.0 / 0.0 = {0}", 5.0 / 0.0); // Infinity Console.WriteLine("5.0 / 0 = {0}", 5.0 / 0); // Infinity Console.WriteLine("0.0 / 0.0 = {0}", 0.0 / 0.0); // NaN Console.WriteLine("5 / 0.0 = {0}", 5 / 0.0); // Infinity Console.WriteLine("0.0 / 0 = {0}", 0.0 / 0); // NaN //Console.WriteLine("5 / 0 = {0}", 5 / 0); // Err: Division by constant zero //Console.WriteLine("0 / 0 = {0}", 0 / 0); // Err: Division by constant zero // Infinity + 10 = Infinity // Infinity * 0 = 0 // NaN + 10 = NaN // NaN * 0 = NaN } } }
运行后结果如下图所示:
相关文章推荐
- C#的Infinity和NaN
- 浅谈C#中的Infinity和NaN
- Kaggle-ValueError: Input contains NaN, infinity or a value too large for dtype('float32').
- Infinity,NaN
- NaN and infinity literals not allowed by shader model 错误解决方法
- Double 中的 NAN与INFINITY
- java中Infinity和NAN
- Java中的NAN和INFINITY
- Scala和Java中的Infinity和NaN
- JavaScript那些事儿(2):Math,Infinity与NaN,它们不是浮云,中学数学都学过
- java NAN 和 INFINITY
- C#中的Infinity有个小坑
- C# 如何判断数据是否为 NaN
- Infinity和NaN
- Java中的Infinity和NaN
- 奇怪的js NaN and Infinity
- 20.5.2 Infinity and NaN
- 警惕运算中的INFINITY 和 NaN
- Infinity NaN undefined和null
- js中的特殊数值——NaN,+0,-0,Infinity,undefined,null