Coder Social home page Coder Social logo

0.30000000000000004's Introduction

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 has a degree of inaccuracy which 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, 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 uses. 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 representation.

Below are some examples of sending .1 + .2 to standard output in a variety of languages.

0.30000000000000004's People

Contributors

0racle avatar abrudz avatar aconchillo avatar amirtcho avatar anirbanmu avatar ash avatar autarch avatar bjsemrad avatar blieque avatar brackendev avatar braincompiler avatar brehberg avatar cncolder avatar cowens avatar dependabot[bot] avatar dkordic avatar envek avatar erikwiffin avatar ihid avatar ircmaxell avatar itaisteinherz avatar lazymofo avatar lengau avatar linuxbckp avatar polymeris avatar sixlettervariables avatar sre42 avatar stefanedwards avatar still-learning avatar vendethiel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

0.30000000000000004's Issues

Project description and link

The link at the end of the README could be added to the repository metadata fields at the top of the page, where it would be more accessible.

A description can also be added there, which would appear in the page title in browser tabs, in search results, etc.

Link to published website

Is this repo published somewhere? It would be a good idea to include a link to the published website in this repos README.md

add equality test

I would recommend adding another test that shows whether

0.1+0.2==0.3

in the various languages. One or two will get it right, but most do not, even the ones that print 0.3 when you do the addition.

Font size is too small

12px is much too small. I recommend leaving it at the default (normally 16px), and defining most other things in terms of ems or rems instead of pixels, and using max-width: 100% so that the greater width isn’t causing overflow trouble unnecessarily. (Mostly you could instead change widths to max-widths, but some bits may need to be told to be as wide as possible within that.)

awk example is deceptive

$ awk --version | head -1
GNU Awk 5.1.1, API: 3.1 (GNU MPFR 4.1.0-p13, GNU MP 6.2.1)

The example indeed prints 0.3, but 0.1 + 0.2 != 0.3:

$ awk 'BEGIN { print 0.1 + 0.2 }'
0.3
$ awk 'BEGIN { print (0.1 + 0.2) == 0.3 }'
0

It's because

$ awk 'BEGIN { printf "%0.25f", 0.1 + 0.2 }'
0.3000000000000000444089210
$ awk 'BEGIN { printf "%0.25f", 0.3 }'
0.2999999999999999888977698

Some more database info

A bug I filed about IEEE 754 documentation a while ago for MySQL:
https://bugs.mysql.com/bug.php?id=57519

Support for NaN differs between MySQL and PostgreSQL. I didn't see any information about the 'special' values on http://0.30000000000000004.com

Note that there are different behaviors within one database system:
http://sqlfiddle.com/#!6/12e70/3

CREATE TABLE t1 (f FLOAT);
SELECT 0.1 + 0.2;
INSERT INTO t1 VALUES(0.1),(0.2);
SELECT SUM(f) FROM t1;

With Microsoft SQL Server 2014 this has the following output:

0.3
0.30000000000000004

With MySQL 5.6:

0.3
0.30000000447034836

http://sqlfiddle.com/#!9/2e75e/1

Oracle 11gR2 returns 0.3 in both cases:
http://sqlfiddle.com/#!4/0509e6/4

Note that Oracle has FLOAT and BINARY_FLOAT
http://sqlfiddle.com/#!4/cf223/1

Add GNU Octave Code and Results

The results are partially different with which in Matlab.

File:


layout: post
title: 'GNU Octave'
code:

  • 0.1 + 0.2
  • single(0.1)+single(0.2)
  • double(0.1)+double(0.2)
  • 0.1+single(0.2)
  • 0.1+double(0.2)
  • "sprintf('%.17f',0.1+0.2)"
    result:
  • 0.30000
  • 0.30000
  • 0.30000
  • 0.30000
  • 0.30000
  • 0.30000000000000004

Additionally, I met an error-403 while "git push". Could you give me the push permission?

Note on GHC

Using Double triggers the Problem in GHC, Float works fine (could be a precision reason, though):

Prelude> 0.2 + 0.1 :: Double
0.30000000000000004
Prelude> 0.2 + 0.1 :: Float 
0.3

Enforce HTTPS

Hi there, GitHub has an option to redirect the traffic from HTTP to HTTPS, it's in the project settings under the GitHub Pages section

image

image

Clojure improvements

;; Bigdecimal support
(+ 0.1M 0.2M)
; => 0.3

;; Ratios
(+ 1/10 2/10)
;=> 3/10

Proposal: move this to gh-pages

You could leverage gh-pages build to host this site and it would work with custom domain as well.
Or you could just push to gh-pages branch an have http://erikwiffin.github.io/0.30000000000000004 server as backup mirror.
If you consider switching to gh-pages with domain name, you can find instruction

DAX example

There are not that many examples of this in Data Analysis Expressions (DAX),

It would be:

EVALUATE { VAR _sum = 0.1 + 0.2 // 0.30000000000000004 RETURN IF( _sum = 0.3, ":)", ":(" ) }

EVALUATE { VAR _sum = CURRENCY( 0.1 ) + CURRENCY( 0.2 ) // 0.3 RETURN IF( _sum = 0.3, ":)", ":(" ) }

Use of the word `decimals` is confusing

Use of the word decimals is confusing. For example:

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

Here the first-use as decimals is correct, meaning when converted to decimal and written out, but the second-use is confusing; I think it means "digits after the binary-point". I don't think "decimals" is a good word to mean "digits after the binary-point".

I don't have a better word, just found it confusing. Please let me know if I got it all wrong. Awesome explanation though - love it :)

Allowed to Mirror?

Hey Erik Wiffin. Thanks for your initiative to build this site. Very helpful to introduce people about floating point.

Yet, the main site: http://0.30000000000000004.com/ seems down right now so i can't refer people to this site. Currently, there is no way to access this website other than from GH repo and archive.is. Could i mirror this? I don't see LICENSE file so i ask about this.

Also related to #105

The PHP example wrongly suggests PHP acts properly

Your echo .1 + .2; displays 0.3 because PHP converts it to a string in echo first while still internally using 0.30000000000000004. You can verify this with: echo ceil((.1 + .2) * 10); (this prints 4). My suggestion is to run a similar test for all languages and displaying and (if needed) explaining the results.

[C++] setprecision() uses the std namespace

The C++ example is as follows:

std::cout << setprecision(17) << 0.1 + 0.2 << std.endl;

The function setprecision() uses the std namespace, and should therefore be written as such. Same goes for endl. Furthermore, the C example also works in C++; perhaps these should be merged?

objective-c gone

objective-c gone from the list of languages on website after updating examples.

erlang examples

1> io:format("~f~n", [0.1 + 0.2]). 
0.300000
ok
2> io:format("~e~n", [0.1 + 0.2]).
3.00000e-1

Raku output is truncated

Although the markdown source for Raku shows the correct output to raku -e 'say (0.1 + 0.2).fmt("%.17f")' is 0.30000000000000000, on the published website just shows the output as 0.3.

MYSQL FLOAT

Perhaps this example could explain.

CREATE TABLE test(fla FLOAT,flb FLOAT,dba DOUBLE(10,2),dbb DOUBLE(10,2));
We have a table like this:

+-------+-------------+
| Field | Type |
+-------+-------------+
| fla | float |
| flb | float |
| dba | double(10,2)|
| dbb | double(10,2)|
+-------+-------------+
For first difference, we try to insert a record with '1.2' to each field:

INSERT INTO test values (1.2,1.2,1.2,1.2);
The table showing like this:

SELECT * FROM test;

+------+------+------+------+
| fla | flb | dba | dbb |
+------+------+------+------+
| 1.2 | 1.2 | 1.20 | 1.20 |
+------+------+------+------+
See the difference?

We try to next example:

SELECT fla+flb, dba+dbb FROM test;
Hola! We can find the difference like this:

+--------------------+---------+
| fla+flb | dba+dbb |
+--------------------+---------+
| 2.4000000953674316 | 2.40 |
+--------------------+---------+

Source: https://stackoverflow.com/a/28511784/676479

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.