From: Konrad Meyer Date: 2007-08-10T06:12:14+09:00 Subject: Re: precision loss with pack/unpack ? --nextPart3871076.j0fQIJU8k6 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Thursday 09 August 2007 12:06:31 pm Luis Parravicini wrote: > Hello >=20 > I'm reading Java class files using ruby and found some problems when > handling float values. The numbers are stored using IEEE 754 format. > After several hours trying and thinking I might be using unpack the > wrong way, I've wrote the following code which packs a Float and then > immediately unpacks it. And the result is different from the original > value. Yes, small differences are common with floats. The best way to compare them is with something like that demonstrated in:=20 http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%= 20point%20numbers.htm#_Toc135149455 > ------------ > a =3D 0.1234567 > b =3D [a].pack('g').unpack('g')[0] >=20 > puts "#{a} =3D? #{b} =3D> #{a =3D=3D b}" > ------------ >=20 > outputs: >=20 > 0.1234567 =3D? 0.123456701636314 =3D> false >=20 > I came accross this doc, which states that in Perl this happens too > (http://perldoc.perl.org/functions/pack.html) "Note that Perl uses > doubles internally for all numeric calculation, and converting from > double into float and thence back to double again will lose precision > (i.e., unpack("f", pack("f", $foo)) will not in general equal $foo)." > Maybe Ruby is doing the same Perl does? Ruby uses doubles internally for floats as well. Loss of precision is common, but the above link should work well for you. > Any help will be appreciated >=20 > Thanks No problem, glad to (try to) help. =2D-=20 Konrad Meyer http://konrad.sobertillnoon.com/ --nextPart3871076.j0fQIJU8k6 Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBGu4MyCHB0oCiR2cwRAlCBAKCEoZRUszCJIg/zMskAGfO3bTAtxACeJ5oN uOAg43AUqIfFpDrKnF5jzRI= =Y1Sg -----END PGP SIGNATURE----- --nextPart3871076.j0fQIJU8k6--