How to convert double to its exact fractional equivalent in Java?

In technical terms, I need a method that converts an IEEE 754 binary64 number to a reduced ratio of two BigIntegers that mathematically represent exactly the same cost. The method should not process infinite or NaN values , but it should process subnormal files and signed zeros . Since the IEEE 754 binary code number format does not support the presentation of irrational numbers, this task is theoretically possible.

Here are some examples of values:

  • 0.0 = 0/1
  • -0.0 = 0/1
  • 0.5 = 1/2
  • 0.1 = 3602879701896397/36028797018963968
  • 1 / (double) 3 = 6004799503160661/18014398509481984
  • Double.MIN_NORMAL = 1/2 ^ 1022 = 1/44942328371557897693232629769725618340449424473557664318357520289433168951375240783177119330601884005280028469967848339414697442203604155623211857659868531094441973356216371319075554900311523529863270738021251442209537670585615720368478277635206809290837627671146574559986811484619929076208839082406056034304
  • Double.MIN_VALUE = 1/2 ^ 1074 = 1/202402253307310618352495346718917307049556649764142118356901358027430339567995346891960383701437124495187077864316811911389808737385793476867013399940738509921517424276566361364466907742093216341239767678472745068562007483424692698618103355649159556340810056512358769552333414615230502532186327508646006263307707741093494784
  • Double.MAX_VALUE = (2 ^ 1 024 - 2 ^ 971)/1 = 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368/1
+4
1

, .

, 11 , 52 .

0.

, ( ), Math.getExponent . ,

  • NaN , Double.MAX_EXPONENT + 1.
  • , Double.MIN_EXPONENT -1.

, 1 52 . , (.. ) 1, 52 , .

public static BigInteger[] convertToFraction(double value) {
  int exponent = Math.getExponent(value);
  if (exponent > Double.MAX_EXPONENT) {
    // The value is infinite or NaN.
    throw new IllegalArgumentException("Illegal parameter 'value': " + value);
  }
  long positiveSignificand;
  if (exponent < Double.MIN_EXPONENT) {
    // The value is subnormal.
    exponent++;
    positiveSignificand = Double.doubleToLongBits(value) & 0x000fffffffffffffL;
  } else {
    positiveSignificand = (Double.doubleToLongBits(value) & 0x000fffffffffffffL) | 0x0010000000000000L;
  }
  BigInteger significand = BigInteger.valueOf(value < 0 ? -positiveSignificand : positiveSignificand);
  exponent -= 52; // Adjust the exponent for an integral significand.
  BigInteger coefficient = BigInteger.ONE.shiftLeft(Math.abs(exponent));
  if (exponent >= 0) {
    return new BigInteger[] { significand.multiply(coefficient), BigInteger.ONE };
  } else {
    BigInteger gcd = significand.gcd(coefficient);
    return new BigInteger[] { significand.divide(gcd), coefficient.divide(gcd) };
  }
}
+4

All Articles