From: "Shot (Piotr Szotkowski)" Date: 2007-12-01T01:08:26+09:00 Subject: Re: Bizarre Floating point errors in Ruby? Serious bug? --pLNOfKbXnsRpMFAO Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MenTaLguY: > On Thu, 29 Nov 2007 06:03:21 +0900, "Shot (Piotr Szotkowski)" wrote: >> space.ship.traveller@gmail.com: >>> >>> print (-140.14 * 100) >>> -14014.0 >>> Well, actually this is the correct answer.... i.e. the result we are >>> looking for if we were using real math. But, it is not the correct >>> way to round 14013.999999, which is the actual value that we get with >>> floating point math... >> Why isn=E2=80=99t it? I might have learned basic math years ago, but to >> me -14013.999999 rounded to one decimal place is exactly -14014.0 >> (it=E2=80=99s also -14014.00 when rounded to two decimal places, as well= as >> -14014 when rounded to integers). > This comes up a lot, and I guess the thing that most people have > a hard time wrapping their heads around here is the fact that floating > point numbers don't obey the laws of basic math -- not exactly. Just to clarify =E2=80=93 I wasn=E2=80=99t writing anyhing about the ramifi= cations of IEEE 754, I was just pointing out that -14014.0 *is* the proper rounded version of -14013.999999, so there=E2=80=99s no bug in how Ruby rounds the IEEE 754 floats. For anyone finding this thread in their pursuit of =E2=80=98but *why* Ruby floats seem broken sometimes?=E2=80=99, a good starting point might be this Wikipedia entry: http://en.wikipedia.org/wiki/IEEE_754-1985 -- Shot (=E2=80=98that=E2=80=99s no bug, it=E2=80=99s a spac^H^H^H^H^Hn IEE= E float!=E2=80=99) --=20 "^\\([^ ()]+\\)\\(([0-9]+\\),\\([0-9]+\\))" gotta love regexps it looks like some elaborate Japanese smiley like "your parents just found out you've been slacking in class and you also have the flu" --pLNOfKbXnsRpMFAO Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFHUDOli/mCfdEo8UoRAtb5AJ9c2AAeTWbzdh/IZeCPKDguKttfuQCbBEsr MPNWI1oD6Px2sQPZ1zY+Y0U= =rvr+ -----END PGP SIGNATURE----- --pLNOfKbXnsRpMFAO--