码迷,mamicode.com
首页 > 其他好文 > 详细

[Basic] 浮点型比较需要注意精度

时间:2016-01-15 14:32:15      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

public class DoubleCompare
    {
        // Fields 浮点型的误差
        private const double DOUBLE_DELTA = 1E-06;

        public static bool AreEqual(double value1, double value2)
        {
            return (value1 == value2)
                || Math.Abs(value1 - value2) < DOUBLE_DELTA;
        }

        public static bool GreaterThan(double value1, double value2)
        {
            return ((value1 > value2) && !AreEqual(value1, value2));
        }

        public static bool GreaterThanOrEqual(double value1, double value2)
        {
            return (value1 > value2) || AreEqual(value1, value2);
        }

        public static bool IsZero(double value)
        {
            return (Math.Abs(value) < DOUBLE_DELTA);
        }

        public static bool LessThan(double value1, double value2)
        {
            return ((value1 < value2) && !AreEqual(value1, value2));
        }

        public static bool LessThanOrEqual(double value1, double value2)
        {
            return (value1 < value2) || AreEqual(value1, value2);
        }
    }

 

[Basic] 浮点型比较需要注意精度

标签:

原文地址:http://www.cnblogs.com/panpan-v1/p/5133156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!