How regular(whole) numbers are stored in computers#

As you may known every thing on computer are stored in bits(binary) 0s and 1s, every integers is either represented in signed and unsigned, which in turn are represented as bits.

// signed example
var signed_int_with_8_bits int8
var signed_int_with_16_bits int16

// unsigned example
var unsigned_int_with_8_bits uint8
var unsigned_int_with_16_bits uint16

what does this unsigned and signed mean? unsigned -> only positive numbers, signed -> includes both positive and negative numbers. Now lets look how this unsigned and signed numbers are represented in bits(binary), which is the one is stored on to our computer.

// unsigned example
var unsigned_int_with_4_bits int4
unsigned_int_with_4_bits = 10
// btw for eg: consider go has 4bit int to make the example easier to explain, it ranges from 0-2^n - 1, n - bit(4, 8, 16, 32 etc)
// From the above code, we declared an unsigned 4bit value of `10`, which converts to `1010` in binary and this got stored into our computer, simple and straight forward

// signed example
var signed_int_with_4_bits int4
var signed_negative_int_with_4_bits int4
signed_int_with_4_bits = 2
signed_negative_int_with_4_bits = -2

// This is somewhat tricky here 4bit means it can fit from -2^(n-1) to 2^(n-1) - 1, for 4 bit it can fit from -8 to 7... 
// there is a reason why signed numbers supports less basically range n-1bits, coz since signed bit supports both positive and negative numbers
// the first bit(MSB bit) is always reserved to determine the sign of the number(positive or negative), for positive its 0 and negative its 1

// now lets see how the above signed numbers are converted to bits
// 2 -> 0010 simple and straight forward
// -2 -> 0010(first convert to binary), since its negative invert the binary now -> 1101, now add 1 -> 1110(this is the final binary that stored)
// now when decoding this 1110 -> it knows the MSB bit is 1 so it knows its negative, so first invert -> 0001, add 1 -> 0010(now we got the original)

How floating point numbers are stored in computers#

Now we know how regular numbers are stored in our systems, lets know about how floating point numbers are stored and used, unlike regular numbers(signed, unsigned) representing floating point numbers in binary is little tricky.

Any number can be expressed in floating point with something like mantessa x base^exponent.

EG:
180
1.8 x 10^2 -> mantessa: 1.8, base: 10, exponent: 2

0.02 
2 x 10^-2 -> mantessa: 2.0, base: 10, exponent: -2

0.012
1.2 x 10^-2 -> mantessa: 1.2, base: 10, exponent: -2

12.5
1.25 x 10^1 -> mantessa: 1.25, base: 10, exponent: 1
(this is called normalization, basically to keep the mantessa value between 1 <= n < 10)

Floating point numbers are represented as a whole with either 32 bits or 64 bits, for easier understanding we can take 32 bits as example in here.

For 32 bits, any floating point numbers are represented as
sign(1bit) + exponent(8bits) + mantessa(23bits)
Now lets walk through an example, consider -0.1 and 0.1

1. -0.1
lets convert 0.1 to binary
0.1 x 2 = 0.2 -> take bit 0
0.2 x 2 = 0.4 -> take bit 0
0.4 x 2 = 0.8 -> take bit 0
0.8 x 2 = 1.6 -> take bit 1
0.6 x 2 = 1.2 -> take bit 1
0.2 x 2 = 0.4 -> take bit 0
...
should go until it has 0, so that 0 x 2 -> 0, but for this example it just goes on as u can see,
if something has an indefinite, we can basically restrict that to 23bits(since our mantessa cannot exceed this), in this case our 0.1 would become
000110011001100110011001100110011... now convert this into proper binary based on above sign + exponent ..

now it becomes 0.0001100110011001100.., we add 0 since that is our integer part, for things like 10.1, this would be
1010.00011001100... coz binary of 10 is 1010.

now we should normalize it to make the MSB as 1, which now becomes 1.100110011001100110011.. since we shifted 4bits to right exponent becomes -4
exponent usually has a bias which is (2^n/2)-1, which in our case (2^8/2) - 1, since for exponent we have 8bits supported, the bias here is 127.
we should generally add our original exponent to this exponent bias to store which becomes -4 + 127 = 123, which is represented as
01111011 in 8bits


sign -> 1(since it is negative)
exponent -> 01111011(since exponent is 8bits and our exponent becomes 123 as u can see above)
mantessa -> 10011001100110011001101(mantessa bits upto 23)
you may wonder the last 4bits should be 1100, why 1101, coz our actual bits will be 11001, since our 5th bit(24th bit) is 1, we can round off
and keep 1101, this is like 1.1236 -> 1.124

now our full 32bits for -0.1 becomes
10111101110011001100110011001101

now when trying to decode, it takes the first bit and knows its negative by 1, 
takes the next 8 bits 01111011 -> decodes and knows its 123, now subtract with 127 -> 123 - 127 = -4, now knows its exponent
similarly for mantessa

From the above u can clearly see on how floating points are basically stored on our computer, this is why if u do 0.1+0.2 on some languages u dont end up with 0.3, instead 0.3…. 0.1 -> 00111101110011001100110011001101 0.2 -> 00111110010011001100110011001101
upon adding these two u would get 00111110100110011001100110011011 in binary, how floating addition takes place is like normal addition with some minor tweaks, and then this is converted to decimal which could be 0.3000…