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 comes with some degree of inaccuracy. That's why, more often than not, .1 + .2 != .3.
Why does this happen?
It's actually pretty simple. 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, and 1/7 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 express fractions cleanly which only contain 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 is operating in. When you do 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 number.
Below are some examples of sending .1 + .2 to standard output in a variety of languages.
read more: | wikipedia | IEEE 754 | Stack Overflow | What Every Computer Scientist Should Know About Floating-Point Arithmetic
| Language | Code | Result |
|---|---|---|
| ABAP |
And
|
0.30000000000000004 And 0.3 |
| Ada |
|
3.00000E-01 |
| APL |
|
0.30000000000000004 |
| AutoHotkey |
|
0.300000 |
| awk |
|
0.3 |
| bc |
|
0.3 |
| C |
|
0.30000000000000004 |
| Clojure |
|
0.30000000000000004 |
|
Clojure supports arbitrary precision and ratios. |
||
| ColdFusion |
|
0.3 |
| Common Lisp |
And
And
And
|
0.3 And 3/10 And 0.30000000000000004d0 And 0.20000005 |
|
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. |
||
| C++ |
|
0.30000000000000004 |
| Crystal |
And
|
0.30000000000000004 And 0.3 |
| C# |
And
|
0.30000000000000004 And 0.3 |
|
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 |
||
| D |
|
0.29999999999999999 |
| Dart |
|
0.30000000000000004 |
| dc |
|
.3 |
| Delphi XE5 |
|
3.00000000000000E-0001 |
| Elixir |
|
0.30000000000000004 |
| Elm |
|
0.30000000000000004 |
| elvish |
|
0.30000000000000004 |
|
elvish uses Go’s |
||
| Emacs Lisp |
|
0.30000000000000004 |
| Erlang |
|
0.30000000000000004 |
| FORTRAN |
|
0.300000012 |
| Gforth |
|
0.3 |
| GHC (Haskell) |
And
|
And
|
|
Haskell supports rational numbers. To get the math right, |
||
| Go |
|
0.3 |
| Groovy |
|
0.3 |
|
Literal decimal values in Groovy are instances of java.math.BigDecimal |
||
| Hugs (Haskell) |
|
0.3 |
| Io |
|
0.3 |
| Java |
And
|
0.30000000000000004 And 0.3 |
|
Java has built-in support for arbitrary precision numbers using the BigDecimal class. |
||
| JavaScript |
|
0.30000000000000004 |
|
The decimal.js library provides an arbitrary-precision Decimal type for JavaScript. |
||
| Julia |
|
0.30000000000000004 |
|
Julia has built-in rational numbers support and also a built-in arbitrary-precision BigFloat data type. To get the math right, |
||
| K (Kona) |
|
0.3 |
| Lua |
And
|
0.3 And 0.30000000000000004 |
| Mathematica |
|
0.3 |
|
Mathematica has a fairly thorough internal mechanism for dealing with numerical precision and supports arbitrary precision. |
||
| Matlab |
And
|
0.3 And 0.30000000000000004 |
| MySQL |
|
0.3 |
| Nim |
|
0.3 |
| Objective-C |
|
0.30000000000000004 |
| OCaml |
|
float = 0.300000000000000044 |
| Perl 5 |
And
|
0.3 And 0.30000000000000004 |
| Perl 6 |
And
And
And
|
0.3 And 0.3 And 0.3 And 0.30000000000000004 |
|
Perl 6, unlike Perl 5, uses rationals by default, so .1 is stored something like { numerator => 1, denominator => 10 }. To actually trigger the behavior, you must force the numbers to be of type Num (double in C terms) and use the base function instead of the sprintf or fmt functions (since those functions have a bug that limits the precision of the output). |
||
| PHP |
|
0.3 float(0.30000000000000004441) |
|
PHP |
||
| PicoLisp |
And
|
(/ 3 10) |
|
You must load file “frac.min.l”. |
||
| Postgres |
|
0.3 |
| Powershell |
|
0.3 |
| Prolog (SWI-Prolog) |
|
X = 0.30000000000000004. |
| Pyret |
And
|
0.3 And ~0.30000000000000004 |
|
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 with a |
||
| Python 2 |
And
And
And
|
0.3 And 0.30000000000000004 And 0.3 And 0.3 |
|
Python 2’s “print” statement converts 0.30000000000000004 to a string and shortens it to “0.3”. To achieve the desired floating point result, use print(repr(.1 + .2)). This was fixed in Python 3 (see below). |
||
| Python 3 |
And
And
And
|
0.30000000000000004 And 0.30000000000000004 And 0.3 And 0.3 |
|
Python (both 2 and 3) supports decimal arithmetic with the decimal module, and true rational numbers with the fractions module. |
||
| R |
And
|
0.3 And 0.30000000000000004 |
| Racket (PLT Scheme) |
And
|
0.30000000000000004 And 3/10 |
| Ruby |
And
|
0.30000000000000004 And 3/10 |
|
Ruby supports rational numbers in syntax with version 2.1 and newer directly. For older versions use Rational. |
||
| Rust |
|
0.30000000000000004 And 1/10 + 2/10 = 3/10 |
|
Rust has rational number support from the num crate. |
||
| SageMath |
And
And
And
|
0.3 And 0.30000000000000004 And [“0.300000000000000 +/- 1.64e-16”] And 3/10 |
|
SageMath supports various fields for arithmetic: Arbitrary Precision Real Numbers, RealDoubleField, Ball Arithmetic, Rational Numbers, etc. |
||
| scala |
And
And
|
0.30000000000000004 And 0.3 And 0.3 |
| Smalltalk |
|
0.30000000000000004 |
| Swift |
And
|
0.3 And 0.30000000000000004 |
| TCL |
|
0.30000000000000004 |
| Turbo Pascal 7.0 |
|
3.0000000000E-01 |
| Vala |
|
0.30000000000000004 |
| Visual Basic 6 |
|
0.0000000000000001 |
|
Appending the identifier type character |
||
| WebAssembly (WAST) |
And
|
0.30000001192092896 And 0.30000000000000004 |
|
https://webassembly.studio/?f=r739k6d6q4t |
||
| zsh |
|
0.30000000000000004 |
I am Erik Wiffin. You can contact me at: erik.wiffin.com or erik.wiffin@gmail.com.
This project is on github. If you think this page could be improved, send me a pull request.
Floating Point Math的更多相关文章
- Brief Tour of the Standard Library
10.1. Operating System Interface The os module provides dozens of functions for interacting with the ...
- Awk by Example--转载
原文地址: http://www.funtoo.org/Awk_by_Example,_Part_1?ref=dzone http://www.funtoo.org/Awk_by_Example,_P ...
- C++ QUICK REFERENCE
C++ string 用法详解 字符串分割(C++) C++ QUICK REFERENCE Matt Mahoney, mmahoney@cs.fit.edu DECLARATIONS enum ...
- net programming guid
Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hallbeej ...
- VS2012下基于Glut 矩阵变换示例程序:
也可以使用我们自己的矩阵运算来实现OpenGL下的glTranslatef相应的旋转变换.需要注意的是OpenGL下的矩阵是列优先存储的. 示例通过矩阵运算使得圆柱或者甜圈自动绕Y轴旋转,可以单击鼠标 ...
- [Python] 01 - Number
故事背景 一.大纲 如下,chapter4 是个概览,之后才是具体讲解. 二. 编译过程 Ref: http://www.dsf.unica.it/~fiore/LearningPython.pdf
- JavaScript-Tool:Moment.js
ylbtech-JavaScript-Tool:Moment.js Parse, validate, manipulate, and display dates and times in JavaSc ...
- Delphi 64与32位的差异
Delphi 64与32位的差异 最近,Delphi推出了64位预览版本, 我做为一个忠实的Delphier, 看到这消息后,第一时间学习,并写下这个做为以后的参考资料. 相同点: 在Delphi ...
- [Python] 01 - Number and Matrix
故事背景 一.大纲 如下,chapter4 是个概览,之后才是具体讲解. 二. 编译过程 Ref: http://www.dsf.unica.it/~fiore/LearningPython.pdf
随机推荐
- 【6.24校内test】T3 棠梨煎雪
[题目背景] 岁岁花藻檐下共将棠梨煎雪. 自总角至你我某日辗转天边. 天淡天青,宿雨沾襟. 一年一会信笺却只见寥寥数言. ——银临<棠梨煎雪> [问题描述] 扶苏正在听<棠梨煎雪&g ...
- 洛谷 P1472 奶牛家谱 Cow Pedigrees 题解
题面 这道题我觉得是个不错的题: 根据题意可以较清晰的发现ans只和n和k有关:(因为输入的只有这两个数啊~): 那么设f[i][j]表示前i层用了j个节点的方案数,g[i][j]表示深度小于等于i并 ...
- 看电视剧<潜伏>有感
前几天看了老电视剧-潜伏,有一些感慨. 一,立场和真相都不重要,形式才是最重要的. 二.历史在不断的轮回中. 好汉历经千辛万苦杀掉了为害一方的恶霸,好汉的威望达到了顶峰,自然的成了村庄的守护者和掌控者 ...
- MyBatis的Insert操作详解
一.前言 数据库操作怎能少了INSERT操作呢?下面记录MyBatis关于INSERT操作的笔记,以便日后查阅. 二. insert元素 属性详解 其属性如下: parameterType ,入参的全 ...
- [Vue] vuex-interview
1.你有使用过 vuex 的 module 吗?主要是在什么场景下使用? 把状态全部集中在状态树上,非常难以维护. 按模块分成多个 module,状态树延伸多个分支,模块的状态内聚,主枝干放全局共享状 ...
- Elasticsearch入门教程(一):Elasticsearch及插件安装
原文:Elasticsearch入门教程(一):Elasticsearch及插件安装 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:h ...
- flume复习(一)
关于flume官方文档介绍可以去:http://flume.apache.org/看看.接下来就介绍一下关于我个人对flume的理解 一.flume介绍: 1.flume是一个分布式.可靠.和高可用的 ...
- flutter-dart语言初识
dart 官方文档 http://dart.goodev.org/guides/language/language-tour# 重要概念所以能够使用变量引用的都是对象,也就是所以可以赋值给变量的都是对 ...
- python脚本实现向钉钉群组发送消息
一.json格式 import json import requests def sendmessage(message): url = 'https://oapi.dingtalk.com/robo ...
- windows设置通过NFS连接到Linux共享数据
win7下增加了很多有用的功能,只是默认没有开启而已,今天简述下一个WIN7下的NFS功能,通过这个功能,可以让win7共享Linux下面的磁盘分区或者目录数据,这个功能原理只能通过samba或者ft ...