MattC wrote: ↑Sat 26 Nov 2022, 18:17
Can someone please explain why I get this error in my program while using integers (%).
Well, for a start it's not an "error" - that's an emotive word because it implies something is wrong, which it isn't.
The reason you get that 'result' is because of rounding. Let's take it step by step. The calculation you are doing boils down to this:
This sets V to a value
slightly less than 1000, and when I say
slightly we can see just how small the difference is as follows:
This prints
5.55111512E-17 so the value is very nearly 1000, but not quite.
When you PRINT a number, it is automatically rounded to the nearest value that can be represented with the precision specified (by default 9 significant figures) so of course
PRINT V prints 1000.
But when you cast a value to an integer it is
rounded down to the next lowest integer (technically it is
truncated towards zero). So because V is very slightly less than 1000, despite the difference being infinitesimal, it is rounded down to 999.
All completely normal. For reference you might like to know just how precisely the value
1.2 can be represented as an 80-bit floating-point number. This is that closest value:
Code: Select all
1.20000000000000000004336808689942017736029811203479766845703125