- 8 months ago
Hey there! Ready to crack the code on IEEE 754 floating-point numbers? In this video, I break down how to convert decimals to binary and back for 32-bit and 64-bit floats with clear, easy-to-follow steps. From scientific notation to biasing exponents, I?ve got you covered with examples (like 45.84375!). Perfect for computer science students, programmers, or anyone curious about how computers store fractions. Pause, rewind, and learn at your pace! Subscribe for more, check my other videos, and drop a comment with your thoughts!
Introduction to IEEE 754 00:00:00
Prerequisites for Understanding 00:01:03
Steps to Convert Decimal to IEEE 754 00:02:31
Bias and Exponent Explanation 00:03:17
32-bit Float Layout 00:04:09
Sign Bit and Fraction Bits 00:05:44
Example: Decimal to 32-bit Float 00:07:56
Converting 45.84375 to Binary 00:08:34
Scientific Notation for Binary 00:09:05
Biasing the Exponent 00:10:26
Placing Bits in Layout 00:11:17
Final 32-bit Float Representation 00:13:32
Example: IEEE 754 to Decimal 00:14:28
Extracting Sign, Exponent, Fraction 00:15:36
Unbiasing Exponent and Reconstructing 00:16:23
Converting Binary to Decimal 00:18:53
Introduction to 64-bit Floats 00:19:47
64-bit Float Layout and Bias 00:20:05
Example: 64-bit Float to Decimal 00:21:09
Processing 64-bit Float Bits 00:22:05
Reconstructing 64-bit Scientific Notation 00:23:26
Final Decimal Conversion for 64-bit 00:25:00
Conclusion and Recap 00:25:54
Call to Subscribe and Outro 00:26:36
Thanks for watching!
Find us on other social media here:
- https://www.NeuralLantern.com/social
Please help support us!
- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras
Introduction to IEEE 754 00:00:00
Prerequisites for Understanding 00:01:03
Steps to Convert Decimal to IEEE 754 00:02:31
Bias and Exponent Explanation 00:03:17
32-bit Float Layout 00:04:09
Sign Bit and Fraction Bits 00:05:44
Example: Decimal to 32-bit Float 00:07:56
Converting 45.84375 to Binary 00:08:34
Scientific Notation for Binary 00:09:05
Biasing the Exponent 00:10:26
Placing Bits in Layout 00:11:17
Final 32-bit Float Representation 00:13:32
Example: IEEE 754 to Decimal 00:14:28
Extracting Sign, Exponent, Fraction 00:15:36
Unbiasing Exponent and Reconstructing 00:16:23
Converting Binary to Decimal 00:18:53
Introduction to 64-bit Floats 00:19:47
64-bit Float Layout and Bias 00:20:05
Example: 64-bit Float to Decimal 00:21:09
Processing 64-bit Float Bits 00:22:05
Reconstructing 64-bit Scientific Notation 00:23:26
Final Decimal Conversion for 64-bit 00:25:00
Conclusion and Recap 00:25:54
Call to Subscribe and Outro 00:26:36
Thanks for watching!
Find us on other social media here:
- https://www.NeuralLantern.com/social
Please help support us!
- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras
Category
🤖
TechTranscript
00:00Hey there! Let's talk about IEEE 754 floating point numbers stored inside of your machine.
00:13What the heck am I even talking about? Well, there's a standard called IEEE 754 which governs
00:20how floating point numbers are stored inside of your computer on x8664 machines. So,
00:26you know at this point of watching this video you probably already know how to convert binary
00:32back and forth you know between binary and decimal and hopefully you understand how to do binary with
00:36fractions. If you don't I have other videos I'll talk about that in a second and you also understand
00:43that in the machine there is like a slightly different format that's my doggy slightly
00:47different format for storing signed integers in the machine. Okay so floating point numbers have
00:52an even different format that we have to follow and the hardware is designed that way. So before
00:58we can really benefit... you're all going to see how bad I spell. Okay before we can benefit from this
01:07video your prerequisite knowledge should be that for starters you should be able to convert back and
01:13forth between binary and decimal including numbers with fractions. So if you don't know how to do that yet
01:25this video is is not for you just yet you should go check out my other videos I explain in other videos
01:31exactly how to do that. Go to the other videos come back and then check this one out after you know how
01:36to convert back and forth between binary and decimal with fractions. The second thing you should know how
01:41to do before watching this video is convert binary numbers with fractions
01:49to scientific notation and and back again meaning you should understand scientific notation and you
01:57should also understand how to take a binary number with a fraction and convert it to scientific notation
02:02and then convert it or deconvert it back to like regular regular form. Again I have another video for
02:09this so if you don't know how to do that yet you should probably pause this video and then go find my
02:13other video and check that out. Okay so the basic steps let's see for converting from a regular number
02:26to IEEE floating point number in the machine using just binary alone is the following so I'm going to say
02:31basic steps now whoops whoops step one convert the decimal number to binary if needed and I say if
02:44needed because I don't know maybe you started with a binary number so you don't actually need to convert it
02:48but I'm just going to assume that you have a decimal number so you're going to convert it to binary first
02:52then convert the binary number to scientific notation I don't know if convert is the right word here
03:00because we're not actually changing the value of the number we're just changing the representation so
03:05convert it to a scientific notation form maybe would be better step three we'll add a bias to the
03:13exponents of the scientific notation form what do I mean by that you know if you have like a binary
03:21number and it's in scientific notation format it's going to look like this I liked when I'm doing IEEE 754
03:27I like to keep the right side of the number in decimal I'm sorry if this confuses you but
03:34it's not really necessary to convert this to binary when we're just trying to convert back and forth
03:38between IEEE so I always say two to the something power rather than one zero to the something power
03:44because I want to stay in decimal so I'll say you know two to the seventh power right what this means
03:51is that seven is the exponent and I have to bias the exponent before I put it into the machine so
03:59maybe it's worth understanding or maybe copy pasting right now that the bias for 32-bit floating
04:05point numbers I should probably I'll paste the whole thing here okay so here's like the format
04:10for a 32-bit floating point number we're good about this just for a second the bias for 32-bit floating
04:16point number in IEEE 754 is 127 so that means I pretty much have to just take the number seven
04:23and bias it by adding 127 to it after I've done that I can convert the biased exponent to unsigned
04:35binary whole number to an unsigned binary whole number so step four here is just regular
04:42another um decimal to binary conversion so I would take you know seven plus 127 the result of that
04:49which I think is like 134 maybe uh will just be converted into binary and unsigned binary integer
04:55not two's complement or anything like that then step five is respect the layout so I just
05:03I just copy pasted the layout up above and the layout is this you can see that uh here let me see if I just
05:11maybe paste this down at the bottom you can see that we actually have 32 bits here if I count them
05:16for you real fast we've got one for the sign and then we've got um I can never remember this I think
05:22it's eight for the exponent one two three four five six seven eight got eight bits for the exponent that
05:27means I've already used nine of 32 bits so that means I should have like 23 bits here so one two three
05:34four five six seven nine ten eleven twelve thirty twenty twenty twenty one twenty two twenty three
05:40just to double check so we know what the sign bit is actually we don't yet know in this video so in this
05:47video the sign bit it's always going to be a zero if the number is positive and a one if the number is
05:54negative so that's the sign bit the biased exponent bits go there and then these f's those are just the
06:01fractional bits uh the bits that are just the you know like the mantissa or the mantissa part
06:07of the fraction so imagine if we have a number like this all i'm saying is we would copy paste those
06:13numbers right here notice how i'm ignoring that first one dot something because when you put binary
06:20in scientific notation the first number is always going to be a one so it can be assumed there's no
06:25point in storing it inside the machine and then if you don't have enough digits to account for all of
06:30the fractional bits you will literally just pad with zeros on the right side because padding with
06:36zeros on the right side of a fraction doesn't actually change the value one two three four
06:40if i put the numbers if i put the zeros on the left side of the fraction it would make the fractional
06:44part smaller and smaller so that would be bad i don't want to change the value and this is the
06:48opposite of how you pad a whole number if i put numbers on the left side of a whole number part i'm not
06:53changing the value but if i put numbers on the right side of a whole number part which you know in this
06:57case would be the biased exponent then i would be changing the value so we wouldn't want to do that
07:03anyway i'm going to remove this uh and just stick with step five so let's see five a is place the
07:09sine bits step five b is and i'm going to do an example for you don't worry place the exponent bits and
07:18then five c is place the fraction bits everything after the first one dot from scientific notation form
07:36again because the first one is implied it will never be a zero if you've done it correctly
07:40if you end up with a binary number that looks like this in scientific notation
07:43then you've done something wrong because the first number is supposed to be a one
07:49okay so we have those basic steps and now let's do an example so here's an example um one two three
07:58example let's convert decimal uh well to i triple e 754 32-bit float format aka single precision floating
08:13point number double precision floating point number is 64-bit we'll do an example for that at the very
08:18end of this but i'm just going to stick with 32 and do the hard work here so suppose we have
08:23the number 45.84375 84375 just double checking and then we'll convert uh to binary again this knowledge
08:34is contained in a different video so if you don't know how to do this yet you'll need to go see my other
08:40video and so you can see it's uh you know 45.84375 in binary is just going to be this i've padded it
08:48with two zeros to the left just because i don't know i have a habit of wanting everything to look
08:53like it's inside of eight bit chunks you can see there's eight bits here but really i can just remove
08:57those first two numbers it doesn't matter convert the number to binary uh then i have to convert the
09:04binary number to scientific notation i'm going to put maybe pseudo scientific notation because i'm
09:10not going to convert the right part where it's the exponent uh into binary i'm going to keep it in
09:15decimal you know just to clarify again uh if you were going to convert a binary number to scientific
09:22notation and you were actually going to publish it somewhere or give it to someone then you should
09:26convert the right side it's just that i'm not going to because i don't really need to convert the right
09:30side i'm not interested in having the whole number in scientific notation in binary i'm just interested
09:36in what the fractional bits are and then what the uh what the exponent is going to do like how many
09:42positions to the left or right is it actually going to you know move the decimal point and that's just
09:47going to be the number five you know five times to the left or to the right uh so i'm going to go
09:53pseudo scientific notation uh you pretty much you know as i covered in the different video all we're
09:59going to be doing is just moving the decimal point over one two three four five until there's a one
10:03in the leading spot and then account for it by saying times two to the fifth power to say the number
10:10is a lot bigger than it looks so that's that and um let's see if there are any trailing zeros that's okay
10:18on the right side of the fraction they don't actually matter you can also just delete them if you want
10:23then we need to bias the exponents so how do we bias the exponent the exponent is just the number
10:29five that's see i was telling you like i don't really care to convert it to binary because i'm
10:33just gonna bias it first and then after that i'll buy it or i'll convert it to binary so biasing it is
10:39just basically adding the bias i should probably copy the layout here just to make sure that it's on
10:46this page and it's easy to see so i'll do that so in the 32-bit float layout the bias is 127 so i'm
10:54going to add 127 to 5 the final number is going to be 132 so that's the biased exponent now then i
11:00convert the biased exponent to binary and that's going to be this 10000100 then set up the layout
11:15so you don't get confused this step is crucial everyone always gets confused by the layout because
11:21like we're human beings i can't count 32 zeros that are all like in the same area and and like you
11:27know it and and not get confused every single time i mean i guess i won't get confused half the time but
11:33that's not good enough so step one is place the layout for yourself so it's easier for you to place
11:40the bits and i wrote this at the top eight bits and 23 bits just as a reminder double check yourself
11:45one two three four five six seven eight nine ten one two three four five six seven eight nine ten
11:49one two three that's 23 bits one two three four one two three four that's eight bits we don't need to
11:54remember that the sign bit is one bit for now it's not too hard so now let's place it so the first thing
12:01to note is that this number was actually positive positive is implied in ieee unless you have the the
12:08negative sign there but if it makes you feel better we'll just put a positive sign there just to
12:12clarify and be specific so because the number is positive the sign bit is going to be zero
12:17if it were negative it would be one then we will put the biased exponent bits so we we already cop
12:24we already converted up there so i'm just going to copy paste it here
12:29we're pretty lucky that the bits are just as big as the number of biased exponent bits that are available
12:35but suppose for the sake of argument that we had just like you know this small number here
12:40for the biased exponent what would you do with the remaining spaces you would just pad with zeros to
12:45the left because remember in whole numbers in binary and decimal if you pad to the left you're not
12:50actually changing the value uh you definitely would not want to pad to the right but you got to put
12:55a bit in every position it's got to be something so i'm just going to pad with zeros
13:01then we take the fractional part from the scientifically notated form so that's just this right
13:05here notice how i'm copy pasting every bit except for the one that starts the number i'm not going
13:12to do the one dot part because that's always assumed so we don't need to store it it wouldn't make sense
13:17i'll copy paste the fractional part and i'll just stick it down there
13:21and now we have a bunch of leftover bits that we need to fill out
13:24with the fractional part you pad to the right not to the left so i'm just going to put zeros
13:29to the right because that won't change the value of the fractional part
13:32here now we've got it i'm just going to double check my work here
13:37oh gosh it's too hard to read and then a bunch of zeros okay
13:42then remove these spaces because now that you've done your layout uh you actually want to
13:47bear in mind that the machine doesn't have spaces the machine is just you know a bunch of ones and
13:51zeros everywhere so i'm just going to carefully remove the two spaces and this is it this is our final
13:58i triple e 754 floating point representation of the number of the decimal number positive 45.84375
14:09seems like a lot but it's not too bad once you kind of get used to the format and the bias
14:14and you already know the other steps of you know converting decimal uh to binary and back again
14:21so let's do another number let's do it backwards so i'm going to open up a new tab here
14:24and i'm going to say um example i triple e 754 32 bits float two decimal
14:36okay so suppose we started with the following number
14:41start with this number oh my gosh right remember the layout so uh in the layout we have use the layout
14:49maybe i should copy paste the layout again one more time just to make sure that it's easy to see
14:58so i'm going to paste it up at the top and um well no maybe i want this because this is like a little
15:04bit better i want that version okay so 32 bits a bias of 127 so we're going to use the layout i'm just
15:15going to copy paste the original number and then add spaces because i know well i guess i can copy
15:21paste the layout itself right above that number i know i need a space right there that's the sign bit
15:27and then i need a space after that and notice how the numbers all line up so there's nothing missing
15:32just to double check yourself okay so i have the layout i know right away that the number is going
15:37to be positive because the sign bit is zero sign is positive okay so then um i need to pull the biased
15:45uh exponent pull the biased exponent and so that's just going to be the exponent bits only so i'm
15:53just going to copy paste those bits and then i'll pull out the fractional part first pull out the
16:01fractional part i'm going to copy paste all those bits and maybe i'll just say zero point those bits
16:07because that's what i originally grabbed and i can delete all the zeros on the right side because
16:12again that won't change the value of the fractional part so i'm going to delete make sure you're only
16:15deleting zeros though delete that now i need to unbias the biased exponents uh first actually i should
16:29well let's convert the biased exponents to decimal a little easier i guess you don't have to do that
16:35but it's a little easier for me pull the biased exponent uh so this number is is going to be uh 132
16:45which i think is what we had last night let me just double check that 132 yeah so the biased exponent
16:50that we pulled out is 132 then we unbiased the biased exponent so just subtract the bias again in
16:5732-bit format here the bias is 127 so i'm going to subtract 20 127 instead of adding 127 so now i know
17:04the real bias or sorry i know the real exponent is five then i can recreate the binary number in scientific
17:14notation i'll put um i'll put pseudo here just because it's not you know i'm not i'm not converting
17:21binary for for the entire thing so that means i can take the fractional part here and i can say
17:28it's going to be one dot the fractional part maybe i should remove that zero here to make it less
17:39i don't know because if i put a one there it kind of feels like i'm saying the number is zero dot
17:43something but we know that it's one dot something i don't like that i'm putting a zero there i'm just
17:46going to i'm just going to erase it because it's really going to end up being a one and if i say
17:52pull the fractional part and i put a one there then it doesn't kind of sound like i'm pulling the
17:56one from somewhere no the one is implied anyway so now i can recreate the number in pseudoscientific
18:02notation i can say times two to the something power we know that the exponent is five again it's
18:08pseudo scientific notation because if by we're going to do the complete you know true scientific
18:13notation the right side should also be in binary but i'm not going to do it i'm a little bit lazy
18:19because we don't need that i triply so it's going to be two to the fifth power
18:24then if i undo the scientific notation i'm really just going to take the scientifically notated
18:32version and i'm going to say well i'm going to move the decimal point to the right five times
18:37because that's what the uh the part on the right says so i'm going to go one two three four five stick
18:42it there remove the original decimal point and then i can remove uh the the exponent part so
18:48then let me just double check my work one zero one one zero one one one zero one okay so we've got
18:54that now then all we have to do is convert uh this number from binary to decimal
19:05the final number here is just you know it's going to be the same number that we worked with previously
19:09so i'm just going to copy paste it there is uh you know of course this video is not about converting
19:16back and forth from binary to decimal with fractions check out my other video if you want
19:20to learn how to do that but uh once we do that you know this number becomes that number
19:30and uh and now we know how to convert back and forth between a decimal number and i triple e
19:35754 32-bit floats in the machine no problem a lot of steps but no problem once you understand them
19:44okay now let's do an example with 64-bit floating point numbers honestly this is not going to be
19:50more difficult in 64-bit floats the idea and the format and the steps it's all going to be the exact
19:55same thing it's just that we have more bits for the biased exponent and the fraction and then the
20:01bias itself will be a little bit higher so let me copy paste the format for you to see so in i
20:06triple e 64-bit floats i triple e 64-bit uh float uh well i triple e 754 64-bit float uh you know
20:20we refer to these floats as doubles right double precision floating point numbers notice how instead
20:26of eight bits for the biased exponent we have 11 so we can represent a much larger number and also
20:32notice how for the uh the fraction part instead of having 23 bits we have 52 bits so we can also
20:38represent much more precisely that's really the only difference let's uh oh sorry the other difference
20:44is also that the bias is now equal to 1023 instead of 127. so what is that like 10 times larger of a
20:55number that we can represent or something like that so mostly we get more precision um if you don't
21:00like it sorry it's the way it is it's in the hardware so let's do an example for a 64-bit uh float
21:07for a double 64-bit example uh let's say we started with the following number
21:19inside of the machine like we looked inside of the machine and we grabbed these bits
21:25and i'll get rid of the spaces so we have like a giant big huge number we're trying to figure out
21:31you know what is what is this number in decimal so i'm going to add spaces uh to respect the layout
21:38i'm going to just copy paste the layout right here i'm going to say respect the layout i'm going to
21:43paste it and then that helps me line everything up so i'm going to say the sine bit is the first bit
21:47and then i'll put a space there so i know all right the sine bit is a one that means it's going to be a
21:51negative number the biased exponent bits are there the fractional bits are there so well first thing
22:00i can just do is say that the sine is going to be negative because the sine bit is a one
22:07and then i'll pull the biased exponent bits which is going to be this number right here
22:19if i convert the biased well maybe i should pull the fraction bits next
22:27fraction bits are just here i'm going to copy paste those
22:29here and then i can remove the zeros be very careful you're not removing any ones
22:37so there was a lot of wasted bits there that's okay i'll convert the biased exponent
22:42to decimal so that's going to be um this number becomes 1031 i think and i did not do that in my
22:55head i have an answer sheet don't worry i'm not that cool then we'll unbiased or de-bias
23:02unbias the biased exponents by just subtracting the bias remember in this 64-bit format the bias is
23:101023 so that means i got to take 1031 minus 1023 and that will be equal to eight
23:20okay now we kind of have everything we need to sort of reconstruct the scientific notation part right
23:26we have the uh the fraction bits that's the most important part oh and then also the sine bit so i
23:31can say negative something it's obviously going to be one dot something because even though the number
23:36is negative it will still always start with a one that rule doesn't change negative one dot something
23:42and i'll just paste the whoops paste the fraction bits i think i did paste the right thing so we paste
23:49the fraction bits and then we have to use the uh regular exponent the unbiased exponent the non-biased
23:56exponent two times times two to the whoops times two to the eighth power
24:00four now this is our number in scientific notation um convert to sci scientific notation
24:12i'll put p for pseudo scientific notation
24:17and then now we have to convert to just regular format
24:21okay so i'm going to copy paste this right here
24:28and if it's two to the eighth power that means the real number is going to be bigger than it looks
24:34in scientific notation even though if you are very familiar with scientific notation you you kind of
24:38already know that it looks big but you know if we just if we're only talking about the bits on the left
24:42we'll move it over to the right eight times one two three four five six seven eight put the decimal
24:48point there remove the original decimal point now we no longer need two to the eighth power
24:53and again just as a reminder if it was two to the negative eight power you would be moving the
24:57decimal point in the other direction so now we've got this number one one one zero one zero one zero one
25:05and then a point zero one okay so then convert to decimal and again this video doesn't talk about
25:12how to convert back and forth from binary to decimal find my other videos but basically
25:17uh it's going to end up being six uh negative four six nine point two five
25:25okay we now have done a quick practice starting with a giant number of bits and slowly converting them
25:34to the real uh floating point number in decimal that uh that they represent i'm not going to do another
25:40number in the reverse direction because we already did that for 32 bits
25:44and again with 64 bits the only real difference is just you have more exponent and fraction bits
25:51and the bias is a higher number and that's it so like all the other steps are just the same
25:55i just wanted to show you one time with a 64-bit number in one direction but um i think at this
26:01point we're good thank you so much for watching this video i hope you learned a little bit of stuff
26:07and i hope you had a little bit of fun i'll see you in the next video
26:10hey everybody thanks for watching this video again from the bottom of my heart i really appreciate it
26:18i do hope you did learn something and have some fun uh if you could do me a please a small little
26:23favor could you please subscribe and follow this channel or these videos or whatever it is you do
26:29on the current social media website that you're looking at right now um it would really mean the
26:34world to me and it'll help make more videos and grow this community so we'll be able to do more
26:38videos longer videos better videos or just i'll be able to keep making videos in general so please
26:44do do me a kindness and uh and subscribe you know sometimes i'm sleeping in the middle of the night
26:50and i just wake up because i know somebody subscribed or followed it just wakes me up and
26:54i get filled with joy that's exactly what happens every single time so you could do it as a nice
26:58favor to me or you could you control me if you want to just wake me up in the middle of the night
27:02just subscribe and then i'll i'll just wake up i promise that's what will happen also uh if you
27:08look at the middle of the screen right now you should see a qr code which you can scan in order to
27:12go to the website which i think is also named somewhere at the bottom of this video and it'll take
27:17you to my main website where you can just kind of like see all the videos i published and the
27:22services and tutorials and things that i offer and all that good stuff and uh if you have a suggestion
27:29for uh uh clarifications or errata or just future videos that you want to see please leave a comment
27:36or if you just want to say hey what's up what's going on you know just send me a comment whatever i also
27:40wake up for those in the middle of the night i get i wake up in a cold sweat and i'm like this
27:44it would really it would really mean the world to me i would really appreciate it so again thank
27:50you so much for watching this video and um enjoy the cool music as as i fade into the darkness which
27:58is coming for us all
28:28so
28:32so
28:34so
28:36so
28:38so
28:40so
28:42so
28:44so
Be the first to comment