From: Chad Perrin Date: 2011-03-30T05:08:36+09:00 Subject: Re: Why can a floating point number be used as an array index? --ZGiS0Q5IWpPtfppv Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Mar 30, 2011 at 03:35:21AM +0900, Jeff Dik wrote: > Why can a floating point number be used as an array index? Anybody > know of a good use case for this? >=20 > irb(main):020:0> [1, 2, 3][0.3] > 1 > irb(main):021:0> [1, 2, 3][0.9] > 1 I don't know about why, exactly, or about use cases (though I suppose it might just make things easier sometimes, without errors cropping up all over the place if you're working with floating point numbers a lot and are too lazy to do integer conversions yourself). How seems, from a little experimenting, to be self-evident to me: $ irb irb(main):001:0> [1,2,3][0.7] =3D> 1 irb(main):002:0> [1,2,3][1.7] =3D> 2 irb(main):003:0> [1,2,3][1.1] =3D> 2 irb(main):004:0> [1,2,3][-0.1] =3D> 1 irb(main):005:0> [1,2,3][-1.1] =3D> 3 It looks like it just does integer truncation. --=20 Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ] --ZGiS0Q5IWpPtfppv Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.14 (FreeBSD) iEYEARECAAYFAk2SOWIACgkQ9mn/Pj01uKVsBwCgpZw7ib2135kiX2kwHpazUekb moIAoI117pHZatIC0QXzRZo2JY9N+9in =nfQN -----END PGP SIGNATURE----- --ZGiS0Q5IWpPtfppv--