[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
>his programming language can't add up 0.2+0.1
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 118
Thread images: 14
File: 0.30000000000000004.png (54 KB, 749x434) Image search: [Google]
0.30000000000000004.png
54 KB, 749x434
>his programming language can't add up
0.2+0.1
>>
>>54687006
>his programming language doesn't follow the IEEE floating point standard
>>
~ $ bpython3
bpython version 0.15 on top of Python 3.5.1+ /usr/bin/python3
>>> 0.2+0.1
0.30000000000000004

well
>>
>>54687194
What's up with the extra 0.00000000000000004?
>>
>>54687194
further

~ $ perl
print(0.2+0.1)
0.3


 $ scala
Welcome to Scala version 2.11.6 (OpenJDK 64-Bit Server VM, Java 1.8.0_91).
Type in expressions to have them evaluated.
Type :help for more information.

scala> 0.2+0.1
res0: Double = 0.30000000000000004


~ $ php
<?php
print (0.2 + 0.1)
?>
0.3


non-repl languages need not apply
>>
>>54687006

print(0.2+0.1)

0.3
0.3
0.3
0.3


that's probably how it'll go
>>
>>54687260
 $ lua 
didn't specify what lang this was
>>
>>54687205
floats are not stored as BCD, that number is the closest approximation to 0.3 as you can get

In the other languages posted here, it's probably the case that their interpreter truncates past a certain decimal point, which rounds the result to 0.3
>>
>>54687289
Thanks.
>>
>>54687289
IEEE754 actually allows base 10 exponents, but usually base 2 is used
>>
>>54687006
>17.2/0.1
171.99999999999997
>>
php > $s = .2 + .1;
php > var_dump($s);
float(0.3)


php 7.0.5
>>
>>54687194
>>54687006
Python with decimal module
0.3000000000000000166533453694

kek.
>>
>>54687328
lmao

 perl -E 'say 17.2/0.1'
172
>>
> ITT people who do not understand the basic idea of storing floating point numbers
embarassing
>>
instead of printing this shit, you guys need to check if
(0.2 + 0.1) == (0.3)

your print statement is probably truncating
>>
>>54687006
>His programming language can't add numbers without performance issues
>>
>>54687822
CHICKEN
(c) 2008-2015, The CHICKEN Team
(c) 2000-2007, Felix L. Winkelmann
Version 4.10.0 (rev b259631)
linux-unix-gnu-x86-64 [ 64bit manyargs dload ptables ]
compiled 2015-08-04 on yves.more-magic.net (Linux)

#;1> (= .3 .3)
#t
#;2> (+ .1 .2)
0.3
#;3> (= .3 (+ .1 .2))
#f
>>
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
Prelude> 0.1 + 0.2
0.30000000000000004
>>
(+ 1/10 2/10)
3/10

>Using inexact numbers
>>
>>54687006
λ 0.2+0.1 :: Rational
3 % 10
λ 0.2+0.1 :: CReal
0.3
λ 0.2+0.1 :: Double
0.30000000000000004
λ 0.2+0.1 :: Fixed E6
0.300000
λ

numbers sure are hard
>>
File: file.png (14 KB, 618x236) Image search: [Google]
file.png
14 KB, 618x236
>>54687260
>>
>>54687902
>not caring about performance
>>
double left = 0.2d;
double right = 0.1d;

(left + right).Dump();
>> 0.3


C# btw
>>
File: 1458972550636.png (131 KB, 300x302) Image search: [Google]
1458972550636.png
131 KB, 300x302
>>54687959
WHAT?
>>
>>54688065
Same in python desu senpai
>>
>>54688065
>>54688119
>ITT: “programmers” who don't understand jackshit about how their computers work
>>
>>54688130
(You)
>>
>>54687959
Even if not exactly .3 shouldn't they have the same error and therefore be equal?
>>
>>54688170
No.
>>
File: wrong.jpg (29 KB, 396x400) Image search: [Google]
wrong.jpg
29 KB, 396x400
>>54687017
IEEE can't do anything about the fact that a 32 bit float can't represent .1 accurately.
>>
>>54688208
It is accurate though, it's just not exact
>>
Go

package main
import "fmt"
func main() {
fmt.Println(.1 + .2)
var a float64 = .1
var b float64 = .2
fmt.Println(a + b)
fmt.Printf("%.54f\n", .1 + .2)
}
/*
Outputs:
0.3
0.30000000000000004
0.299999999999999988897769753748434595763683319091796875
*/


Why does this happen?
>>
>>54688622
Its not a programming language fault. It's the way the processors are built to operate. And the compiler that translates the code to the processor. So basically its intel's fault you cant do that.
>>
>>54687822
>mfw

>>> (0.2 + 0.1) == (0.3)
False
>>
>>54688622
>Why does this happen?
The simplest way to explain it is that IEEE floats approximate fractions as a series of powers of two (since computers use a binary number system).

That's why they can encode numbers like 1/2 = 0.5, 1/4 = 0.25 or 1/8 = 0.125 in an exact way, but not 3/10 = 0.3.

λ 0.1 + 0.2 == 0.3
False
λ 0.5 + 0.125 == 0.625
True


Note that this is entirely analogous to how humans represent numbers: Except we do it in base 10 instead of base 2. For us, we can perfectly represent something like 3/10 = 0.3 but meanwhile we fail hilariously on something coprime to 10 like 1/3 = 0.3333333333....

Since computer floats have some “fixed length” representation, a base 10 computer would have to cut this off at something like 10 digits (0.3333333333). So you will get “wrong” results such as:

1/3 + 1/3 + 1/3 = 0.999999999 != 1.0

Same thing is happening for the 0.1 + 0.2 == 0.3 test, except we don't “see” the truncation happening because 0.1 and 0.2 happen to be represented cleanly in base 10 (as opposed to base 2).
>>
>>54688698
I knew it was the jews
>>
It isn't always the jews lads
>>
>>54687205
>>54687006
that extra 0.00000000000000004 is there just in case you need it
>>
>>54687959
>>54688065
>>54688170
I'll tell you a secret, PI == 3.141592 is false too
>>
>>54688926
WHAT
>>
>>54688933
pi is irrational, 3.141592 is rational
3.141592 == 3141592 / 1000000
>>
>>54688926
Also, 0.99999999.... (periodic 9) is equal to 1, true story
>>
>>54689058
not this meme again
>>
>>54689074
I didn't say anything wrong
>>
File: but thats wrong.png (133 KB, 396x400) Image search: [Google]
but thats wrong.png
133 KB, 396x400
>>54688208
>the background looks transparent
>it's just fucking blue
Why would you do this?
>>
File: ss_140517.png (140 KB, 416x350) Image search: [Google]
ss_140517.png
140 KB, 416x350
>>54689208
not like yours is much better
>>
>>54689208
>>54689276
>shitposting
>on /g/
>in 2016
>>
>>54689284
>tripfag
I shouldn't have trusted his lies
>>
File: why is life.gif (508 KB, 500x383) Image search: [Google]
why is life.gif
508 KB, 500x383
>>54689276
Yeah, my power level isn't high enough to undo that nonsense. Sorry.
If you can make a better copy, please go ahead.
>>
>>54687818
I don't. Please explain.
>>
>>54689719
>>54688782
>>
Mech engineer here. Is it truncation errors from the way the numbers are stored?
>>
Floating point can only accurately represent sums of fractions 1/2^n
>>
>>54690076
Only for small enough n
>>
>>54689809

>A floating-point number is a rational number, because it can be represented as one integer divided by another (...)
>Whether or not a rational number has a terminating expansion depends on the base. (...)
>For example, the decimal number 0.1 is not representable in binary floating-point of any finite precision; the exact binary representation would have a "1100" sequence continuing endlessly (...)


Source:
>https://en.wikipedia.org/wiki/Floating_point
>>
>>54688170
It's not using decimal. Your number is stored as binary and then read back to you in decimal up from its location in memory.
>>
>>54688155
He's right though.
>>
If you know you're going to need exactly 0.3, you should probably just store ten times the value you need but as an int.
>>
File: Capture.png (8 KB, 677x342) Image search: [Google]
Capture.png
8 KB, 677x342
>>
http://www.gnumeric.org/numerical-issues.html
>>
>>54687818
Why do you need a float to store a definite and limited value like 0.3? No float needed.
>>
it always amazes me how many non-programmers are on /g/. when is Hiroshima going make a consumer tech board so all the shitposters can move out?
>>
>>54687289
I'm confused. I thought floating points were just an integer with a point somewhere on it. Like, part of the memory is reserved for the numerical value, and another one for the position of the point.

What the fuck is it doing that would require it to "approximate" a number?
>>
>>54695283
You're thinking of fixed point.
>>
>>54695307
Alright, so what's a floating point then? And why in hell is still being used?
>>
a=$(bc<<<"0.1+0.2")
echo $a
.3


u mad actual-programming-language fags?
>>
>>54695406
echo $(bc<<<"0.1+0.2 == 0.3")
1

Shell scripting master race.
>>
>>54687205
http://0.30000000000000004.com/
>>
>>54695326
Floating point data types offer higher precision. Many processors have hardware dedicated specifically to make floating point work faster.
>>
int a = 1;
int b = 2;
int c = a + b;

printf("0.%i + 0.%i = 0.%i", a, b, c);
>>
>>54689208
that image predates yotsuba automatically putting the bgcolor on thumbnails with transparency
>>
>>54695466
>no BASIC
Ree desu senpai
>>
>>54695529
Got you covered anon. I just ran this in QBASIC in DOSBOX:

IF .1 + .2 = .3 THEN
PRINT "Yes"
ELSE
PRINT "No"
END IF


You'll never guess the results.

[spoiler]It's "No".[/spoiler]
>>
File: 1460221798244.jpg (316 KB, 768x1024) Image search: [Google]
1460221798244.jpg
316 KB, 768x1024
... and this is why you never use == to check for equality for floating point numbers. Thanks for the reminder!
>>
>>54695613
What should we do if we must absolutely check that two floating points are equal?
>>
>>54695639
Use a range then. Is your brain that small?
>>
>>54695613

I alway compare like this:

include RuleOfThumb

int a = 1;
int b = 2;
RuleOfThumb rlOfTh = new RuleOfThumb();

System.out.println( rlOfTh(a+b) ); // output: "about '0.3', I guess?"
>>
>>54695639
fabs(a - b) < epsilon
>>
>>54695692
I hope this is a prank
>>
>>54695686
I'd ask you to specify but you visibly have a personality disorder making you unable to be helpful.

>>54695697
That sounds arbitrary and error-prone.
>>
>>54695733
>Refuses to think
>Gets mad
>>
>>54695639
>>54695697
I wrote a program with a lot of intense calculations that was supposed to neglect duplicates. I typecasted
(long double)
to
(double)
in the comparison expression. I figured that would truncate any last-few-bits weirdness. I don't know if this is correct but werks on my machine.
>>
>>54695827
if you just need some limited number of digits of precision (e.g. if you're calculating dollars and cents in a transactional setting and NOT in something that involves interest or whatever), you could just work in pennies and use integers.

basically every programming language is crazy good at working with integers compared to floats and other stuff.
>>
>>54687961
>not building hardware to handle inexact numbers
>>
File: tfw decimal classs.png (16 KB, 836x420) Image search: [Google]
tfw decimal classs.png
16 KB, 836x420
>all these programming language that don't have a decimal class

Once and for all, C# is master race again.
>>
>>54687006
the only way you can accurately represent that is using a binary coded decimal abstraction.
>>
>>54695890
And who uses that language?
>>
>>54695890
>decimal

congrats you have a BCD type

Python 3.5.1+ (default, May  9 2016, 11:00:17) 
[GCC 5.3.1 20160429] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import *
>>> print(Decimal('0.1') + Decimal('0.2'))
0.3
>>> (Decimal('0.1') + Decimal('0.2')) == Decimal('0.3')
True
>>>
>>
>>54696084
People who are over the 90's.
>>
>>54696134
That's the only way you should be storing numbers anyways. Everyone in this thread is wrong for using floating points unless they purposely want to be inaccurate.

>>54696084
Modern businesses making desktop applications.
I'm happily employed using C#.
>>
>>54696134
Wat de fug, lads
Python 2.7.11 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:58:36) [MSC v.1500 64 bit (AMD64)] on win32
>>> from decimal import *
>>> Decimal(0.1)+Decimal(0.2)==Decimal(0.3)
False
>>> Decimal('0.1')+Decimal('0.2')==Decimal('0.3')
True
>>
>>54696425
BCDs are basically just strings anyhow.
In most implementations, anyhow.

when you give decimal a number, it is subject to the same inaccuracies.
>>
>>54696425
I think your compiler is translating the decimal numbers you hardcode into it into floating points If you don't put them between quotes. Hence, 0.1 without quotes is being written into your executable as 0.10000000000000004 or something, then converted to a decimal.
>>
>>54696425
0.1, 0.2, 0.3 are stored as floats, not BCD
>>
#include <stdio.h>
int main(void)
{
float pc = 0.2;
float dc = 0.1;
float pd = dc + pc;
printf("Numbers are %.2f %.2f.\n", pc, dc);
printf("added gets %.2f.\n", pd);
return 0;
}
>>
Microsoft (R) F# Interactive version 14.0.23413.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> 0.1 + 0.2 = 0.3;;
val it : bool = false
>>
>>54695890
kek
> 0.1M + 0.2M = 0.3M;;
val it : bool = true
>>
>>54687961

>look at how fast I get the wrong answer
>>
>>54687006
>not converting your floats to ints in order to save memory
You would get the correct result too.
>>
>>(+ 2.0 1.0)
3


we did it reddit
>>
nice thread, today i learned.
>>
File: Selection_006.png (12 KB, 643x110) Image search: [Google]
Selection_006.png
12 KB, 643x110
wow that was hard
>>
File: 4chan_float_double_faggots.png (27 KB, 1018x588) Image search: [Google]
4chan_float_double_faggots.png
27 KB, 1018x588
Did I win?

>rust
>mozilla
>>
File: made_a_fucking_mistake.png (20 KB, 1044x628) Image search: [Google]
made_a_fucking_mistake.png
20 KB, 1044x628
>>54699221
whoopsie

>human
>programmer
>>
>>54696424
>That's the only way you should be storing numbers anyways.
Topcuck, Microshill probably also thinks C# is the only way you should be programming, Windows is the only OS you should ever use and touch interfaces are sufficient for all of humanity.
>>
>>54697482
>>not converting your floats to ints in order to save memory
>You would get the correct result too.
This is what the fourth variant in >>54687902 does.

Except since it's Haskell, it actually does it without getting in your way syntactically.
>>
>>54699945
Sorry, meant to link to >>54687942
>>
import std.stdio: writeln;
(0.2 + 0.1).writeln;

0.3


eat shit
>>
>>54700022
And of course
(0.2 + 0.1 == 0.3).writeln;

true
>>
>>54695466

Go home, Go. You're drunk.
>>
>>54687006
So proud of myself.
>>
>>54687205
It's impossible to perfectly add up numbers.

You've got only 32 or 64 bit so they are always little bit fuzzy
>>
>>54687289
Floats as BCD geez that would be overhead.
>>
>>54688130
You mean python programmers? :^)

This is why /g/ hates python, the following is full of beginner or shitty programmers
>>
>>54700122
senpai can't you read? It's 0.1+0.2 you tard
>>
>Be me
>buy two products: One costing 10 cents, and another costing 20
>mfw i'm getting scammed by .00000000000000004
>mfw no face

Why is this allowed
>>
>>54702087
Floating point invented by jews confirmed
Thread replies: 118
Thread images: 14

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.