Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: done

Because floating-point numbers can represent fractions, programmers often mistakenly assume that they can represent any simple fraction exactly. In fact, floating-point numbers are subject to range limitations just as integers are. Furthermore, limitedFloating-point variables must not be used as loop counters. Limited-precision binary floating-point numbers cannot represent

  1. any simple fraction exactly
  2. all decimals precisely, even when the decimals can be represented in a small number of digits.

...

  1. all digits of

...

  1. large values, meaning that incrementing a large floating-point value might not change that value within the available precision.

As a result, floating-point variables must not be used as loop counters.

Noncompliant Code Example

...

The CERT C Secure Coding Standard

FLP30-C. Do not use floating point variables as loop counters

The CERT C++ Secure Coding Standard

FLP30-CPP. Do not use floating point variables as loop counters

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cbdf9b46894c0adb-0f04eaa0-4db74847-a15b8138-9df74d73a982bf01fa6b06b0"><ac:plain-text-body><![CDATA[

[ISO/IEC TR 24772:2010

http://www.aitcnet.org/isai/]

"Floating?point Arithmetic [PLF]"

]]></ac:plain-text-body></ac:structured-macro>

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9dfbd5420b57d0d3-1eda3056-40dd42f5-9c3d8c35-76e9b78e83c16b58365cb742"><ac:plain-text-body><![CDATA[

[[Bloch 2005

AA. Bibliography#Bloch 05]]

Puzzle 34: Down for the Count

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f93642f49e419e64-c5793614-4584457f-a59b879b-b8ea036597aab24f10aa746d"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[§4.2.3, "Floating-Point Types, Formats, and Values"

http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]

]]></ac:plain-text-body></ac:structured-macro>

...