1.1+0.1=1.2000000000000002??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

1.1+0.1=1.2000000000000002???

why do 1.1 + 0.1=1.2000000000000002 ? in Julia language

27th Jan 2018, 7:09 PM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
7 Answers
+ 17
The short answer is, that floating point arithmetic sucks. It's not only Julia, try and run print(1.1+0.1==1.2) in Python or try it in any other language.For the long answer, read https://en.m.wikipedia.org/wiki/Floating-point_arithmetic
27th Jan 2018, 7:20 PM
Tob
Tob - avatar
+ 5
My pleasure. :)
27th Jan 2018, 7:29 PM
Tob
Tob - avatar
+ 4
thanks alot
27th Jan 2018, 7:22 PM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 4
You can either use the built in type Rational or get a package for decimal floating point arithmetic. Both have their own limitations though.
28th Jan 2018, 5:46 PM
Tob
Tob - avatar
+ 2
how can I avoid the error in my calculations?
28th Jan 2018, 4:48 PM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 2
thanks
28th Jan 2018, 5:58 PM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 1
high precision. Gotta tell it to convert to different type of different Float bit
8th Mar 2021, 4:40 AM
Neur0s1s
Neur0s1s - avatar