Foredoomed
Foredoomed

Reputation: 2239

Offset binary format for float in java

when i am looking at the wikipedia page for Offset Binary, i cant follow the following sentence:

Unusually however, instead of using "excess 2^(n-1)" it uses "excess 2^(n-1)-1" which means that inverting the leading (high-order) bit of the exponent will not convert the exponent to correct twos' complement notation.

can anyone explain it in details and give me some examples?

Upvotes: 2

Views: 822

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533500

This is the range of exponents which allows you to calculate 1/Float.MAX_VALUE and 1/Float.MIN_NORMAL without going to zero or Infinity. If there was one more negative exponent and one less positive exponent, (with an offset of 128) 1/Float.MIN_NORMAL would be Infinity.


The exponent in floating point is offset rather than plain two-complement.

e.g. for double a 0 exponent is 11-bit value for 1023 or 0b0111111111, -1 is 1022 or 0b0111111110, +1 is 1024 0b10000000000.

In twos-complement, the number would be 0 is 0b00000000000, -1 is 0b11111111111, and +1 is 0b00000000001

A property of using an offset, is that the maximum is half the possible number of values. i.e. for 11 bits the range is -1023 to 1024 instead of -1024 to 1023.

Another property is that number can be compared by just comparing the integer values.

i.e.

long l1 = Double.doubleToRawLongBits(d1);
long l2 = Double.doubleToRawLongBits(d2);
if (l1 > l2) // like d1 > d2

The only difference is in the handling of NaN and -0.0 The approach Double.compare(double, double) uses is based on this.

Upvotes: 2

Related Questions