Ada 95 Quality and Style Guide | Chapter 5 |

5.5.6 Accuracy of Operations With Real Operands

guideline

Use <=and>=in relational expressions with real operands instead of=.

exampleCurrent_Temperature : Temperature := 0.0; Temperature_Increment : Temperature := 1.0 / 3.0; Maximum_Temperature : constant := 100.0; ... loop ... Current_Temperature := Current_Temperature + Temperature_Increment; ... exit when Current_Temperature >= Maximum_Temperature; ... end loop;

rationaleFixed- and floating-point values, even if derived from similar expressions, may not be exactly equal. The imprecise, finite representations of real numbers in hardware always have round-off errors so that any variation in the construction path or history of two real numbers has the potential for resulting in different numbers, even when the paths or histories are mathematically equivalent.

The Ada definition of model intervals also means that the use of

<=is more portable than either<or=.

notesFloating-point arithmetic is treated in Guideline 7.2.7.

exceptionsIf your application must test for an exact value of a real number (e.g., testing the precision of the arithmetic on a certain machine), then the

=would have to be used. But never use=on real operands as a condition to exit a loop .

< Previous Page |
Search | Contents | Index | Next Page > |

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |

TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC |

Appendix | References | Bibliography |