Troubleshooting: Array Values Stored as 0.10000001

In summary, the conversation discusses a problem where storing a value as 0.1 in an array results in it being stored as 0.10000001, causing issues with calculating the average and stopping a loop. The problem stems from the fact that 0.1 cannot be stored exactly in binary due to it being a repeating decimal. The suggested solution is to store numbers as integers multiplied by a certain factor to achieve more accurate results.
  • #1
SolStis
12
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
  • #2
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.
 
  • #3
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
 
  • #4
http://docs.sun.com/source/806-3568/ncg_goldberg.html"
 
Last edited by a moderator:
  • #5
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.
 

What causes array values to be stored as 0.10000001?

This issue is most commonly caused by a phenomenon called floating-point rounding error. Computers store numbers in binary format, which can lead to small errors when converting between decimal and binary. These errors can accumulate and result in seemingly random values, such as 0.10000001, being stored in an array.

How can I prevent this issue from occurring in my code?

To prevent this issue, it is important to be mindful of the limitations of binary representation when working with decimal numbers. One approach is to use a decimal data type instead of a floating-point data type, which can provide more precise results. Additionally, rounding functions can be used to reduce the impact of floating-point errors.

Can this issue affect the accuracy of my calculations?

Yes, this issue can potentially affect the accuracy of calculations involving decimal numbers. While the errors may seem small, they can accumulate and result in significant differences in the final result. It is important to carefully consider the impact of floating-point rounding errors when performing calculations.

How can I identify if my code is affected by this issue?

If you notice unexpected values, such as 0.10000001, being stored in an array, it is likely that your code is affected by floating-point rounding error. You can also use debugging tools to analyze the values stored in your arrays and identify any patterns or inconsistencies.

Are there any other potential solutions to this issue?

In addition to using decimal data types and rounding functions, another solution is to use an arbitrary-precision arithmetic library. These libraries allow for more precise handling of decimal numbers and can help minimize the impact of floating-point errors. However, they may also come with a performance cost.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
812
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
976
  • Programming and Computer Science
Replies
11
Views
994
  • Programming and Computer Science
Replies
1
Views
943
Back
Top