Array storage

  • Thread starter SolStis
  • Start date
  • #1
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?
 

Answers and Replies

  • #2
HallsofIvy
Science Advisor
Homework Helper
41,833
956
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
12
0
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
KTC
90
0
http://docs.sun.com/source/806-3568/ncg_goldberg.html" [Broken]
 
Last edited by a moderator:
  • #5
299
1
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.
 

Related Threads on Array storage

  • Last Post
Replies
1
Views
2K
Replies
1
Views
3K
  • Last Post
Replies
1
Views
1K
Replies
7
Views
7K
  • Last Post
Replies
2
Views
4K
  • Last Post
Replies
1
Views
609
  • Last Post
Replies
10
Views
3K
Replies
1
Views
1K
  • Last Post
Replies
14
Views
4K
Top