[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

@ -1,12 +1,12 @@
/* /*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch> * Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -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,12 +63,12 @@ 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);
} }
/** /**
* Sets the value to 0 * Sets the value to 0
* *
* @see Parameter#clear() * @see Parameter#clear()
*/ */
@Override @Override

View File

@ -1,12 +1,12 @@
/* /*
* Copyright 2013 Martin Smock <smock.martin@gmail.com> * Copyright 2013 Martin Smock <smock.martin@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -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

@ -1,12 +1,12 @@
/* /*
* Copyright 2013 Martin Smock <smock.martin@gmail.com> * Copyright 2013 Martin Smock <smock.martin@gmail.com>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -20,7 +20,7 @@ import java.math.RoundingMode;
/** /**
* A helper class that contains mathematical computations that can be used throughout. * A helper class that contains mathematical computations that can be used throughout.
* *
* @author Martin Smock &lt;smock.martin@gmail.com&gt; * @author Martin Smock &lt;smock.martin@gmail.com&gt;
* @author Michael Gatto &lt;michael@gatto.ch&gt; * @author Michael Gatto &lt;michael@gatto.ch&gt;
*/ */
@ -31,15 +31,15 @@ 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));
} }
@ -49,17 +49,16 @@ public class MathHelper {
* <p> * <p>
* Note: this implementation tests if the value < bound, and if this is not so, checks if the values are equal under * Note: this implementation tests if the value < bound, and if this is not so, checks if the values are equal under
* 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);
} }
/** /**
@ -68,11 +67,12 @@ public class MathHelper {
* <p> * <p>
* Note: This implementation tests if value > bound and, if it is so, if equality does NOT hold. Thus, it is * Note: This implementation tests if value > bound and, if it is so, if equality does NOT hold. Thus, it is
* 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) {
@ -83,7 +83,7 @@ public class MathHelper {
* <p> * <p>
* Rounds the given double value to the number of decimals * Rounds the given double value to the number of decimals
* </p> * </p>
* *
* <p> * <p>
* <b>Warning:</b> Do not use the returned value for further calculations. Always finish calculates and use one of * <b>Warning:</b> Do not use the returned value for further calculations. Always finish calculates and use one of
* the following methods: * the following methods:
@ -94,12 +94,12 @@ public class MathHelper {
* </ul> * </ul>
* to test on equality or greater than/ smaller than * to test on equality or greater than/ smaller than
* </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
*/ */
public static double toPrecision(double value, int decimals) { public static double toPrecision(double value, int decimals) {
@ -116,7 +116,7 @@ public class MathHelper {
/** /**
* Returns the value with the precision where precision is set to {@link #PRECISION_DIGITS} * Returns the value with the precision where precision is set to {@link #PRECISION_DIGITS}
* *
* @see #toPrecision(double, int) * @see #toPrecision(double, int)
*/ */
public static double toPrecision(double value) { public static double toPrecision(double value) {