Determine if two numbers are close in value. - CSharp System

CSharp examples for System:Math Number

Description

Determine if two numbers are close in value.

Demo Code

// (c) Copyright Microsoft Corporation.
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;

public class Main{
        /// <summary>
        /// Determine if two numbers are close in value.
        /// </summary>
        /// <param name="left">First number.</param>
        /// <param name="right">Second number.</param>
        /// <returns>
        /// True if the first number is close in value to the second, false
        /// otherwise.
        /// </returns>
        public static bool AreClose(double left, double right)
        {//from w w w  .j  a  va  2  s.  co m
            if (left == right)
            {
                return true;
            }

            double a = (Math.Abs(left) + Math.Abs(right) + 10.0) * 2.2204460492503131E-16;
            double b = left - right;
            return (-a < b) && (a > b);
        }
}

Related Tutorials