You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleFraction.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package org.apache.poi.ss.format;
  16. public class SimpleFraction {
  17. /** The denominator. */
  18. private final int denominator;
  19. /** The numerator. */
  20. private final int numerator;
  21. /**
  22. * Create a fraction given a double value and a denominator.
  23. *
  24. * @param val double value of fraction
  25. * @param exactDenom the exact denominator
  26. * @return a SimpleFraction with the given values set.
  27. */
  28. public static SimpleFraction buildFractionExactDenominator(double val, int exactDenom){
  29. int num = (int)Math.round(val*exactDenom);
  30. return new SimpleFraction(num,exactDenom);
  31. }
  32. /**
  33. * Create a fraction given the double value and either the maximum error
  34. * allowed or the maximum number of denominator digits.
  35. *
  36. * @param value the double value to convert to a fraction.
  37. * @param maxDenominator maximum denominator value allowed.
  38. *
  39. * @throws RuntimeException if the continued fraction failed to
  40. * converge.
  41. * @throws IllegalArgumentException if value > Integer.MAX_VALUE
  42. */
  43. public static SimpleFraction buildFractionMaxDenominator(double value, int maxDenominator){
  44. return buildFractionMaxDenominator(value, 0, maxDenominator, 100);
  45. }
  46. /**
  47. * Create a fraction given the double value and either the maximum error
  48. * allowed or the maximum number of denominator digits.
  49. * <p>
  50. * References:
  51. * <ul>
  52. * <li><a href="http://mathworld.wolfram.com/ContinuedFraction.html">
  53. * Continued Fraction</a> equations (11) and (22)-(26)</li>
  54. * </ul>
  55. * </p>
  56. *
  57. * Based on org.apache.commons.math.fraction.Fraction from Apache Commons-Math.
  58. * YK: The only reason of having this class is to avoid dependency on the Commons-Math jar.
  59. *
  60. * @param value the double value to convert to a fraction.
  61. * @param epsilon maximum error allowed. The resulting fraction is within
  62. * <code>epsilon</code> of <code>value</code>, in absolute terms.
  63. * @param maxDenominator maximum denominator value allowed.
  64. * @param maxIterations maximum number of convergents
  65. * @throws RuntimeException if the continued fraction failed to
  66. * converge.
  67. * @throws IllegalArgumentException if value > Integer.MAX_VALUE
  68. */
  69. private static SimpleFraction buildFractionMaxDenominator(double value, double epsilon, int maxDenominator, int maxIterations)
  70. {
  71. long overflow = Integer.MAX_VALUE;
  72. double r0 = value;
  73. long a0 = (long)Math.floor(r0);
  74. if (a0 > overflow) {
  75. throw new IllegalArgumentException("Overflow trying to convert "+value+" to fraction ("+a0+"/"+1l+")");
  76. }
  77. // check for (almost) integer arguments, which should not go
  78. // to iterations.
  79. if (Math.abs(a0 - value) < epsilon) {
  80. return new SimpleFraction((int)a0, 1);
  81. }
  82. long p0 = 1;
  83. long q0 = 0;
  84. long p1 = a0;
  85. long q1 = 1;
  86. long p2;
  87. long q2;
  88. int n = 0;
  89. boolean stop = false;
  90. do {
  91. ++n;
  92. double r1 = 1.0 / (r0 - a0);
  93. long a1 = (long)Math.floor(r1);
  94. p2 = (a1 * p1) + p0;
  95. q2 = (a1 * q1) + q0;
  96. //MATH-996/POI-55419
  97. if (epsilon == 0.0f && maxDenominator > 0 && Math.abs(q2) > maxDenominator &&
  98. Math.abs(q1) < maxDenominator){
  99. return new SimpleFraction((int)p1, (int)q1);
  100. }
  101. if ((p2 > overflow) || (q2 > overflow)) {
  102. throw new RuntimeException("Overflow trying to convert "+value+" to fraction ("+p2+"/"+q2+")");
  103. }
  104. double convergent = (double)p2 / (double)q2;
  105. if (n < maxIterations && Math.abs(convergent - value) > epsilon && q2 < maxDenominator) {
  106. p0 = p1;
  107. p1 = p2;
  108. q0 = q1;
  109. q1 = q2;
  110. a0 = a1;
  111. r0 = r1;
  112. } else {
  113. stop = true;
  114. }
  115. } while (!stop);
  116. if (n >= maxIterations) {
  117. throw new RuntimeException("Unable to convert "+value+" to fraction after "+maxIterations+" iterations");
  118. }
  119. if (q2 < maxDenominator) {
  120. return new SimpleFraction((int) p2, (int)q2);
  121. } else {
  122. return new SimpleFraction((int)p1, (int)q1);
  123. }
  124. }
  125. /**
  126. * Create a fraction given a numerator and denominator.
  127. * @param numerator
  128. * @param denominator maxDenominator The maximum allowed value for denominator
  129. */
  130. public SimpleFraction(int numerator, int denominator)
  131. {
  132. this.numerator = numerator;
  133. this.denominator = denominator;
  134. }
  135. /**
  136. * Access the denominator.
  137. * @return the denominator.
  138. */
  139. public int getDenominator() {
  140. return denominator;
  141. }
  142. /**
  143. * Access the numerator.
  144. * @return the numerator.
  145. */
  146. public int getNumerator() {
  147. return numerator;
  148. }
  149. }