org.apache.commons.lang3.math.Fraction Java Examples

The following examples show how to use org.apache.commons.lang3.math.Fraction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PeriodicMarkerEventSource.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
private PeriodicMarkerEventSource(String category, Reference reference, double period, long rollover, boolean foreground, RGBA color1, @Nullable RGBA color2) {
    if (period <= 0) {
        throw new IllegalArgumentException("period cannot be less than or equal to zero"); //$NON-NLS-1$
    }
    if (rollover < 0) {
        throw new IllegalArgumentException("rollover cannot be less than zero"); //$NON-NLS-1$
    }
    fCategory = category;
    fReference = reference;
    fPeriod = period;
    fPeriodInteger = (long) period;
    try {
        fPeriodFraction = Fraction.getFraction(fPeriod - fPeriodInteger);
    } catch (ArithmeticException e) {
        /* can't convert to fraction, use floating-point arithmetic */
        fPeriodFraction = null;
    }
    fRollover = rollover;
    fColor1 = color1;
    fColor2 = color2;
    fForeground = foreground;
}
 
Example #2
Source File: PeriodicMarkerEventSource.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
private Reference adjustReference(Reference baseReference, long time) {
    long offsetIndex = (long) ((time - baseReference.time) / fPeriod);
    long offsetTime = 0;
    Fraction fraction = fPeriodFraction;
    if (fraction != null) {
        /*
         * If period = int num/den, find an offset index that is an exact
         * multiple of den and calculate index * period = (index * int) +
         * (index / den * num), all exact calculations.
         */
        offsetIndex = offsetIndex - offsetIndex % fraction.getDenominator();
        offsetTime = offsetIndex * fPeriodInteger + offsetIndex / fraction.getDenominator() * fraction.getNumerator();
    } else {
        /*
         * Couldn't compute fractional part as fraction, use simple
         * multiplication but with possible rounding error.
         */
        offsetTime = Math.round(offsetIndex * fPeriod);
    }
    Reference reference = new Reference(baseReference.time + offsetTime, baseReference.index + offsetIndex);
    return reference;
}
 
Example #3
Source File: PCGenTaskExecutor.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void run()
{
	progressMultiplier = Fraction.getFraction(1, tasks.size());
	while (!tasks.isEmpty())
	{
		currentTask = tasks.poll();
		setValues(currentTask.getMessage(), baseProgress.getNumerator(), baseProgress.getDenominator());
		currentTask.addPCGenTaskListener(this);
		currentTask.run();
		currentTask.removePCGenTaskListener(this);
		baseProgress = baseProgress.add(progressMultiplier);
	}
}
 
Example #4
Source File: PCGenTaskExecutor.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void progressChanged(PCGenTaskEvent event)
{
	if (currentTask.getMaximum() == 0)
	{
		return;
	}
	Fraction progress = Fraction.getFraction(currentTask.getProgress(), currentTask.getMaximum());
	progress = progress.multiplyBy(progressMultiplier);
	progress = baseProgress.add(progress);
	setValues(currentTask.getMessage(), progress.getNumerator(), progress.getDenominator());
}
 
Example #5
Source File: PCGenTaskExecutor.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void run()
{
	progressMultiplier = Fraction.getFraction(1, tasks.size());
	while (!tasks.isEmpty())
	{
		currentTask = tasks.poll();
		setValues(currentTask.getMessage(), baseProgress.getNumerator(), baseProgress.getDenominator());
		currentTask.addPCGenTaskListener(this);
		currentTask.run();
		currentTask.removePCGenTaskListener(this);
		baseProgress = baseProgress.add(progressMultiplier);
	}
}
 
Example #6
Source File: PCGenTaskExecutor.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void progressChanged(PCGenTaskEvent event)
{
	if (currentTask.getMaximum() == 0)
	{
		return;
	}
	Fraction progress = Fraction.getFraction(currentTask.getProgress(), currentTask.getMaximum());
	progress = progress.multiplyBy(progressMultiplier);
	progress = baseProgress.add(progress);
	setValues(currentTask.getMessage(), progress.getNumerator(), progress.getDenominator());
}
 
Example #7
Source File: FractionUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenFractionClass_whenCalledgetFraction_thenCorrect() {
    assertThat(Fraction.getFraction(5, 6)).isInstanceOf(Fraction.class);
}
 
Example #8
Source File: FractionUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenTwoFractionInstances_whenCalledadd_thenCorrect() {
    Fraction fraction1 = Fraction.getFraction(1, 4);
    Fraction fraction2 = Fraction.getFraction(3, 4);
    assertThat(fraction1.add(fraction2).toString()).isEqualTo("1/1");
}
 
Example #9
Source File: FractionUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenTwoFractionInstances_whenCalledsubstract_thenCorrect() {
    Fraction fraction1 = Fraction.getFraction(3, 4);
    Fraction fraction2 = Fraction.getFraction(1, 4);
    assertThat(fraction1.subtract(fraction2).toString()).isEqualTo("1/2");
}
 
Example #10
Source File: FractionUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void givenTwoFractionInstances_whenCalledmultiply_thenCorrect() {
    Fraction fraction1 = Fraction.getFraction(3, 4);
    Fraction fraction2 = Fraction.getFraction(1, 4);
    assertThat(fraction1.multiplyBy(fraction2).toString()).isEqualTo("3/16");
}
 
Example #11
Source File: CalcState.java    From OpenModsLib with MIT License 3 votes vote down vote up
@Override
public Calculator<?, ExprType> newCalculator(final SenderHolder holder) {
	final Calculator<Fraction, ExprType> calculator = FractionCalculatorFactory.createDefault();

	calculator.environment.setGlobalSymbol("_x", () -> Fraction.getFraction(holder.getX(), 1));

	calculator.environment.setGlobalSymbol("_y", () -> Fraction.getFraction(holder.getY(), 1));

	calculator.environment.setGlobalSymbol("_z", () -> Fraction.getFraction(holder.getZ(), 1));

	return calculator;
}