--nextPart3871076.j0fQIJU8k6 Content-Type: text/plain; charset so-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Thursday 09 August 2007 12:06:31 pm Luis Parravicini wrote: > Hello > > 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: http://www.cygnus-software.com/papers/comparingfloats/Comparing%20floating%20point%20numbers.htm#_Toc135149455 > ------------ > a = 0.1234567 > b = [a].pack('g').unpack('g')[0] > > puts "#{a} =? #{b} => #{a == b}" > ------------ > > outputs: > > 0.1234567 =? 0.123456701636314 => false > > 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 > > Thanks No problem, glad to (try to) help. -- Konrad Meyer <konrad / tylerc.org> 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 uOAg43AUqIfFpDrKnF5jzRISg -----END PGP SIGNATURE----- --nextPart3871076.j0fQIJU8k6--