Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Math processing
Expertise: Beginner
Apr 5, 1999



Application Security Testing: An Integral Part of DevOps

Floating point accuracy

When I do the following,
double answer = 121 * .1;
System.out.println("Answer: " + answer);
I get,
Answer: 12.100000000000001
What's going on?

Almost every computer architecture represents numbers in binary format, also known as base 2. Java's virtual machine is no exception and follows the IEEE binary floating point standard. It is not possible to represent exactly all floating point numbers in base 2 (or base 10, for that matter). The base 10 decimal number 0.1 is one such number. You should assume that every floating point operation will contain some small error and account for it by comparing numbers with a tolerance value representing the amount of error you are willing to accept.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date