4

How do interpreted languages typically go about representing floating-point numbers in their bytecode? For example, suppose I've got a Java program with the line

double a = 3.0;

What does 3.0 look like in the program's bytecode? I see two possibilities:

1) Writing the bit-level representation of the in-memory number into the bytecode, and then just reading it back into memory when we need it. There are probably problems with portability here (the size of double on different machines can vary, so how do we store a number in bytecode in a portable way?).

2) Write a canonical representation of the number into the bytecode which can be decoded when program execution time comes. The literal string 3.0 might serve that purpose here, but then we're wasting execution time parsing a floating-point number during runtime.

So my question is, which strategy is most common/efficient/best? You can feel free to cite examples from different interpreted languages that compile down to bytecode (Python, Lua, etc.), or merely point me in the direction of some resources of some relevant articles if that's necessary (I've been Googling quite a bit, but few virtual machines are well-documented).

1 Answer 1

6

Most languages do something like #1, that is, they specify a particular binary encoding, but said encoding is almost invariably IEEE-754 because that is the floating-point standard that a majority of architectures implement. A “double-precision” IEEE-754 floating-point number is defined to be 64 bits.

Examples:

  • The Java class file format specifies the encoding of floating-point values stored in the constant table and as immediate operands to instructions:

    The high_bytes and low_bytes items of the CONSTANT_Double_info structure together represent the double value in IEEE 754 floating-point double format (§2.3.2). The bytes of each item are stored in big-endian (high byte first) order.

  • The Lua VM also assumes IEEE-754 floats:

    Number is the Lua number data type, normally an IEEE 754 64-bit double.

1
  • 1
    Lua is more like OP's first option (go native, accept portability problems). The Lua bytecode format isn't intended to be portable, and (as the "normally" indicates) the number type can vary depending on how Lua is compiled.
    – user7043
    Jul 7, 2013 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.