In Week 2 Lecture (the first one), Mr. Malan says a reason for floating - point imprecision (40:00). I don't get his reason. Sorry for the bad question, but please explain...
Thanks in advance.
2 Answers
While it is easy to represent integers in a computer, it is different for floating point numbers. Most FP numbers cannot be precisely represented inside a computer. The reason is because numbers are represented in base 2 in a computer, but most base 10 fractions can't be represented by base 2 systems. For a detailed explanation, you might try googling for base 2 representation and storage of floating point numbers in computers.
Simply put, a float may appear to be accurately stored, until you look at it to a high number of digits. For example, 4.2 may print out as 4.2 on your computer, but if you change the number of digits to be printed, you'll find that 4.2 is actually stored in a computer as something like 4.199999998753 (this is just an example, your numbers may differ.)
If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)
-
actually you can't lose precision converting bases, if you need a more deep understanding you should try googling for base 2 representation of floating point in computer systems Commented Jan 1, 2016 at 12:52
-
1Really! Certain fractions cannot be accurately represented as decimal numbers for any particular base. For example, 1/3 cannot be accurately and exactly represented in base 10 by a fixed number of digits. It repeats forever as 0.33333.... Others can be represented precisely, such as 1/5 in base 10, or 0.2. However, in base 2, a fraction can only be accurately represented if the denominator is a power of 2. So, if you try to convert 1/5 base 10 to a FP stored in base 2, you can only approximate it because 1/5 cannot be precisely represented in base 2.– Cliff BCommented Jan 1, 2016 at 18:48
-
Fractions are out of scope for this discussion, and floating point calculation is not as simple as you assume, also
0.5
has an exact representation in binary00111111000000000000000000000000
Commented Jan 1, 2016 at 21:24 -
Of course 0.5 stores precisely. 0.5 = 1/2 and 1/2 has a denominator that is a power of 2. My example was 0.2 or 1/5. Run the following program: #include <stdio.h> int main(int argc, char* argv[]) { float x = 0.2; printf("0.2 is stored as %0.26f\n", x); } The result is
0.2 is stored as 0.20000000298023223876953125
If 0.2 were precisely stored, it would have printed as 0.2000000000000000000000000.– Cliff BCommented Jan 1, 2016 at 22:08 -
Sorry, my mistake I didn't actually understood the floating point quite right, thank you for your patience Commented Jan 2, 2016 at 0:22
First you must understand that there are infinite floating numbers between any two numbers, so floating points between 0.0
and 1.0
are infinite.
If you have limited number of pointers and you want to represent those numbers, you would only be able to represent subset of them, and round all numbers in between.
Let's say you have only 3
pointers to represent numbers between 0 and 1, you may choose [0.25,0.5,0.75]
, as your pointers, that means if you want to represent 0.4
or 0.45
you would use 0.5
.
Floating types has finite number of digits so it can only represent a subset of the floating point spectrum.
Edit
Look at Cliff B answer for a more Accurate explanation. Also, I found this video Floating Point Numbers - Computerphileto be very helpful.
-
Not really a good explanation. The quantity of pointers that one might have available has nothing to do with how accurately they are stored. If the storage method were accurate, the number stored in each pointer would be accurate, regardless of how many numbers there are between any two pointers - which could still be infinite. The problem lies in how the numbers are actually stored.– Cliff BCommented Jan 1, 2016 at 5:31
-
It's actually a very good explanation, and as said “if you can't explain it simply you don't understand it well enough”, my answer simplifies the reason for accuracy loss in floating point representation without going through the actual procedure, if you want a more detailed explanation read about the different representations of floating points in computer systems Commented Jan 1, 2016 at 12:39
-
Sorry Bonga your answer isn't very clear...but what are pointers anyway? If it was explained in the lecture, sorry because how can I find the specific spot in the vide?! Commented Jan 2, 2016 at 4:05