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.12, 2) = 12.120000000000001
Math.RoundUp(12.119, 2) = 12.119999999999999
Math.RoundDown(12.121, 2) = 12.120000000000001

Thus, the result of comparison Math.RoundUp(12.119, 2) = Math.RoundDown(12.121, 2) is negative.

Specify tolerated error before comparing real numbers:

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

See also:

Developers Knowledge Base