448,651 Members | 1,757 Online
Need help? Post your question and get tips & solutions from a community of 448,651 IT Pros & Developers. It's quick & easy.

# Integers vs floats

 P: 12 Can someone explain the output I'm getting from the following 2 code portions? In the first, I've used n and sizeOfArrays as long doubles, and in the second I've made sizeOfArrays an integer, as this is the form needed for setting the number of elements in an array. However, the moment it's in integer form, the output is "wrong" ie not what I want it to be. Why is this? How can I get around it? Expand|Select|Wrap|Line Numbers     long double h = 0.1;     long double n = 1.5/h;     long double sizeOfArrays = n + 1;       cout.setf(ios::fixed);     cout.precision(7);       cout << n << " and  " << sizeOfArrays << endl;   Output is 15.0000000 and 16.0000000 Expand|Select|Wrap|Line Numbers     long double h = 0.1;     long double n = 1.5/h;     int sizeOfArrays = n + 1;       cout.setf(ios::fixed);     cout.precision(7);       cout << n << " and  " << sizeOfArrays << endl;   Output is 15.0000000 and 15 If I set n as an integer, it gives an output value of 14, too. This I could understand better, and setting the array size as "n+1" naturally wouldn't work while n was a float, but I thought I could get around it all by creating a new variable called sizeOfArrays set as an integer. The results are above. May 9 '07 #1
6 Replies

 P: 12 To explain more clearly what output I was expecting: n should be = 15, doing the calculation manually, therefore "sizeOfArray" should be = 16. Calling n an integer makes its value = 14, and calling "sizeOfArray" and integer makes it 15. This is what I don't understand. I have to make one or the other an integer to be able to use it to describe the number of elements in the array. The reason I want to do this all in code (when I know what the answers are!) is that I want to be able to change the value of h at will, and n (and therefore the desired array size) depends on the value of h, so doing it in code stops me from having to go all the way through the rest of the program changing every bit that's dependent on h. May 9 '07 #2