Comparison of Real Numbers

Article number: KB000019

General Information

Related blocks:

Description

Due to features of internal representation of real numbers it is incorrect to compare them using the "=" statement.

Real numbers may differ depending on the way they have been obtained. For example:

Math.Round(12.122) = 12.120000000000001
Math.RoundUp(12.1192) = 12.119999999999999
Math.RoundDown(12.1212) = 12.120000000000001

Thus the result of comparison Math.RoundUp(12.1192) = Math.RoundDown(12.1212) is negative.

Specify tolerated error before comparing real numbers:

Math.Abs(Double1-Double2)<0.00...01

See also:

Developers Knowledge Base