Floating Point Arithmetic: Issues and Limitations | Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

"Hi! I am a test bot. Check https://xval.me for stuff!"

👍︎︎ 1 👤︎︎ u/sxtybot 📅︎︎ Mar 08 2021 🗫︎ replies
Captions
hello and welcome to our channel so before starting the video i have a simple question to you all and uh what is 0.6 minus 0.2 right so you have three seconds to think and give me the answer so i have heard so many of people saying that answer is 0.4 and which is wrong i mean technically it is right but according to python it is strong right so in order to understand that you know please stick to this video and give a like and subscribe to the channel if you found this video informative i'll try to keep this video as short as possible and try to give much information for you so let's try and now start understanding what's the problem that we are going to face by using floating point numbers arithmetic operations so i'm going to launch uh python here so i'm going to use python 3.9 so i'm not going to i'm not using any older versions of python this is the latest version of python and let's do some small latimeric operations what is 2 plus 3 5 what is 2 star 3 6 right so there is no problem that you are going to face by using this integers right so but the problem starts when you are you know moving to the above level of complexity that is the floating numbers so when i say 0.1 plus 0.1 so it is giving me 0.2 right so you might feel that there is nothing error in this right let me do something here so 0.6 minus let's say 0.6 so 0.9 minus 0.3 it has given me what zero point some n numbers of zeros and one right so but why is this happening because uh 0.9 minus 0.3 is technically 0.6 right but according to python it is not no let's also do something like this so 0.3 minus 3 so let's see for that it is equal to 0.6 or not and it is going to give you false no are you getting the point 0.9 minus 0.3 is not equal to 0.6 right because it is giving us false and to be honest whenever first time i saw this i was like shocked right now why is that happening right so is there anything wrong with my system or is very wrong with my python installation but there is nothing wrong with python insulation there is nothing wrong with your computer this is how the things are done right so as i said previously 0.6 minus 0.2 is not going to give us 0.4 but impact is going to give us 0.3 some ten nines or twelve lines and seven and if you ask me why is this happening this is because the way floating numbers are being represented in python itself right so first of all uh you need some back knowledge of what this happened so let's say you know that right so in machine so everything is stored in the form of uh ones and zeros right so everything is a binary right so even if you're trying to know store integers they're going to be stored as binaries and the same thing here even if you are trying to know store floating point numbers they are going to be stored as fractions that is uh they are going to be no uh converted into base uh base two values that is decimal numbers from by converting them into fractions so these numbers are converted into fractions fractions are then converted into base numbers and in fact there is an entire entire article on python documentation about that we'll also see that and also if you observe right so uh this has to do nothing with python right so python has nothing to do with this and this is not a bug but this is how uh machines are built right so as you know python so when we say python we are implementing to c python right so traditional python is nothing but c python so c python has been built on c right so it is being built using c language and in c languages the numbers are represented in the base two base two numbers right so uh the representation of floating numbers in python is similar in fact it is same as how the numbers are being represented in uh c language and you might also know that there is another version of python called as a pi pi that is i call it spifi you might be calling this pypy or something like that so the difference is py py is not built on c but it is built on a jit compiler and i'm not sure about the specifics what happens in inter really but when i say python that is even in your computer codings right so when you select python you are using c python by default but when you are using pypy you are using jit compiler jit is nothing but just in time compiler so that is a why you know you might have observed that you are getting better run times with pypy so let's try and implement this same thing with the pypy so i have uh chosen a pyp editor so for this i'm going to use id1.com so id1.com is a pretty good source for online editing tools so i'm going to use pyp by here and if i you know run 0.6 minus 0.2 then this is going to be give me giving us our exact answer the answer that we are uh expecting right so but when i go for traditional python so that is python 3 so i have used weeks for gigs ide for you and now if i print that i'm not going to get 0.4 but in fact i'm going to get 0.3 something and 7 right so i hope you understood the problem right so you understood what's uh wrong here and let me just give an example oh sorry let me just give a complete walk through here of why this is happening and what are the ways that which why you can no uh overcome this error right so as i said there is an uh documentation of every issue and limitation of with flight floating point archimedes in python right so i have i will link this in the description you can check that out for yourself so let's try and read this document so that you will understand along with me know of each and every quirks in uh dealing with floating point mathematics right so the first we have given an example that is called as 0.125 and they say that floating dependent numbers are represented in a computer hardware as base 20 base 2 fractions right so we're not going to store this as a direct numbers but we are going to know convert them into fractions and these fractions are then converted into base two numbers right so in the end everything is going to be zeros and once that is binary numbers but for the floating numbers they are going to be converted into fractions binary fractions and then they are going to convert into base two numbers right so for 0.125 we are going to say 1 by 10 plus 2 by 10 plus 5 by 10 so it is going to give us 1 2 5. the same same thing with zero point two zero zero one right but here comes the problem right so how are going to represent one by three that is we are trying to divide one unit into three people right so i can say it is 0.3 right so i will ask another person he will say no it is not 3.3 it is 0.33 another person says that 0.333 in fact all of them are right right you cannot uh no argue that a particular answer is wrong but all the answers are right but they have different level of error right so they all are approximate answers because no one knows the exact answer of 1 by 3 right because it is an continuous function it is an infinite function sorry infinite fraction right so no one knows the correct answer so if i say zero point ten nines then zero point uh sorry zero point uh ten threes then someone with answer like zero point twelve threes is also producing an answer which is better than me so basically he's doing the better approximation than me but he's giving him the same same answer so the answer is same but it levels uh differs on the different degrees of how we are approximating the number so this is what problem happens when representing floating point numbers into our machine right so when you're trying to know uh store a number called as 0.1 you are not going to store 0.1 you are going to store uh zero point triple zero this this is a binary representation and this is an infinity repeating fraction right so when when you face some infinite repeating fraction there has to be a point where you cut off cut the number because you cannot store infinite bits in the computer right so the computers are built in such a way that you know everything is fixed right so you have an integer you have a fraction integer has a three bits or integer has four bits or so i'm just giving a random number scheme and uh it is 64-bit representation it is 32-bit representation and there are some rules for which we have to stick to and you cannot play with the rules the same thing happens here so when trying to represent fraction numbers so we are going to be uh you know given with some uh infinite derivative fractions and uh we cannot represent inventory repeating fractions into finite space right so there has to be some kind of uh cut where no we say okay this is where i'm going to know stop the number and i'm taking this much of an error here and so this is what i'm going to store as my fraction so that is what happens here so when i when someone says 0.1 but uh the exactly the value that is being stored in the machine is 0.1 some 10 0's and some three fives and some random number but user does not want to say this number right so this is where python cleverly manages rounding of the values so it is not going to give you this value is going to give you 0.1 because it is rounding that number so when when someone is saying the numbers 0.1 right so as the python also says that the number 0.1 and 0.2101 and 0 some random number these are all approximations of the number 0.1 because they all are representing 0.1 but they all have a very some degree of error right so you have to keep that in mind and if you have so even the funnier thing is let know if you know add these numbers 0.1.1.1 it is not going to give you 0.3 is going to give you false right so we have seen that previously right so it is going to give us false because they all are different uh approximations and this does not equal to exactly 0.3 because yes yes we assume that the value has to be 3 but it is closer to three and not exactly three so this is where uh uh handling a floating point arithmetics in python uh gives some issues and i'm doing this video because i have faced that in hard way so i had an uh no offer with some company where they gave they wanted to know they wanted me to give a coding interview and this is what happened right so they were asked they were giving me some standard coin exchange problem where i have a customer who buys some stuff and pays cash and i have to return him change right so and there are multiple denominations of the number so if it was an indian company uh the the denominations would be something like 110 and all but it was an american company so the designations were something like uh i have a nickel that is point zero zero five dollars and i have a pennies that is points of one dollars so i had to use the floating point arithmetics and i'm unaware of this right so before this day i have i'm not no aware of this limitation so i simply implemented an algorithm to calculate the coin change and to my surprise when i tried to submit this uh i have i have noticed that there is some kind of uh approximation happening here and by the time i release there is not much time left so i had taken some wrong wrong submissions as well so i wanted to share this video with all so that uh no you all are aware of that and you can you know come back this error by using the round functions or you can also use pypy that is python that is made on jit compiler right so using these two methods you can overcome this error and i hope this video was informative and you found a new thing that which you are not familiar previously right so thanks for watching and if you found the video interesting please give a like and subscribe to the channel and we'll see you with more videos like this thanks for watching [Music] you
Info
Channel: Sai Prakash Reddy
Views: 281
Rating: 5 out of 5
Keywords: Sai Prakash, ieee 754, floating point error in python
Id: Hmm3QaOWBPM
Channel Id: undefined
Length: 12min 18sec (738 seconds)
Published: Sat Mar 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.