Tag: 除零

如何防止零除?

ads = ads.Where(x => (x.Amount – x.Price) / (x.Amount / 100) >= filter.Persent); 如果x.Amount == 0我有错误“遇到零除错误”。 喜欢我在这个要求是避免? 更新: 这有帮助,但我不喜欢这个决定: ads = ads.Where(x => (x.Amount – x.Price) / ((x.Amount / 100)==0?0.1:(x.Amount / 100)) >= filter.Persent); 还有另外一种方法吗?

Int vs Double并除以零exception

当整数除以零时,我们得到编译时错误;而在double的情况下,没有编译错误,但在运行时,我们得到无穷大/ NaN作为结果。 知道为什么int和double在除零exception时会有不同的行为吗? void Main() { int number = 20; var result1 = number/0; // Divide by zero compile time exception double doubleNumber = 20; var result2 = doubleNumber/0.0; // no compile time error. Result is infinity or NaN }