-4

I try to fill dynamically allocated arrays with numbers that are calculated in the program. One of these arrays is very large (1000 x 900 x 3).

If I fill it with doubles, the program output will occasionally be reasonable, but on successive runs of the same code different outputs are created. In most cases these become absurdly huge (10e+74 or 10e+228). However, if I use floats for that specific array, everything is fine - numbers are reasonable and reproducible.

Any thoughts why this happens? Even more important: Can I just keep floating numbers or may there be a larger issue that needs further investigation

Edit:

This calculated the entries of the large array:

T[it+1][ix][iy] = T[it][ix][iy] + ((1/(dx*dx))*(T[it][ix+1][iy] - 2*T[it][ix][iy] + T[it][ix-1][iy]) + (1/(dy*dy))*(T[it][ix][iy+1] - 2*T[it][ix][iy] + T[it][ix][iy-1]) + M_SQRT2*cos(iy*dy)- Pe*((1/((2*dx)))*vx[it][ix][iy]*(T[it][ix+1][iy]-T[it][ix-1][iy]) + (1/(2*dy))*vy[it][ix][iy]*(T[it][ix][iy+1] - T[it][ix][iy-1])))*dt;

I don't want to post the whole code, it is fairy large. If you would like to see anything else, please let me know.

Edit 2:

After rewriting the program with 3D-vectors, which did not change anything. I eventually found out, that I used an even number for the array sizes. Changing it (and the intervals dx and dy) to an odd number fixes the issue. Any thoughts on that phenomenon?

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
chris1992
  • 41
  • 1
  • 8
  • 5
    It sounds like you have a bug in your code. – Oliver Charlesworth Mar 06 '15 at 07:28
  • 3
    Without seeing the code that manipulate the data, it's a bit hard to suggest something useful. – R Sahu Mar 06 '15 at 07:28
  • I just liked to know if this behavior is usual when using doubles instead of floats, i.e. a whether it is a typical indication to use floats rather that doubles. – chris1992 Mar 06 '15 at 07:48
  • 1
    I suggest stripping down the code to the minimum required to reproduce the problem. You will probably find the bug during that process - removing something that should be irrelevant makes the program work. If not, you will have something you can post. – Patricia Shanahan Mar 06 '15 at 11:37

1 Answers1

1

To answer your question: you typically shouldn't see incorrect behaviour with doubles if you have correct behaviour with floats (the other way around is quite possible, as floats are more susceptible to round-off error, and underflow/overflow).

Usually if you see extreme values like 10e+228 it is a sign that you're accessing uninitialised memory: check that all your variables and array values have been properly initialised.

Simon Byrne
  • 7,694
  • 1
  • 26
  • 50
  • Thank you, that was my initial thought as well, but I have looked at it quite intensely now and cannot find anything wrong, but I will look once more... – chris1992 Mar 06 '15 at 08:07
  • 10e+228 is hex 6f7a6208b5068394. 10e+74 is 4f81afd6ec0e1411. Both look as though they could be slightly mangled ASCII data, consistent with the uninitialized data theory. – Patricia Shanahan Mar 06 '15 at 15:13