@@ -105,6 +105,31 @@ private Fraction(int num, int den) {
105105 }
106106 }
107107
108+ /**
109+ * Private constructor: Instances are created using factory methods.
110+ *
111+ * <p>This constructor should only be invoked when the fraction is known
112+ * to be already reduced, and non-ZERO;
113+ * otherwise use {@link #Fraction(int, int)}, or {@link #ZERO}.
114+ *
115+ * @param num Numerator.
116+ * @param den Denominator.
117+ * @param ignore Ignored, do not really use this, just to make it can overload.
118+ */
119+ private Fraction (int num , int den , boolean ignore ) {
120+ if (den == 0 ) {
121+ throw new FractionException (FractionException .ERROR_ZERO_DENOMINATOR );
122+ }
123+ if (num < 0 && den < 0 ) {
124+ if (num != Integer .MIN_VALUE && den != Integer .MIN_VALUE ) {
125+ num = -num ;
126+ den = -den ;
127+ }
128+ }
129+ numerator = num ;
130+ denominator = den ;
131+ }
132+
108133 /**
109134 * Private constructor: Instances are created using factory methods.
110135 *
@@ -470,8 +495,8 @@ public Fraction abs() {
470495 @ Override
471496 public Fraction negate () {
472497 return numerator == Integer .MIN_VALUE ?
473- new Fraction (numerator , -denominator ) :
474- new Fraction (-numerator , denominator );
498+ new Fraction (numerator , -denominator , false ) :
499+ new Fraction (-numerator , denominator , false );
475500 }
476501
477502 /**
@@ -483,7 +508,7 @@ public Fraction negate() {
483508 */
484509 @ Override
485510 public Fraction reciprocal () {
486- return new Fraction (denominator , numerator );
511+ return new Fraction (denominator , numerator , false );
487512 }
488513
489514 /**
@@ -581,7 +606,7 @@ public Fraction subtract(final int value) {
581606 if (isZero ()) {
582607 // Special case for min value
583608 return value == Integer .MIN_VALUE ?
584- new Fraction (Integer .MIN_VALUE , -1 ) :
609+ new Fraction (Integer .MIN_VALUE , -1 , false ) :
585610 new Fraction (-value );
586611 }
587612 // Convert to numerator with same effective denominator
@@ -674,7 +699,7 @@ public Fraction multiply(final int value) {
674699 // (see multiply(Fraction) using value / 1 as the argument).
675700 final int d2 = ArithmeticUtils .gcd (value , denominator );
676701 return new Fraction (Math .multiplyExact (numerator , value / d2 ),
677- denominator / d2 );
702+ denominator / d2 , false );
678703 }
679704
680705 /**
@@ -740,7 +765,7 @@ public Fraction divide(final int value) {
740765 // (see multiply(Fraction) using 1 / value as the argument).
741766 final int d1 = ArithmeticUtils .gcd (numerator , value );
742767 return new Fraction (numerator / d1 ,
743- Math .multiplyExact (denominator , value / d1 ));
768+ Math .multiplyExact (denominator , value / d1 ), false );
744769 }
745770
746771 /**
@@ -789,18 +814,18 @@ public Fraction pow(final int exponent) {
789814 }
790815 if (exponent > 0 ) {
791816 return new Fraction (ArithmeticUtils .pow (numerator , exponent ),
792- ArithmeticUtils .pow (denominator , exponent ));
817+ ArithmeticUtils .pow (denominator , exponent ), false );
793818 }
794819 if (exponent == -1 ) {
795820 return this .reciprocal ();
796821 }
797822 if (exponent == Integer .MIN_VALUE ) {
798823 // MIN_VALUE can't be negated
799824 return new Fraction (ArithmeticUtils .pow (denominator , Integer .MAX_VALUE ) * denominator ,
800- ArithmeticUtils .pow (numerator , Integer .MAX_VALUE ) * numerator );
825+ ArithmeticUtils .pow (numerator , Integer .MAX_VALUE ) * numerator , false );
801826 }
802827 return new Fraction (ArithmeticUtils .pow (denominator , -exponent ),
803- ArithmeticUtils .pow (numerator , -exponent ));
828+ ArithmeticUtils .pow (numerator , -exponent ), false );
804829 }
805830
806831 /**
0 commit comments