summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/unit/LinearUnitValue.java
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2020-09-07 15:15:20 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2020-09-07 15:15:20 -0500
commit5a7e8f6fcb175b238eb1d5481513b35039107a3e (patch)
tree3078cd98b290fbab46ea9a6cc181e3e18e3f62e7 /src/org/unitConverter/unit/LinearUnitValue.java
parent21dc2db1cccfd9781bbdbbbafdacf88630cab5c0 (diff)
Created an UncertainDouble class for uncertainty operations.
Diffstat (limited to 'src/org/unitConverter/unit/LinearUnitValue.java')
-rw-r--r--src/org/unitConverter/unit/LinearUnitValue.java252
1 files changed, 56 insertions, 196 deletions
diff --git a/src/org/unitConverter/unit/LinearUnitValue.java b/src/org/unitConverter/unit/LinearUnitValue.java
index 5685a6d..d86d344 100644
--- a/src/org/unitConverter/unit/LinearUnitValue.java
+++ b/src/org/unitConverter/unit/LinearUnitValue.java
@@ -3,12 +3,11 @@
*/
package org.unitConverter.unit;
-import java.math.BigDecimal;
-import java.math.RoundingMode;
import java.util.Objects;
import java.util.Optional;
import org.unitConverter.math.DecimalComparison;
+import org.unitConverter.math.UncertainDouble;
/**
* A possibly uncertain value expressed in a linear unit.
@@ -33,7 +32,8 @@ public final class LinearUnitValue {
public static final LinearUnitValue getExact(final LinearUnit unit,
final double value) {
return new LinearUnitValue(
- Objects.requireNonNull(unit, "unit must not be null"), value, 0);
+ Objects.requireNonNull(unit, "unit must not be null"),
+ UncertainDouble.of(value, 0));
}
/**
@@ -46,41 +46,23 @@ public final class LinearUnitValue {
* @since 2020-07-26
*/
public static final LinearUnitValue of(final LinearUnit unit,
- final double value, final double uncertainty) {
+ final UncertainDouble value) {
return new LinearUnitValue(
- Objects.requireNonNull(unit, "unit must not be null"), value,
- uncertainty);
- }
-
- /**
- * Gets an uncertain {@code LinearUnitValue}
- *
- * @param unit unit to express with
- * @param value value to express
- * @param relativeUncertainty relative uncertainty of value
- * @return uncertain {@code LinearUnitValue} instance
- * @since 2020-07-28
- */
- public static final LinearUnitValue ofRelative(final LinearUnit unit,
- final double value, final double relativeUncertainty) {
- return LinearUnitValue.of(unit, value, relativeUncertainty * value);
+ Objects.requireNonNull(unit, "unit must not be null"),
+ Objects.requireNonNull(value, "value may not be null"));
}
private final LinearUnit unit;
- private final double value;
- private final double uncertainty;
+ private final UncertainDouble value;
/**
- * @param unit unit to express as
- * @param value value to express
- * @param uncertainty absolute uncertainty of value
+ * @param unit unit to express as
+ * @param value value to express
* @since 2020-07-26
*/
- private LinearUnitValue(final LinearUnit unit, final double value,
- final double uncertainty) {
+ private LinearUnitValue(final LinearUnit unit, final UncertainDouble value) {
this.unit = unit;
this.value = value;
- this.uncertainty = uncertainty;
}
/**
@@ -89,7 +71,7 @@ public final class LinearUnitValue {
* @since 2020-08-04
*/
public final UnitValue asUnitValue() {
- return UnitValue.of(this.unit, this.value);
+ return UnitValue.of(this.unit, this.value.value());
}
/**
@@ -110,8 +92,7 @@ public final class LinearUnitValue {
* @since 2020-07-26
*/
public final LinearUnitValue convertTo(final LinearUnit other) {
- return LinearUnitValue.of(other, this.unit.convertTo(other, this.value),
- this.unit.convertTo(other, this.uncertainty));
+ return LinearUnitValue.of(other, this.unit.convertTo(other, this.value));
}
/**
@@ -122,8 +103,7 @@ public final class LinearUnitValue {
* @since 2020-07-28
*/
public LinearUnitValue dividedBy(final double divisor) {
- return LinearUnitValue.of(this.unit, this.value / divisor,
- this.uncertainty / divisor);
+ return LinearUnitValue.of(this.unit, this.value.dividedByExact(divisor));
}
/**
@@ -134,10 +114,8 @@ public final class LinearUnitValue {
* @since 2020-07-28
*/
public LinearUnitValue dividedBy(final LinearUnitValue divisor) {
- return LinearUnitValue.ofRelative(this.unit.dividedBy(divisor.unit),
- this.value / divisor.value,
- Math.hypot(this.getRelativeUncertainty(),
- divisor.getRelativeUncertainty()));
+ return LinearUnitValue.of(this.unit.dividedBy(divisor.unit),
+ this.value.dividedBy(divisor.value));
}
/**
@@ -154,12 +132,8 @@ public final class LinearUnitValue {
return false;
final LinearUnitValue other = (LinearUnitValue) obj;
return Objects.equals(this.unit.getBase(), other.unit.getBase())
- && Double.doubleToLongBits(
- this.unit.convertToBase(this.getValue())) == Double
- .doubleToLongBits(
- other.unit.convertToBase(other.getValue()))
- && Double.doubleToLongBits(this.getRelativeUncertainty()) == Double
- .doubleToLongBits(other.getRelativeUncertainty());
+ && this.unit.convertToBase(this.value)
+ .equals(other.unit.convertToBase(other.value));
}
/**
@@ -180,9 +154,7 @@ public final class LinearUnitValue {
final LinearUnitValue other = (LinearUnitValue) obj;
return Objects.equals(this.unit.getBase(), other.unit.getBase())
&& DecimalComparison.equals(this.unit.convertToBase(this.value),
- other.unit.convertToBase(other.value))
- && DecimalComparison.equals(this.getRelativeUncertainty(),
- other.getRelativeUncertainty());
+ other.unit.convertToBase(other.value));
}
/**
@@ -195,32 +167,11 @@ public final class LinearUnitValue {
if (other == null
|| !Objects.equals(this.unit.getBase(), other.unit.getBase()))
return false;
- final double thisBaseValue = this.unit.convertToBase(this.value);
- final double otherBaseValue = other.unit.convertToBase(other.value);
- final double thisBaseUncertainty = this.unit
- .convertToBase(this.uncertainty);
- final double otherBaseUncertainty = other.unit
- .convertToBase(other.uncertainty);
- return Math.abs(thisBaseValue - otherBaseValue) <= Math
- .min(thisBaseUncertainty, otherBaseUncertainty);
- }
-
- /**
- * @return relative uncertainty of value
- *
- * @since 2020-07-26
- */
- public final double getRelativeUncertainty() {
- return this.uncertainty / this.value;
- }
-
- /**
- * @return absolute uncertainty of value
- *
- * @since 2020-07-26
- */
- public final double getUncertainty() {
- return this.uncertainty;
+ final LinearUnit base = LinearUnit.valueOf(this.unit.getBase(), 1);
+ final LinearUnitValue thisBase = this.convertTo(base);
+ final LinearUnitValue otherBase = other.convertTo(base);
+
+ return thisBase.value.equivalent(otherBase.value);
}
/**
@@ -237,24 +188,22 @@ public final class LinearUnitValue {
*
* @since 2020-07-26
*/
- public final double getValue() {
+ public final UncertainDouble getValue() {
return this.value;
}
+ /**
+ * @return the exact value
+ * @since 2020-09-07
+ */
+ public final double getValueExact() {
+ return this.value.value();
+ }
+
@Override
public int hashCode() {
return Objects.hash(this.unit.getBase(),
- this.unit.convertToBase(this.getValue()),
- this.getRelativeUncertainty());
- }
-
- /**
- * @return true iff the value has no uncertainty
- *
- * @since 2020-07-26
- */
- public final boolean isExact() {
- return this.uncertainty == 0;
+ this.unit.convertToBase(this.getValue()));
}
/**
@@ -276,8 +225,8 @@ public final class LinearUnitValue {
this.unit, subtrahend.unit));
final LinearUnitValue otherConverted = subtrahend.convertTo(this.unit);
- return LinearUnitValue.of(this.unit, this.value - otherConverted.value,
- Math.hypot(this.uncertainty, otherConverted.uncertainty));
+ return LinearUnitValue.of(this.unit,
+ this.value.minus(otherConverted.value));
}
/**
@@ -298,8 +247,8 @@ public final class LinearUnitValue {
addend.unit));
final LinearUnitValue otherConverted = addend.convertTo(this.unit);
- return LinearUnitValue.of(this.unit, this.value + otherConverted.value,
- Math.hypot(this.uncertainty, otherConverted.uncertainty));
+ return LinearUnitValue.of(this.unit,
+ this.value.plus(otherConverted.value));
}
/**
@@ -310,8 +259,7 @@ public final class LinearUnitValue {
* @since 2020-07-28
*/
public LinearUnitValue times(final double multiplier) {
- return LinearUnitValue.of(this.unit, this.value * multiplier,
- this.uncertainty * multiplier);
+ return LinearUnitValue.of(this.unit, this.value.timesExact(multiplier));
}
/**
@@ -322,10 +270,8 @@ public final class LinearUnitValue {
* @since 2020-07-28
*/
public LinearUnitValue times(final LinearUnitValue multiplier) {
- return LinearUnitValue.ofRelative(this.unit.times(multiplier.unit),
- this.value * multiplier.value,
- Math.hypot(this.getRelativeUncertainty(),
- multiplier.getRelativeUncertainty()));
+ return LinearUnitValue.of(this.unit.times(multiplier.unit),
+ this.value.times(multiplier.value));
}
/**
@@ -336,14 +282,13 @@ public final class LinearUnitValue {
* @since 2020-07-28
*/
public LinearUnitValue toExponent(final int exponent) {
- return LinearUnitValue.ofRelative(this.unit.toExponent(exponent),
- Math.pow(this.value, exponent),
- this.getRelativeUncertainty() * Math.sqrt(exponent));
+ return LinearUnitValue.of(this.unit.toExponent(exponent),
+ this.value.toExponentExact(exponent));
}
@Override
public String toString() {
- return this.toString(!this.isExact());
+ return this.toString(!this.value.isExact());
}
/**
@@ -363,107 +308,22 @@ public final class LinearUnitValue {
final Optional<String> symbol = this.unit.getSymbol();
final String chosenName = symbol.orElse(primaryName.orElse(null));
- final double baseValue = this.unit.convertToBase(this.value);
- final double baseUncertainty = this.unit.convertToBase(this.uncertainty);
+ final UncertainDouble baseValue = this.unit.convertToBase(this.value);
// get rounded strings
- String valueString, baseValueString, uncertaintyString,
- baseUncertaintyString;
- if (this.isExact()) {
- valueString = Double.toString(this.value);
- baseValueString = Double.toString(baseValue);
- uncertaintyString = "0";
- baseUncertaintyString = "0";
- } else {
- final BigDecimal bigValue = BigDecimal.valueOf(this.value);
- final BigDecimal bigUncertainty = BigDecimal.valueOf(this.uncertainty);
-
- // round based on uncertainty
- // if uncertainty starts with 1 (ignoring zeroes and the decimal
- // point), rounds
- // so that uncertainty has 2 significant digits.
- // otherwise, rounds so that uncertainty has 1 significant digits.
- // the value is rounded to the same number of decimal places as the
- // uncertainty.
- BigDecimal roundedUncertainty = bigUncertainty.setScale(
- bigUncertainty.scale() - bigUncertainty.precision() + 2,
- RoundingMode.HALF_EVEN);
- if (roundedUncertainty.unscaledValue().intValue() >= 20) {
- roundedUncertainty = bigUncertainty.setScale(
- bigUncertainty.scale() - bigUncertainty.precision() + 1,
- RoundingMode.HALF_EVEN);
- }
- final BigDecimal roundedValue = bigValue
- .setScale(roundedUncertainty.scale(), RoundingMode.HALF_EVEN);
-
- valueString = roundedValue.toString();
- uncertaintyString = roundedUncertainty.toString();
-
- if (primaryName.isEmpty() && symbol.isEmpty()) {
- final BigDecimal bigBaseValue = BigDecimal.valueOf(baseValue);
- final BigDecimal bigBaseUncertainty = BigDecimal
- .valueOf(baseUncertainty);
-
- BigDecimal roundedBaseUncertainty = bigBaseUncertainty
- .setScale(
- bigBaseUncertainty.scale()
- - bigBaseUncertainty.precision() + 2,
- RoundingMode.HALF_EVEN);
- if (roundedBaseUncertainty.unscaledValue().intValue() >= 20) {
- roundedBaseUncertainty = bigBaseUncertainty
- .setScale(
- bigBaseUncertainty.scale()
- - bigBaseUncertainty.precision() + 1,
- RoundingMode.HALF_EVEN);
- }
- final BigDecimal roundedBaseValue = bigBaseValue.setScale(
- roundedBaseUncertainty.scale(), RoundingMode.HALF_EVEN);
-
- baseValueString = roundedBaseValue.toString();
- baseUncertaintyString = roundedBaseUncertainty.toString();
- } else {
- // unused
- baseValueString = "";
- baseUncertaintyString = "";
- }
- }
+ // if showUncertainty is true, add brackets around the string
+ final String valueString = showUncertainty ? "("
+ : "" + this.value.toString(showUncertainty)
+ + (showUncertainty ? ")" : "");
+ final String baseValueString = showUncertainty ? "("
+ : "" + baseValue.toString(showUncertainty)
+ + (showUncertainty ? ")" : "");
// create string
- if (showUncertainty) {
- if (primaryName.isEmpty() && symbol.isEmpty())
- return String.format("(%s ± %s) unnamed unit (= %s ± %s %s)",
- valueString, uncertaintyString, baseValueString,
- baseUncertaintyString, this.unit.getBase());
- else
- return String.format("(%s ± %s) %s", valueString, uncertaintyString,
- chosenName);
- } else {
- // truncate excess zeroes
- if (valueString.contains(".")) {
- while (valueString.endsWith("0")) {
- valueString = valueString.substring(0, valueString.length() - 1);
- }
- if (valueString.endsWith(".")) {
- valueString = valueString.substring(0, valueString.length() - 1);
- }
- }
-
- if (baseValueString.contains(".")) {
- while (baseValueString.endsWith("0")) {
- baseValueString = baseValueString.substring(0,
- baseValueString.length() - 1);
- }
- if (baseValueString.endsWith(".")) {
- baseValueString = baseValueString.substring(0,
- baseValueString.length() - 1);
- }
- }
-
- if (primaryName.isEmpty() && symbol.isEmpty())
- return String.format("%s unnamed unit (= %s %s)", valueString,
- baseValueString, this.unit.getBase());
- else
- return String.format("%s %s", valueString, chosenName);
- }
+ if (primaryName.isEmpty() && symbol.isEmpty())
+ return String.format("%s unnamed unit (= %s %s)", valueString,
+ baseValueString, this.unit.getBase());
+ else
+ return String.format("%s %s", valueString, chosenName);
}
}