Fraction.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * This file is part of architectury.
  3. * Copyright (C) 2020, 2021 architectury
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 3 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package me.shedaniel.architectury.utils;
  20. import com.google.common.math.LongMath;
  21. import org.jetbrains.annotations.NotNull;
  22. import java.text.DecimalFormat;
  23. public final class Fraction extends Number implements Comparable<Fraction> {
  24. private static final Fraction[] SIMPLE_CACHE = new Fraction[2048];
  25. private static final Fraction ZERO = ofWhole(0);
  26. private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("###.###");
  27. private final long numerator;
  28. private final long denominator;
  29. private boolean simplified;
  30. static {
  31. for (int i = 0; i < 2048; i++) {
  32. SIMPLE_CACHE[i] = new Fraction(i - 1024, 1);
  33. }
  34. }
  35. private Fraction(long numerator, long denominator) {
  36. if (denominator > 0) {
  37. this.numerator = numerator;
  38. this.denominator = denominator;
  39. } else if (denominator < 0) {
  40. this.numerator = -numerator;
  41. this.denominator = -denominator;
  42. } else {
  43. throw new ArithmeticException("/ by zero");
  44. }
  45. this.simplified = (this.numerator >= -1 && this.numerator <= 1) || this.denominator == 1;
  46. }
  47. public static Fraction zero() {
  48. return ZERO;
  49. }
  50. public static Fraction ofWhole(long whole) {
  51. if (whole >= -1024 && whole < 1024) {
  52. Fraction cached = SIMPLE_CACHE[(int) whole + 1024];
  53. if (cached != null) {
  54. return cached;
  55. }
  56. }
  57. return new Fraction(whole, 1);
  58. }
  59. public static Fraction of(long numerator, long denominator) {
  60. if (denominator == 1)
  61. return ofWhole(numerator);
  62. if (denominator == -1)
  63. return ofWhole(-numerator);
  64. return new Fraction(numerator, denominator);
  65. }
  66. public static Fraction of(long whole, long numerator, long denominator) {
  67. return of(numerator + whole * denominator, denominator);
  68. }
  69. public static Fraction from(double value) {
  70. int whole = (int) value;
  71. double part = value - whole;
  72. int i = 1;
  73. while (true) {
  74. double tem = part / (1D / i);
  75. long numerator = Math.round(tem);
  76. if (Math.abs(tem - numerator) < 0.00001) {
  77. return of(whole, numerator, i);
  78. }
  79. i++;
  80. }
  81. }
  82. public long getNumerator() {
  83. return numerator;
  84. }
  85. public long getDenominator() {
  86. return denominator;
  87. }
  88. public Fraction add(Fraction other) {
  89. if (other.numerator == 0) return this;
  90. return of(numerator * other.denominator + other.numerator * denominator, denominator * other.denominator);
  91. }
  92. public Fraction minus(Fraction other) {
  93. if (other.numerator == 0) return this;
  94. return of(numerator * other.denominator - other.numerator * denominator, denominator * other.denominator);
  95. }
  96. public Fraction multiply(Fraction other) {
  97. if (other.numerator == other.denominator) return this;
  98. return of(numerator * other.numerator, denominator * other.denominator);
  99. }
  100. public Fraction divide(Fraction other) {
  101. if (other.numerator == other.denominator) return this;
  102. return of(numerator * other.denominator, denominator * other.numerator);
  103. }
  104. public Fraction inverse() {
  105. if (numerator == denominator)
  106. return this;
  107. Fraction fraction = of(denominator, numerator);
  108. fraction.simplified = fraction.simplified && this.simplified;
  109. return fraction;
  110. }
  111. public Fraction simplify() {
  112. if (simplified)
  113. return this;
  114. if (numerator == 0)
  115. return ofWhole(0);
  116. long gcd = LongMath.gcd(Math.abs(numerator), denominator);
  117. Fraction fraction = of(numerator / gcd, denominator / gcd);
  118. fraction.simplified = true;
  119. return fraction;
  120. }
  121. public boolean isGreaterThan(Fraction fraction) {
  122. return compareTo(fraction) > 0;
  123. }
  124. public boolean isLessThan(Fraction fraction) {
  125. return compareTo(fraction) < 0;
  126. }
  127. @Override
  128. public boolean equals(Object o) {
  129. if (this == o) return true;
  130. if (o == null || getClass() != o.getClass()) return false;
  131. Fraction fraction = (Fraction) o;
  132. return numerator * fraction.denominator == denominator * fraction.numerator;
  133. }
  134. @Override
  135. public int hashCode() {
  136. return Double.hashCode(doubleValue());
  137. }
  138. @Override
  139. public int compareTo(@NotNull Fraction fraction) {
  140. return Long.compare(numerator * fraction.denominator, denominator * fraction.numerator);
  141. }
  142. @Override
  143. public int intValue() {
  144. return (int) longValue();
  145. }
  146. @Override
  147. public long longValue() {
  148. return numerator / denominator;
  149. }
  150. @Override
  151. public float floatValue() {
  152. return (float) numerator / denominator;
  153. }
  154. @Override
  155. public double doubleValue() {
  156. return (double) numerator / denominator;
  157. }
  158. public String toDecimalString() {
  159. return DECIMAL_FORMAT.format(doubleValue());
  160. }
  161. @Override
  162. public String toString() {
  163. if (intValue() == doubleValue()) return toDecimalString();
  164. return String.format("%s (%d/%d)", toDecimalString(), numerator, denominator);
  165. }
  166. }