[Minor] round to 8 decimals in FloatParameter and FloatValue

This commit is contained in:
Robert von Burg 2018-01-19 11:12:32 +01:00
parent 4de72bfe8b
commit 00e41fdc4a
3 changed files with 34 additions and 33 deletions

View File

@ -18,10 +18,10 @@ package li.strolch.model.parameter;
import li.strolch.model.StrolchValueType; import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor; import li.strolch.model.visitor.ParameterVisitor;
import li.strolch.utils.dbc.DBC; import li.strolch.utils.dbc.DBC;
import li.strolch.utils.helper.MathHelper;
/** /**
* @author Robert von Burg <eitch@eitchnet.ch> * @author Robert von Burg <eitch@eitchnet.ch>
*
*/ */
public class FloatParameter extends AbstractParameter<Double> { public class FloatParameter extends AbstractParameter<Double> {
@ -63,7 +63,7 @@ public class FloatParameter extends AbstractParameter<Double> {
@Override @Override
public void setValue(Double value) { public void setValue(Double value) {
validateValue(value); validateValue(value);
this.value = value; this.value = MathHelper.toPrecision(value, 8);
} }
/** /**

View File

@ -20,6 +20,7 @@ import java.io.Serializable;
import li.strolch.model.StrolchValueType; import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.ITimeValue; import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue; import li.strolch.model.timevalue.IValue;
import li.strolch.utils.helper.MathHelper;
/** /**
* {@link IValue} implementation to work with Double valued {@link ITimeValue} objects * {@link IValue} implementation to work with Double valued {@link ITimeValue} objects
@ -61,7 +62,7 @@ public class FloatValue implements IValue<Double>, Serializable {
@Override @Override
public FloatValue add(Double o) { public FloatValue add(Double o) {
this.value += o; this.value = MathHelper.toPrecision(this.value + o, 8);
return this; return this;
} }

View File

@ -33,13 +33,13 @@ public class MathHelper {
* Check if the two values are equal with respect to the precision * Check if the two values are equal with respect to the precision
* *
* @param firstValue * @param firstValue
* the first value to compare * the first value to compare
* @param secondValue * @param secondValue
* the second value to compare to * the second value to compare to
*
* @return boolean True, if the two values are equal under the set precision. Fales, otherwise. * @return boolean True, if the two values are equal under the set precision. Fales, otherwise.
*/ */
public static boolean isEqualPrecision(double firstValue, double secondValue) { public static boolean isEqualPrecision(double firstValue, double secondValue) {
return (java.lang.Math.abs(firstValue - secondValue) < (1.0d / PRECISION)); return (java.lang.Math.abs(firstValue - secondValue) < (1.0d / PRECISION));
} }
@ -51,15 +51,14 @@ public class MathHelper {
* the precision. Thus, it's efficient whenever the value is expected to be smaller than the bound. * the precision. Thus, it's efficient whenever the value is expected to be smaller than the bound.
* *
* @param value * @param value
* The value to check * The value to check
* @param bound * @param bound
* The bound given * The bound given
*
* @return true, if value < bound under the given precision. False, otherwise. * @return true, if value < bound under the given precision. False, otherwise.
*/ */
public static boolean isSmallerEqualPrecision(double value, double bound) { public static boolean isSmallerEqualPrecision(double value, double bound) {
if (value < bound) return value < bound || isEqualPrecision(value, bound);
return true;
return isEqualPrecision(value, bound);
} }
/** /**
@ -70,9 +69,10 @@ public class MathHelper {
* efficient whenever the value is not expected to be greater than the bound. * efficient whenever the value is not expected to be greater than the bound.
* *
* @param value * @param value
* The value to check * The value to check
* @param bound * @param bound
* The bound given. * The bound given.
*
* @return true, if value > bound and the values do not test equal under precision. False, otherwise. * @return true, if value > bound and the values do not test equal under precision. False, otherwise.
*/ */
public static boolean isGreaterPrecision(double value, double bound) { public static boolean isGreaterPrecision(double value, double bound) {
@ -96,9 +96,9 @@ public class MathHelper {
* </p> * </p>
* *
* @param value * @param value
* the double value to round * the double value to round
* @param decimals * @param decimals
* number of decimals * number of decimals
* *
* @return the rounded number * @return the rounded number
*/ */