Troubleshooting: Array Values Stored as 0.10000001

Click For Summary

Discussion Overview

The discussion revolves around the issue of floating-point precision in programming, specifically regarding the storage of the value 0.1 in an array and its implications for calculations such as averaging. Participants explore the challenges posed by binary representation of decimal numbers and propose various solutions to mitigate the problem.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that storing the value 0.1 results in it being represented as 0.10000001 due to floating-point precision issues, which affects the average calculation and causes a loop to malfunction.
  • Another participant shares a similar experience with a program for solving differential equations, explaining that the inability to represent 0.1 exactly in binary led to unexpected behavior in their calculations.
  • A participant describes a workaround they implemented, which involves creating a separate array to calculate the average, although they acknowledge it is cumbersome.
  • One participant suggests that for accurate values, it may be better to store numbers as integers by scaling them (e.g., multiplying by 1000 for three decimal places) to avoid precision issues.

Areas of Agreement / Disagreement

Participants express a shared understanding of the floating-point precision issue, but there are differing opinions on the best approach to resolve it, with no consensus on a single solution.

Contextual Notes

Participants highlight the limitations of floating-point representation in binary systems, particularly for decimal fractions, and the implications for mathematical operations. The discussion does not resolve the underlying mathematical complexities involved.

Who May Find This Useful

Programmers, computer scientists, and anyone dealing with numerical computations in software development may find this discussion relevant.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
6K
  • · Replies 7 ·
Replies
7
Views
3K