Jay wrote:ah so it is a little bug! lol i was thinking either my pc had gone wonky or my install had gone south!
Well, the PC's CPU is doing exactly what it has been told to do.
It attempts the multiplication, and then finds that the result won't fit to 32 bits - so the bits that would represent the bigger number are just lost, leading to the strange results (one of the bits is used to indicate +/- too). It's what's known as an overflow error - trying to "fit a quart into a pint pot."
But there is something being hidden from us.
When this happens inside the CPU, a flag gets set called the "Overflow flag" - and in many PC languages, the programmer would be able to check that flag, and use it to show an error message, or call a special maths routine that could handle a bigger number by splitting the number up into chunks.
Tester's solution works up to a point - but there is still a problem that must be accounted for...
Although the float numbers are alllowed to be much bigger (or smaller), there will be a loss of precision - i.e. the result of the multiplication will be approximately the right size, but not always the exact answer.
It works a little like this...
Lets say you have a very rubbish calculator that could only show you four digits. How would you write 123,000,000?
Ah - there is a way - we can make a rule that says "the last digit is the number of zeros". So the calculator could write it as "123
6" - which is the principle of 'scientific notation" or "E notation" (e.g. 1.543e12).
But what about 123,000,001? Well, the best we could do here is to say "the one is so small, let's just ignore it" - and our crappy four digit calculator still writes "123
6"
So when using float numbers, above a certain value, you can't represent every single integer value - there's a point where only even numbers can be represented, later, only every fourth one, and so on as the numbers get bigger - which means there can still be maths errors. e.g.
123,000,000 + 1 = 123,000,000
So , FS is not really doing much wrong - the way the CPU represents numbers is the biggest part of the limitation. BUT, I do think it would be good if the integer maths primitives showed us the overflow error rather than outputting a confusing value - the same way that floats show us "NF" or "NAN" when there is a float maths problem.
Note that within Ruby, the number ranges are much larger - floats are 64bit rather than 32bit, and integers are also 64bit, but also with a special "BigNum" class than can handle integers of any size (at much greater CPU cost). But once you send those numbers out of Ruby and into the 'green' world, they will again be truncated to the 32bit values.