
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
4
|
Chapter 1: Numbers and Enumerations
Discussion
Fractions can be expressed as a numerator over a denominator; however, storing
them as a floating-point value might be necessary. Storing fractions as floating-point
values introduces rounding errors that make it difficult to perform comparisons.
Expressing the value as a fraction (e.g., 1/6) allows the maximum precision. Express-
ing the value as a floating-point value (e.g., 0.16667) can limit the precision of the
value. In this case, the precision depends on the number of digits that the developer
decides to use to the right of the decimal point.
You might need a way to determine whether two values are approximately equal to
each other. This comparison is achieved by defining a value (
epsilon), representing
the smallest positive value by which two numbers can differ and still be considered
equal. In other words, by taking the absolute value of the difference between the
fraction (
numerator/denominator) and the floating-point value (dblValue) and compar-
ing it to a predetermined value passed to the
epsilon argument, you can determine
whether the floating-point value is a good approximation of the fraction.
Consider a comparison between the fraction 1/7 and its floating-point value,
0.14285714285714285. The following call to the
IsApproximatelyEqualTo ...