···11---
22title: Basic Answers
33+description: Concise answers to common basic questions about floating-point math, like "Why don't my numbers add up?"
34---
4556### Why don't my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?
+1
content/errors/comparison.html
···11---
22title: Comparison
33+description: Explanation of the various pitfalls in comparing floating-point numbers.
34---
4556Due to rounding errors, most [floating-point](/formats/fp/) numbers end up being slightly imprecise. As long as this
+1
content/errors/propagation.html
···11---
22title: Error Propagation
33+description: Explanations about propagation of errors in floating-point math.
34---
4556While the errors in single [floating-point numbers](/formats/fp/) are very small, even simple calculations on them
+1
content/errors/rounding.html
···11---
22title: Rounding Errors
33+description: Explanation of the reasons for rounding errors in floating-point math, and of rounding modes.
34---
4556Because [floating-point numbers](/formats/fp/) have a limited number of digits, they cannot represent all
+1
content/formats/binary.html
···11---
22title: Binary Fractions
33+description: In-depth explanation of how binary fractions work, what problems the cause and why they are used anyway
34---
4556How they work
+1
content/formats/exact.html
···11---
22title: Exact Types
33+description: Description of various datatypes that can be more exact that floating-point numbers
34---
4556While [binary](/formats/binary/) [floating-point](/formats/fp/) numbers are better for computers to work with, and usually good enough for humans, sometimes they are just not appropriate. Sometimes, the numbers really must add up to the last bit, and no technical excuses are acceptable - usually when the calculations involve money.
+1
content/formats/fp.html
···11---
22title: Floating Point Numbers
33+description: Explanation of how floating-points numbers work and what they are good for
34---
4556Why floating-point numbers are needed
+1
content/formats/integer.html
···11---
22title: On Using Integers
33+description: Explanation why using integers to avoid floating-point problems by having them represent e.g. cents is not a good solution.
34---
4556While integer types are usually binary and by definition do not support fractions, they are exact (no [rounding errors](/errors/rounding/) when converting from decimal integers) and can be used as a sort of "poor man's [decimal type](/formats/exact/)" by choosing an implicit fixed decimal point so that the smallest unit you work with can be represented as the integer 1. In practical terms, this is often put as: **"To handle money, store and calculate everything in cents and format only the output"**.
+2
content/index.html
···11---
22title: What Every Programmer Should Know About Floating-Point Arithmetic
33+description: Aims to provide both short and simple answers to the common recurring questions of novice programmers about floating-point numbers not 'adding up' correctly, and more in-depth information about how IEEE 754 floats work, when and how to use them correctly, and what to use instead when they are not appropriate.
44+verification: SoYmbsJEmSz2s1LmZk_cku4pKwhRsU6m0ZOTgGdnTL0
35---
4657or
+1
content/languages/csharp.html
···11---
22title: How to deal with floating-point numbers in C#
33+description: Tips for using floating-point numbers in C#
34---
4556Floating-Point Types
+2-1
content/languages/java.html
···11---
22title: How to deal with floating-point numbers in Java
33+description: Tips for using floating-point numbers in Java
34---
4556Floating-Point Types
···1112Decimal Types
1213-------------
1314Java has an [arbitrary-precision](/formats/exact/) decimal type named <code>java.math.BigDecimal</code>, which
1414-also allows to choose the rounding mode:
1515+also allows to choose the rounding mode.
1516 BigDecimal a = new BigDecimal("0.1");
1617 BigDecimal b = new BigDecimal("0.2");
1718 BigDecimal c = a.add(b); // returns a BigDecimal representing exactly 0.3
+1
content/languages/javascript.html
···11---
22title: How to deal with floating-point numbers in JavaScript
33+description: Tips for using floating-point numbers in JavaScript
34---
4556Floating-Point Types
+1
content/languages/php.html
···11---
22title: How to deal with floating-point numbers in PHP
33+description: Tips for using floating-point numbers in PHP
34---
4556Floating-Point Types
+1
content/references.html
···11---
22title: References
33+description: Documents with more in-depth information about floating-point math
34---
4556Documents that contain more in-depth information about the topics
+2-1
content/xkcd.html
···11---
22title: xkcd
33+description: How to mess with people who've learned to *expect* rounding errors in floating-point math.
34---
4556or
67--
7888-How to mess with people who've learned to *expect* rounding errors in floating-point math.
99+<%= @item[:description] %>
910====================
10111112