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.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...
Back
Top