Evan Evan - 8 months ago 26
Perl Question

What is the perl equivalent to Java's Double.doubleToLongBits()?

I have two floating point numbers and I want to compute their difference in Units in Last Place (ULP). In Java this is:

Double.doubleToLongBits(a) - Double.doubleToLongBits(b)

What is the equivalent in Perl?


On a Perl with double-precision floats[1] and integers at least 64 bits in size[2], you can use the following:

unpack('Q', pack('d', $n))

For example,

$ perl -e'printf("%X\n", unpack("Q", pack("d", -0.1)))'

  1. perl -V:nvsize gives 8
  2. perl -V:ivsize gives 8 or higher