Error in my code

Pages: 12
closed account (48T7M4Gy)
How come your original code here is the same as the one at the address above?
Hi,

Don't mix int s and doubles in a for loop. double is not represented exactly, so this might cause off by one errors in the for loop. Instead work out how many iterations of the loop need to happen as an int, then use that value in the for loop.

Floating point types like double are stored as binary fractions - there are lots of values that don't work out exactly. For example 2.0 might really be 1.999999999999997 or it could be 2.000000000000003. In the former case, this code does 1 extra iteration: for (int i = 0; i < 2.0 ; i++)

Obviously it's not a problem if one enters 10.5, but one might be surprised by some numbers which one thinks is a whole number but isn't exactly that when it is converted to double. So in short it's not good practise.
closed account (D4NbpfjN)
what do you mean? about it being at the address above? So basically with the ints and double it is making my program go crazy
what do you mean? about it being at the address above


So you now have 3 accounts that we know of, asking the same questions with exactly the same code. That's not good. Also combined with asking rather silly questions - it looks a lot like trolling. Reported
closed account (D4NbpfjN)
nope only one account. Reason why i have the same code is because i run the code. Then i do my own code. I change the whole thing. I just want to make sure i am in the right ball park when writing the code.
BS
closed account (D4NbpfjN)
okay i am sorry about all the mess. i do only have one account though. Sorry for asking dumb questions. I will figure it out. Thank you for the help.
closed account (48T7M4Gy)
All the best dylan. I think we're done here. Bye, bye.

PS Thanks TIM
Topic archived. No new replies allowed.
Pages: 12