As a few members don't seem to understand the relationship between decimal values and binary representation of them via floating point values, I thought I'd toss together this quick little program so someone could play around with it and see how our values are stored in memory.
_Title "Floating Point Variables"
For i
= 2 To 8: b
(i
) = 1:
Next 'start with a 127 exponent for a value of 0, normalized
l = 0: ex = 0
b$ = "1."
For i
= 1 To 8 'calculate the exponent value Next 'print the 32-bit SINGLE For i
= 9 To 31 'assemble the mantissa into a single string to display If b
(i
) <> 0 Then b$
= b$
+ "1" Else b$
= b$
+ "0"
_MemPut m
, m.OFFSET
, l
'put our 4-byte value into the single variable
Print "Left/Right to move selector" Print "Up/Down to select/unselect bit" Print "Space to toggle bit" Print "+/- to increment whole exponent by 1" Print "Your current sign is:"; b
(0) Print "Your current exponent is:"; ex
- 127 Print "The binary value (in formalized format) is: "; b$;
"E";
_Trim$(Str$(ex
- 127)) Print Using "Which in decimal value is: #################.###################"; s
Now, the important things to keep in mind here is:
1) Your binary value is stored in formalized format. Just as 1,234 is written as 1.234E3, the binary value of 111 (which is 7, in base-10 math) is written and stored as 1.11E2.
2) When looking at how this value is stored in memory, it's stored with:
the first bit representing the sign. (+ or -)
the next 8 bits represent our exponent PLUS 127 (to store negative values)
the remaining bits represent our formalized format
3) Now note, since formalized format will ALWAYS begin with a 1, storing that value is redundant and as such it isn't done. We only store those values right of the period.
So, putting this together, The value for 1 would be:
0 --- 01111111 --- 0000000000000000000000
That first 0 represents that there's no negative sign. (Toggle it and you'll see that our value swaps to become a -1)
The next 8 bits represent the value of 127; which as I mentioned before is an exponent of 0 PLUS 127. This gives us an exponent of 0.
The rest of the bits are the mantissa, which in this case are just 0.
Put those together, and you have a binary value of: +1.0E0 (Remember, the leading 1 to the left of the period is implied and not stored.)
And, the value for 2 would be:
0 --- 10000000 --- 0000000000000000000000
That first 0 represents that there's no negative sign. (Toggle it and you'll see that our value swaps to become a -1)
The next 8 bits represent the value of 128; which as I mentioned before is an exponent of 1 PLUS 127. This gives us an exponent of 1.
The rest of the bits are the mantissa, which in this case are just 0.
Put those together, and you have a binary value of: +1.0E1 (Remember, the leading 1 to the left of the period is implied and not stored.)
Shift that to normalized format and the binary value is: 10. (And, as everyone knows, 10 = 2.)
And, the value for 3 would be:
0 --- 10000000 --- 1000000000000000000000
That first 0 represents that there's no negative sign. (Toggle it and you'll see that our value swaps to become a -1)
The next 8 bits represent the value of 128; which as I mentioned before is an exponent of 1 PLUS 127. This gives us an exponent of 1.
The rest of the bits are the mantissa, which in this case are just 1, followed by a ton of zeros.
Put those together, and you have a binary value of: +1.1E1 (Remember, the leading 1 to the left of the period is implied and not stored.)
Shift that to normalized format and the binary value is: 11. (And, as everyone knows, 11 = 3.)
And since we can now count from 1 to 3 in SINGLE, take a moment to look at how fractional values are stored:
0 --- 01111111 --- 1100000000000000000000
That first 0 represents that there's no negative sign. (Toggle it and you'll see that our value swaps to become a -1)
The next 8 bits represent the value of 127; which as I mentioned before is an exponent of 0 PLUS 127. This gives us an exponent of 0.
The rest of the bits are the mantissa, which in this case are two 1's, followed by all 0's.
Put those together, and you have a binary value of: +1.11E0 (Remember, the leading 1 to the left of the period is implied and not stored.)
In normalized format, that'd look like: 1.11 in binary.
In decimal (base-10) values, what we'd have is:
1 in the one's position. (left of the period.)
1 in the one half's position. (first one right of the period.)
1 in the one fourth's position. (furthermost one on the right.)
Added together 1 + 1/2 + 1/4 = 1.75
1,11 binary (base-2) = 1.75 decimal (base-10)
Play around with it. If nobody believes that our decimal values are stored as base-2 values after this, I don't know what to tell you. Apparently I'll never be able to illustrate it, or explain it to the point where it makes sense for that person and they can connect the dots and put it all together. I've tried, so all I can say is if this doesn't sort it out for you, "Find someone else to explain it and teach it to you."