This repository has been archived on 2022-12-21. You can view files and clone it, but cannot push or open issues or pull requests.
sdm04/src/main/java/ch/usi/inf/sdm/sdm04/math/FractionContracts.java

110 lines
3.5 KiB
Java

/* __ __ __ __ __ ___
* \ \ / / \ \ / / __/
* \ \/ / /\ \ \/ / /
* \____/__/ \__\____/__/
*
* Copyright 2014-2017 Vavr, http://vavr.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.usi.inf.sdm.sdm04.math;
import ch.usi.si.codelounge.jsicko.Contract;
import static ch.usi.si.codelounge.jsicko.Contract.old;
@SuppressWarnings("unused") // used by jSicko
public interface FractionContracts extends Contract {
default boolean non_negative_den(final int returns) {
return returns != 0.;
}
@Pure
Fraction negate();
@Pure
int getDenominator();
@Pure
int getNumerator();
@Pure
default boolean check_if_non_min_val() {
return this.getNumerator() != Integer.MIN_VALUE;
}
@Pure
default boolean must_positive(Fraction returns) {
return returns.getNumerator() >= 0;
}
@Pure
default boolean check_if_negative(final Fraction returns) {
if ((this.getNumerator() > 0 && this.getDenominator() > 0) || (this.getNumerator() < 0 && this.getDenominator() < 0)) {
return returns.getNumerator() < 0;
}
return returns.getNumerator() > 0;
}
@Pure
default boolean must_be_reduced(Fraction returns) {
int den = returns.getDenominator();
int num = returns.getNumerator();
if(num==0){
return true;
}
if( num>den){
return (num % den == num);
} else if (num<den && num>0 ) {
return (num%den == num);
}
return true;
}
@Pure
static boolean get_fraction_pre(final int numerator, final int denominator) {
return denominator != 0 && (denominator > 0 ||
(numerator != Integer.MIN_VALUE && denominator != Integer.MIN_VALUE));
}
@Pure
static boolean get_reduced_fraction_pre(final int numerator, final int denominator) {
return denominator != 0 && (numerator != 0 || denominator != Integer.MIN_VALUE);
}
@Pure
static boolean get_fraction_whole_pre(final int whole, final int numerator, final int denominator) {
final long numeratorValue = whole * (long) denominator + (long) (whole > 0 ? 1 : -1) * numerator;
return numerator >= 0 && denominator > 0 &&
numeratorValue >= Integer.MIN_VALUE && numeratorValue <= Integer.MAX_VALUE;
}
@Pure
static boolean get_reduced_fraction_post(final Fraction returns, final int numerator, final int denominator) {
final Fraction f = Fraction.getFraction(numerator, denominator);
return returns.compareTo(f) == 0;
}
@Pure
static boolean get_whole_fraction_post(final Fraction returns, final int whole, final int numerator, final int denominator) {
final long numeratorValue = whole * (long) denominator + (long) (whole < 0 ? -1 : 1) * numerator;
final Fraction calc = Fraction.getFraction((int) numeratorValue, denominator);
return returns.compareTo(calc) == 0;
}
}