== Why is this an issue?

The safe use of floating-point arithmetic requires a high level of numerical analysis skills and in-depth knowledge of the compiler and target hardware.


If floating-point is to be used, then the following issues need to be covered as part of the deviation process:

* A justification explaining why floating-point is the appropriate or only solution.
* Demonstrate that appropriate skills are available.
* Demonstrate that an appropriate process is being applied.
* Document the floating-point implementation.

The paper “What Every Computer Scientist Should Know about Floating-Point Arithmetic” explains the issues that need to be considered when using floating-point.


For example, when solving a quadratic equation, the value of b^2^-4.a.c is calculated. Assume that a=1.22, b=3.34, c=2.28 and that three significant digits are used during calculation. The exact value of b^2^–4.a.c is 0.0292. However, b^2^ rounds to 11.2 and 4.a.c rounds to 11.1, giving a final answer of 0.1. This example demonstrates _catastrophic cancellation_. The subtraction does not cause an error, but it does expose errors introduced in the multiplications that are used when generating its operands.


== Resources

* MISRA {cpp}:2008, 0-4-2
* Goldberg D., What Every Computer Scientist Should Know about Floating-Point Arithmetic, Computing Surveys, March 1991.


ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

=== is related to: S861

=== on 20 Oct 2014, 13:51:05 Samuel Mercier wrote:
This one cannot be implemented as a regular issue.


We could raise an issue on the project's level as soon as we detect some arithmetic using floating point exceptions, but probably not much more.

=== on 20 Oct 2014, 14:31:16 Ann Campbell wrote:
\[~fabrice.bellingard] this would make a good "Finding"

cc [~freddy.mallet]

=== on 20 Oct 2014, 14:35:55 Ann Campbell wrote:
\[~samuel.mercier] you've included an endnote reference (the "[13]") but not it's value. 

=== on 6 Sep 2019, 10:57:02 Loïc Joly wrote:
Not implementable, closing this issue.

=== on 6 Sep 2019, 13:39:52 Ann Campbell wrote:
\[~loic.joly] how is this not implementable? We raise an issue on "you're adding two floats here" and it's... _tracked!_ Users can then use comments to log their justifications.

=== on 9 Sep 2019, 12:01:24 Loïc Joly wrote:
This is a MISRA rule, and the point of the MISRA rule is not to track all operations that use floating point operation. It means that, as soon as there is one floating point operation, and only once, there should be a document, probably external to the source code, that specifies :

____
this rule requires that documentation be produced to demonstrate that all the issues have been covered by the implementation.
____


This is not the kind of document than can be handled by comments in the justifications.


If instead of following MISRA, you would like to define a SonarSource rule to track all usage of floating point, then of course it would be possible, I just don't see what could be the value of such a rule...

endif::env-github,rspecator-view[]
