I never had this problem in C# unless I worked with divisions or really really huge numbers (larger than 32 bit floats) perhaps or some complex stuff. But in C# I remember this to work. Ruby however rolls over and dies
at just 9 iterations with
just 3 decimals. I am totally... Shocked... I expect 0.01 to be followed by a infinite amount of zero's and not a semi-random number (not totally random, always the same sequence) somewhere at the end that comes out of nowhere.
I understand that floats are imprecise in Ruby due to rounding, divisions or when they get too big, etc. But there is no rounding here. This feels so wrong.
@stretch_progress += 0.01 # <<< bugs. # Old code@stretch_progress = (@stretch_progress + 0.01).round(3) # <<<< This is... Silly. But sadly works. Even if @stretch_progress is a 'clean multiple' of 0.01Some tests:
0.01 + 0.01 == 0.02 # good
Code:f = 0.0100.times do f += 0.01endp f # 1.000007 and 0.0999999 at the 9th iteration instead of 0.09.
Code:f = 0.09.times do f += 0.01endp f # 0.09 (now it DOES work, I only changed the max. number of loops)
Code:a = 0a += 0.01a += 0.01a += 0.01a += 0.01a += 0.01a += 0.01a += 0.01a += 0.01a += 0.01p a # 0.09
Yes I compare the numbers and I noticed that the comparison always failed. There is some kind of pseudo-random number at the end.
This almost wants me to make my own float with a set number of digits which are always accurate for adding numbers (which is easily possible). Forcing every number after x digits to always be a zero no matter what (basically automatic-rounding). Floats are so extremely imprecise (unless I do something wrong) that you should never ever work with them unless you strictly round them everywhere or just never ever compare them. I really did not expect this to happen with just 3 decimals and 9 iterations and by simply adding the numbers.
EDIT:
I'm incrementing a percentage between 0.00 and 1.00.
And BigDecimal is not included in RGSS. It seems to 'have been left out' of the library. Both the class and the required file are not present. In theory BigDecimal should solve the problem:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/BigDecimal.html
BigDecimal provides similar support for very large or very accurate floating point numbers.