Troubleshooting: Array Values Stored as 0.10000001

AI Thread Summary
The discussion centers on the issue of floating-point precision in programming, specifically when allocating a value of 0.1 in an array. The value is stored as 0.10000001 due to the limitations of binary representation, leading to inaccuracies in calculations. This results in the average being greater than 0.1, preventing a loop from terminating as expected. A historical reference is made to a similar problem encountered in differential equations, where values were not printed due to similar precision issues. A proposed solution involves using an array to calculate the average, but this is deemed cumbersome. A more effective recommendation is to store numbers as integers by multiplying them by a factor based on the desired decimal precision, ensuring exact results and avoiding floating-point errors.
SolStis
Messages
10
Reaction score
0
I am allocating a value of 0.1 to a position in an array and it seems to be storing itself as 0.10000001. The problem is that once all the values in the array have reached 0.1 the average should be 0.1 and the loop stops, but as it is being stored as 0.1000001 the average is > 0.1 and the loop never stops
any ideas?
 
Technology news on Phys.org
That looks to me like a problem I ran into many years ago in a program to solve differential equations: I set delta x equal to 0.001 and and thought I had it set to print values when x was equal to 0.1, 0.2, etc. but it never printed!

The problem was that since 0.1= 1/10 so the denominator is NOT a power of 2, in binary that is a repeating "decimal". It cannot be stored exactly in memory so the arithmetic is not exact.
 
Ive came up with a cumbersome solution to it. made an array equal to it and calculated the average of that then used that as the comparison variable. Bit cumbersome but it worked...

Thanks for the reply though
 
http://docs.sun.com/source/806-3568/ncg_goldberg.html"
 
Last edited by a moderator:
If you want accurate values, and you know how many decimal places you are working to, then you are better to store the numbers as integers.

Say you want to work to three decimal places, then multiply all the numbers by 1000 and store them as integers. That way you will get exact results.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top