Why 4.1%2 returns 0.0999999999999996?But 4.2%2==0.2.
See here: What Every Programmer Should Know About Floating-Point Arithmetic
Real numbers are infinite. Computers are working with a finite number of bits (32 bits, 64 bits today). As a result floating-point arithmetic done by computers cannot represent all the real numbers. 0.1 is one of these numbers.
Note that is not an issue related to Ruby, but to all programming languages because it comes from the way computers represent real numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With