Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by sciSpider Java v3.0

...

Code Block
bgColor#FFcccc
double dollar = 1.0;
double dime = 0.1;
int number = 7;
System.out.println (""A dollar less "" + number + "" dimes is $"" +
		    (dollar - number * dime) );

...

Code Block
bgColor#ccccff
long dollar = 100;
long dime = 10;
int number = 7;
System.out.println (""A dollar less "" + number + "" dimes is "" +
		    (dollar - number * dime) + "" cents"" );

This code outputs: A dollar less 7 dimes is 30 cents

...

Code Block
bgColor#ccccff
import java.math.BigDecimal;

BigDecimal dollar = new BigDecimal(""1.0"");
BigDecimal dime = new BigDecimal(""0.1"");
int number = 7;
System.out.println (""A dollar less "" + number + "" dimes is $"" +
	(dollar.subtract(new BigDecimal(number).multiply(dime) )) );

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Other Languages

This rule appears in the C Secure Coding Standard as FLP02-C. Avoid using floating point numbers when precise computation is needed.

...

Wiki Markup
\[[JLS 05|AA. Java References#JLS 05]\] [Section 4.2.3, Floating-Point Types, Formats, and Values|http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3]
\[[Bloch 08|AA. Java References#Bloch 08]\] Item 48: Avoid {{float}} and {{double}} if exact answers are required
\[[Bloch 05|AA. Java References#Bloch 05]\] Puzzle 2: Time for a Change
\[[Goldberg 91|AA. Java References#Goldberg 91]\]

...

07. Floating Point (FLP)            07. Floating Point (FLP)            FLP01-J. Take care in rearranging floating point expressions