|
-
June 28th, 2012, 04:40 PM
#8
Re: Glass Rod Problem
 Originally Posted by Paul McKenzie
I guess you didn't heed the advice to not use floating point variables as counters. Your program will more than likely not run consistently depending on compiler, compiler options chosen, etc. Those loops will run either fewer or more times then you expect.
Always use integer if you want make sure loops iterate the exact number of times.
Paul's advice is exactly correct.
Nevertheless, you might have "lucked out" with your program because of one of the design goals of IEEE-753/754 on the format of floating point numbers. According to this goal, any integer between +/- 2-to-the-number-of-bits-in-the-significand has an exact representation in floating point. For example, for single precision floats, where there are 24 bits in the significand (including the implied leading bit of "1"), all integers between +/- 16,777,216 have an exact representation.
Your code increments the "count" variable by 1, and I doubt that you have more than 16,777,216 trials, so you probably have "lucked out" even though you have incorrectly and dangerously used a float, and not an integer, as your loop counter.
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|