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 base10 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 base2), 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 base10 system, are repeating decimals in the base2 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 base2 (binary) number into a more humanreadable base10 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 128bit decimal numbers, with 2829 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 radix2 floats (let alone specifically 32bit singles and 64bit doubles), but the highperformance 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 exactreal 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 builtin support for arbitraryprecision numbers using the BigDecimal class. 

🔗 JavaScript 




The decimal.js library provides an arbitraryprecision Decimal type for JavaScript. 

🔗 Julia 




Julia has builtin rational numbers support and also a builtin
arbitraryprecision 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 




🔗 ObjectiveC 




🔗 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 floatingpoint numbers. PostgreSQL 11 and earlier outputs In PostgreSQL 12 default behavior for textual output of floats was changed from more humanreadable rounded format to shortestprecise format. Format can be customized by the 

🔗 Prolog (SWIProlog) 




🔗 Pyret 




Pyret has builtin 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 fixedpoint 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