Remove the old FractionUtil class containing the parser since we're using exp4j now.
This commit is contained in:
parent
7cba979236
commit
f42e6fa85a
@ -1,53 +0,0 @@
|
|||||||
package net.sf.openrocket.util;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public abstract class FractionUtil {
|
|
||||||
|
|
||||||
|
|
||||||
private final static Pattern fractionPattern = Pattern.compile("(-?\\d+)/(\\d+)");
|
|
||||||
private final static Pattern mixedFractionPattern = Pattern.compile("(-?\\d+)\\s+(\\d+)/(\\d+)");
|
|
||||||
/**
|
|
||||||
* Parse a double from a string supporting fraction formats.
|
|
||||||
*
|
|
||||||
* Will parse fractions specified with '/'. Mixed numbers separated by ' ' (space).
|
|
||||||
* If no fraction is found in the input string, it is parsed with Double.parseDouble()
|
|
||||||
* which my throw the runtime exception java.lang.NumberFormatException.
|
|
||||||
*
|
|
||||||
* Valid input may look like:
|
|
||||||
*
|
|
||||||
* "1/4" = 0.25d
|
|
||||||
* "-1/4" = -0.25d
|
|
||||||
* "1 1/4" = 1.25d
|
|
||||||
* "-1 1/4" = 1.25d
|
|
||||||
* "1.25" = 1.25d
|
|
||||||
*
|
|
||||||
* @param str
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static Double parseFraction( String str ) {
|
|
||||||
|
|
||||||
if ( str == null ) {
|
|
||||||
throw new java.lang.NumberFormatException("null String");
|
|
||||||
}
|
|
||||||
|
|
||||||
Matcher m1 = mixedFractionPattern.matcher(str);
|
|
||||||
if ( m1.find() ) {
|
|
||||||
double wholepart = Double.parseDouble(m1.group(1));
|
|
||||||
double num = Double.parseDouble(m1.group(2));
|
|
||||||
double den = Double.parseDouble(m1.group(3));
|
|
||||||
return wholepart + Math.copySign(num,wholepart) / den;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matcher m2 = fractionPattern.matcher(str);
|
|
||||||
if( m2.find() ) {
|
|
||||||
double num = Double.parseDouble(m2.group(1));
|
|
||||||
double den = Double.parseDouble(m2.group(2));
|
|
||||||
return num / den;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Double.parseDouble(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,58 +0,0 @@
|
|||||||
package net.sf.openrocket.util;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class FractionUtilTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParseFractions() {
|
|
||||||
|
|
||||||
// zeros
|
|
||||||
assertEquals( 0.0d, FractionUtil.parseFraction("0"), 0.0);
|
|
||||||
assertEquals( 0.0d, FractionUtil.parseFraction("-0"), 0.0);
|
|
||||||
assertEquals( 0.0d, FractionUtil.parseFraction("0/2"), 0.0);
|
|
||||||
assertEquals( 0.0d, FractionUtil.parseFraction("-0/2"), 0.0);
|
|
||||||
assertEquals( 0.0d, FractionUtil.parseFraction("0 0/4"), 0.0);
|
|
||||||
assertEquals( 0.0d, FractionUtil.parseFraction("0 -0/4"), 0.0);
|
|
||||||
|
|
||||||
// Simple fraction.
|
|
||||||
assertEquals( 0.25, FractionUtil.parseFraction("1/4"),0.0);
|
|
||||||
|
|
||||||
// ignores leading & trailing spaces
|
|
||||||
assertEquals( 0.25, FractionUtil.parseFraction(" 1/4 "),0.0);
|
|
||||||
|
|
||||||
// non reduced fraction
|
|
||||||
assertEquals( 0.25, FractionUtil.parseFraction("2/8"),0.0);
|
|
||||||
|
|
||||||
// negative number
|
|
||||||
assertEquals( -0.25, FractionUtil.parseFraction("-1/4"),0.0);
|
|
||||||
|
|
||||||
// improper fraction
|
|
||||||
assertEquals( 1.75, FractionUtil.parseFraction("7/4"),0.0);
|
|
||||||
|
|
||||||
// negative improper fraction
|
|
||||||
assertEquals( -1.75, FractionUtil.parseFraction("-7/4"),0.0);
|
|
||||||
|
|
||||||
// two digit numerator & denominators
|
|
||||||
assertEquals( 11d/16d, FractionUtil.parseFraction("11/16)"),0.0);
|
|
||||||
assertEquals( -11d/16d, FractionUtil.parseFraction("-11/16)"),0.0);
|
|
||||||
|
|
||||||
// Mixed fractions
|
|
||||||
assertEquals( 1.25d, FractionUtil.parseFraction("1 1/4"),0.0);
|
|
||||||
assertEquals( -1.25d, FractionUtil.parseFraction("-1 1/4"),0.0);
|
|
||||||
|
|
||||||
// extra spaces
|
|
||||||
assertEquals( 1.25d, FractionUtil.parseFraction(" 1 1/4"),0.0);
|
|
||||||
assertEquals( 1.25d, FractionUtil.parseFraction("1 1/4"),0.0);
|
|
||||||
assertEquals( 1.25d, FractionUtil.parseFraction("1 1/4 "),0.0);
|
|
||||||
|
|
||||||
assertEquals( 2.75d, FractionUtil.parseFraction("2 3/4"),0.0);
|
|
||||||
assertEquals( 15.75d, FractionUtil.parseFraction("15 3/4"),0.0);
|
|
||||||
assertEquals( 2.75d, FractionUtil.parseFraction("1 7/4"),0.0);
|
|
||||||
|
|
||||||
assertEquals( 69d/64d, FractionUtil.parseFraction("1 5/64"),0.0);
|
|
||||||
assertEquals( -69d/64d, FractionUtil.parseFraction("-1 5/64"),0.0);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user