Floating Point Math
source link: https://0.30000000000000004.com/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Floating Point Math
Floating Point Math
Your language isnโt broken, itโs doing floating point math. Computers can only
natively store integers, so they need some way of representing decimal numbers.
This representation is not perfectly accurate. This is why, more often than not,
0.1 + 0.2 != 0.3
.
Why does this happen?
Itโs actually rather interesting. When you have a base-10 system (like ours), it can only express fractions that use a prime factor of the base. The prime factors of 10 are 2 and 5. So 1/2, 1/4, 1/5, 1/8, and 1/10 can all be expressed cleanly because the denominators all use prime factors of 10. In contrast, 1/3, 1/6, 1/7 and 1/9 are all repeating decimals because their denominators use a prime factor of 3 or 7.
In binary (or base-2), the only prime factor is 2, so you can only cleanly express fractions whose denominator has only 2 as a prime factor. In binary, 1/2, 1/4, 1/8 would all be expressed cleanly as decimals, while 1/5 or 1/10 would be repeating decimals. So 0.1 and 0.2 (1/10 and 1/5), while clean decimals in a base-10 system, are repeating decimals in the base-2 system the computer uses. When you perform math on these repeating decimals, you end up with leftovers which carry over when you convert the computerโs base-2 (binary) number into a more human-readable base-10 representation.
Below are some examples of sending .1 + .2
to standard output in a variety of
languages.
Read more:
Language | Code | Result |
---|---|---|
๐ |
||
PowerShell by default uses double type, but because it runs on .NET it has the
same types as C# does. Thanks to that the Decimal type can be used -
directly by providing the type name More about that in the C# section. |
||
๐ ABAP |
||
|
|
|
๐ APL |
||
|
|
|
APL has a default printing precision of 10 significant digits. Setting |
||
๐ Ada |
||
|
|
|
๐ AutoHotkey |
||
|
|
|
๐ C |
||
|
|
|
๐ C# |
||
|
|
|
C# has support for 128-bit decimal numbers, with 28-29 significant digits
of precision. Their range, however, is smaller than that of both the single and
double precision floating point types. Decimal literals are denoted with the |
||
๐ C++ |
||
|
|
|
๐ Clojure |
||
|
|
|
Clojure supports arbitrary precision and ratios. |
||
๐ ColdFusion |
||
|
|
|
๐ Common Lisp |
||
|
|
|
CLโs spec doesnโt actually even require radix-2 floats (let alone specifically 32-bit singles and 64-bit doubles), but the high-performance implementations all seem to use IEEE floats with the usual sizes. This was tested on SBCL and ECL in particular. |
||
๐ Crystal |
||
|
|
|
๐ D |
||
|
|
|
๐ Dart |
||
|
|
|
๐ Delphi XE5 |
||
|
|
|
๐ Elixir |
||
|
|
|
๐ Elm |
||
|
|
|
๐ Elvish |
||
|
|
|
Elvish uses Goโs |
||
๐ Emacs Lisp |
||
|
|
|
๐ Erlang |
||
|
|
|
๐ FORTRAN |
||
|
|
|
๐ Fish |
||
|
|
|
๐ GHC (Haskell) |
||
|
|
|
If you need real numbers, packages like exact-real give you the correct answer. |
||
๐ GNU Octave |
||
|
|
|
๐ Gforth |
||
|
|
|
In Gforth |
||
๐ Go |
||
|
|
|
Go numeric constants have arbitrary precision. |
||
๐ Groovy |
||
|
|
|
Literal decimal values in Groovy are instances of java.math.BigDecimal. |
||
๐ Guile |
||
|
|
|
๐ Hugs (Haskell) |
||
|
|
|
๐ Io |
||
|
|
|
๐ Java |
||
|
|
|
Java has built-in support for arbitrary-precision numbers using the BigDecimal class. |
||
๐ JavaScript |
||
|
|
|
The decimal.js library provides an arbitrary-precision Decimal type for JavaScript. |
||
๐ Julia |
||
|
|
|
Julia has built-in rational numbers support and also a built-in
arbitrary-precision BigFloat data type. To get the math right, |
||
๐ K (Kona) |
||
|
|
|
๐ Kotlin |
||
|
|
|
๐ Lua |
||
|
|
|
๐ MATLAB |
||
|
|
|
๐ MIT/GNU Scheme |
||
|
|
|
The scheme specification has a concept exactness. |
||
๐ Mathematica |
||
|
|
|
Mathematica has a fairly thorough internal mechanism for dealing with numerical precision and supports arbitrary precision. By default, the inputs Mathematica supports rational numbers: |
||
๐ MySQL |
||
|
|
|
๐ Nim |
||
|
|
|
๐ OCaml |
||
|
|
|
๐ Objective-C |
||
|
|
|
๐ PHP |
||
|
|
|
PHP |
||
๐ Perl |
||
|
|
|
The addition of float primitives only appears to print correctly because not all of the 17 digits are printed by default. The core Math::BigFloat allows true arbitrary precision floating point operations by never using numeric primitives. |
||
๐ PicoLisp |
||
|
|
|
You must load file โfrac.min.lโ. |
||
๐ PostgreSQL |
||
|
|
|
PostgreSQL treats decimal literals as arbitrary precision numbers with fixed point. Explicit type casts are required to get floating-point numbers. PostgreSQL 11 and earlier outputs In PostgreSQL 12 default behavior for textual output of floats was changed from more human-readable rounded format to shortest-precise format. Format can be customized by the |
||
๐ Prolog (SWI-Prolog) |
||
|
|
|
๐ Pyret |
||
|
|
|
Pyret has built-in support for both rational numbers and floating points.
Numbers written normally are assumed to be exact. In contrast, RoughNums are
represented by floating points, and are written prefixed with a |
||
๐ Python 2 |
||
|
|
|
Python 2โs |
||
๐ Python 3 |
||
|
|
|
Python (both 2 and 3) supports decimal arithmetic with the decimal module, and true rational numbers with the fractions module. |
||
๐ R |
||
|
|
|
๐ Racket (PLT Scheme) |
||
|
|
|
๐ Raku |
||
|
|
|
Raku uses rationals by default, so |
||
๐ Regina REXX |
||
|
|
|
๐ Ruby |
||
|
|
|
Ruby supports rational numbers in syntax with version 2.1 and newer directly. For older versions use Rational. Ruby also has a library specifically for decimals: BigDecimal. |
||
๐ Rust |
||
|
|
|
Rust has rational number support from the num crate. |
||
๐ SageMath |
||
|
|
|
SageMath supports various fields for arithmetic: Arbitrary Precision Real Numbers, RealDoubleField, Ball Arithmetic, Rational Numbers, etc. |
||
๐ Scala |
||
|
|
|
๐ Smalltalk |
||
|
|
|
Smalltalk uses fractions by default in most operations; in
fact, standard devision results in fractions, not floating
point numbers. Squeak and similar Smalltalks provide โscaled
decimalsโ that allow fixed-point real numbers ( |
||
๐ Swift |
||
|
|
|
Swift supports decimal arithmetic with the Foundation module. |
||
๐ TCL |
||
|
|
|
๐ Turbo Pascal 7.0 |
||
|
|
|
๐ Vala |
||
|
|
|
๐ Visual Basic 6 |
||
|
|
|
Appending the identifier type character |
||
๐ WebAssembly (WAST) |
||
|
|
|
See demo. |
||
๐ awk |
||
|
|
|
๐ bc |
||
|
|
|
๐ dc |
||
|
|
|
๐ zsh |
||
|
|
I am Erik Wiffin. You can contact me at: erik.wiffin.com or [email protected].
This project is on GitHub. If you think this page could be improved, send me a pull request.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK