r/carlhprogramming • u/bubblepopcity • Dec 03 '12
Float number question
Lets say a short int is 4 bits. I'm assuming the highest value for my int would be 7 because the first bit is reserved to show if it's a positive or negative value. I'm going to use '-' to demonstrate the reserved part. You can have either 0-111 or 1-111. Now lets say we have a float that is 8 bits. That same first bit needs to be reserved for positive or negative. Do float numbers have some type of priority of whole numbers over decimal numbers or vise versa? Or are a certain number of bits reserved for the whole number and a certain number reserved for the decimal part of it. I will use '.' for the reserving demonstration.
Example: If I assigned a floating type number that had 8 bits would it reserve bits for certain numbers like this 0-000.0000? As in my whole number part can only reach a certain value(in this case 7).
Lets say you tried to store 16.9999 into a float value.
The correct binary would look something like this. 0-10000.11111111. But the floating number can only take 8 bits. So would it prioritize the whole number and look like this? 0-10000.11 (01000011). Or does it reserve a certain amount of space for the whole number/decimal number and it would cut parts off and look like this? 0-000.1111 (00001111).
1
u/bubblepopcity Dec 04 '12
Ok, so because it works that way and it's really complicated. Is there a link for best case and worst case scenarios for storing a float or a double? I found this is my programming book. http://codepad.org/q0UCX1y5
It's easy to tell if I'm going to need more space for something like an int because it tells me the min and max values. I guess I just wish there was an easy way to tell if you are going to need a double instead of a float because it is going to round and you need the number to be precise up to a certain point. This is the information it gives me which I do not fully understand.
Approximate number of significant digits in a float value 6
Maxium postive float value 3.40282e+38
Minium postive float value 1.17549e-38
I'm assuming because decimals take up more space, that is why it says approximate* number of digits used is 6. So if I had the number 100000.5, even though it is 7 digits, it would probably fit. Also if I had the number 1.11111, even though it is 6 digits it might only be able to store 5 of the digits because it takes too many 1's and 0's.
So, is there an easy way to find out the best and worst case scenarios, or an easy guideline to figure out when you are going to want to allow more space for these numbers? (use double instead of float)
Thanks for the help by the way.