Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions test/test_uncertain_measurements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,78 @@ TEST(uncertainOps, testHeight)
EXPECT_EQ(ys.units(), m);
}

TEST(uncertainOps, negativeValues)
{
uncertain_measurement x(-2.0, 0.1, m);
uncertain_measurement y(3.0, 0.2, m);

auto z1 = x * y;
EXPECT_FLOAT_EQ(z1.value(), -6.0F);
EXPECT_FLOAT_EQ(z1.uncertainty(), 0.5F);
EXPECT_TRUE(z1 == measurement(-6.0, m * m));

auto z2 = x / y;
EXPECT_FLOAT_EQ(z2.value(), -2.0F / 3.0F);
EXPECT_FLOAT_EQ(z2.uncertainty(), 0.5F / 9.0F);

auto zs1 = x.simple_product(y);
EXPECT_FLOAT_EQ(zs1.value(), -6.0F);
EXPECT_FLOAT_EQ(zs1.uncertainty(), 0.7F);

auto zs2 = x.simple_divide(y);
EXPECT_FLOAT_EQ(zs2.value(), -2.0F / 3.0F);
EXPECT_FLOAT_EQ(zs2.uncertainty(), 0.7F / 9.0F);

EXPECT_FLOAT_EQ((y * -2.0).uncertainty(), 0.4F);
EXPECT_FLOAT_EQ((y * -2.0F).uncertainty(), 0.4F);
EXPECT_FLOAT_EQ((-2.0 * y).uncertainty(), 0.4F);
EXPECT_FLOAT_EQ((y * measurement(-2.0, m)).uncertainty(), 0.4F);
EXPECT_FLOAT_EQ((y / -2.0).uncertainty(), 0.1F);
EXPECT_FLOAT_EQ((y / -2.0F).uncertainty(), 0.1F);
EXPECT_FLOAT_EQ((y / measurement(-2.0, m)).uncertainty(), 0.1F);
EXPECT_FLOAT_EQ((-6.0 / y).uncertainty(), 0.4F / 3.0F);
EXPECT_FLOAT_EQ((-6.0F / y).uncertainty(), 0.4F / 3.0F);
EXPECT_FLOAT_EQ((-6 / y).uncertainty(), 0.4F / 3.0F);
EXPECT_FLOAT_EQ((measurement(-6.0, m) / y).uncertainty(), 0.4F / 3.0F);

auto p2 = pow(x, 2);
EXPECT_FLOAT_EQ(p2.value(), 4.0F);
EXPECT_FLOAT_EQ(p2.uncertainty(), 0.4F);
auto pm1 = pow(x, -1);
EXPECT_FLOAT_EQ(pm1.value(), -0.5F);
EXPECT_FLOAT_EQ(pm1.uncertainty(), 0.025F);
}

TEST(uncertainOps, zeroValue)
{
uncertain_measurement x(0.0, 0.1, m);
uncertain_measurement y(3.0, 0.2, m);

auto z1 = x * y;
EXPECT_FLOAT_EQ(z1.value(), 0.0F);
EXPECT_FLOAT_EQ(z1.uncertainty(), 0.3F);

auto z2 = y * x;
EXPECT_FLOAT_EQ(z2.value(), 0.0F);
EXPECT_FLOAT_EQ(z2.uncertainty(), 0.3F);

auto z3 = x / y;
EXPECT_FLOAT_EQ(z3.value(), 0.0F);
EXPECT_FLOAT_EQ(z3.uncertainty(), 0.1F / 3.0F);

auto zs1 = x.simple_product(y);
EXPECT_FLOAT_EQ(zs1.uncertainty(), 0.3F);
auto zs2 = x.simple_divide(y);
EXPECT_FLOAT_EQ(zs2.uncertainty(), 0.1F / 3.0F);

auto p1 = pow(x, 1);
EXPECT_FLOAT_EQ(p1.value(), 0.0F);
EXPECT_FLOAT_EQ(p1.uncertainty(), 0.1F);
auto p2 = pow(x, 2);
EXPECT_FLOAT_EQ(p2.value(), 0.0F);
EXPECT_FLOAT_EQ(p2.uncertainty(), 0.0F);
}

#ifndef UNITS_HEADER_ONLY
TEST(uncertainStrings, fromString)
{
Expand Down
60 changes: 35 additions & 25 deletions units/units.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,30 +859,31 @@ class uncertain_measurement {
* of squares(rss) method*/
uncertain_measurement operator*(const uncertain_measurement& other) const
{
const float tval1 = uncertainty_ / value_;
const float tval2 = other.uncertainty_ / other.value_;
const float tval1 = uncertainty_ * other.value_;
const float tval2 = other.uncertainty_ * value_;
const float ntol = std::sqrt((tval1 * tval1) + (tval2 * tval2));
const float nval = value_ * other.value_;
return {nval, nval * ntol, units_ * other.units()};
return {nval, ntol, units_ * other.units()};
}

/** Perform a multiplication with uncertain measurements using the simple
* method for uncertainty propagation*/
UNITS_CPP14_CONSTEXPR_METHOD uncertain_measurement
simple_product(const uncertain_measurement& other) const
{
const float ntol =
(uncertainty_ / value_) + (other.uncertainty_ / other.value_);
const float ntol = detail::absolute_value(uncertainty_ * other.value_) +
detail::absolute_value(other.uncertainty_ * value_);
const float nval = value_ * other.value_;
return {nval, nval * ntol, units_ * other.units()};
return {nval, ntol, units_ * other.units()};
}
/** Multiply with another measurement
equivalent to uncertain_measurement multiplication with 0 uncertainty*/
constexpr uncertain_measurement operator*(const measurement& other) const
{
return {
static_cast<float>(value() * other.value()),
static_cast<float>(other.value() * uncertainty()),
static_cast<float>(
detail::absolute_value(other.value()) * uncertainty()),
units_ * other.units()};
}
constexpr uncertain_measurement operator*(const unit& other) const
Expand All @@ -891,23 +892,24 @@ class uncertain_measurement {
}
constexpr uncertain_measurement operator*(float val) const
{
return {value_ * val, uncertainty_ * val, units_};
return {
value_ * val, uncertainty_ * detail::absolute_value(val), units_};
}

constexpr uncertain_measurement operator*(double val) const
{
return uncertain_measurement(
value() * val, uncertainty() * val, units_);
value() * val, uncertainty() * detail::absolute_value(val), units_);
}
/** compute a unit division and calculate the new uncertainties using the
* root sum of squares(rss) method*/
uncertain_measurement operator/(const uncertain_measurement& other) const
{
const float tval1 = uncertainty_ / value_;
const float tval2 = other.uncertainty_ / other.value_;
const float ntol = std::sqrt((tval1 * tval1) + (tval2 * tval2));
const float nval = value_ / other.value_;
return {nval, nval * ntol, units_ / other.units()};
const float tval1 = uncertainty_ / other.value_;
const float tval2 = nval * other.uncertainty_ / other.value_;
const float ntol = std::sqrt((tval1 * tval1) + (tval2 * tval2));
return {nval, ntol, units_ / other.units()};
}

/** division operator propagate uncertainty using simple method allowing
Expand All @@ -916,17 +918,18 @@ class uncertain_measurement {
UNITS_CPP14_CONSTEXPR_METHOD uncertain_measurement
simple_divide(const uncertain_measurement& other) const
{
const float ntol =
(uncertainty_ / value_) + (other.uncertainty_ / other.value_);
const float nval = value_ / other.value_;
return {nval, nval * ntol, units_ / other.units()};
const float ntol = detail::absolute_value(uncertainty_ / other.value_) +
detail::absolute_value(nval * other.uncertainty_ / other.value_);
return {nval, ntol, units_ / other.units()};
}

constexpr uncertain_measurement operator/(const measurement& other) const
{
return {
static_cast<float>(value() / other.value()),
static_cast<float>(uncertainty() / other.value()),
static_cast<float>(
uncertainty() / detail::absolute_value(other.value())),
units_ / other.units()};
}
constexpr uncertain_measurement operator/(const unit& other) const
Expand All @@ -935,12 +938,13 @@ class uncertain_measurement {
}
constexpr uncertain_measurement operator/(float val) const
{
return {value_ / val, uncertainty_ / val, units_};
return {
value_ / val, uncertainty_ / detail::absolute_value(val), units_};
}
constexpr uncertain_measurement operator/(double val) const
{
return uncertain_measurement(
value() / val, uncertainty() / val, units_);
value() / val, uncertainty() / detail::absolute_value(val), units_);
}

/** compute a unit addition and calculate the new uncertainties using the
Expand Down Expand Up @@ -1007,8 +1011,12 @@ class uncertain_measurement {
pow(const uncertain_measurement& meas, int power)
{
auto new_value = detail::power_const(meas.value_, power);
auto new_tol = ((power >= 0) ? power : -power) * new_value *
meas.uncertainty_ / meas.value_;
auto new_tol = (power == 0) ?
0.0F :
detail::absolute_value(
static_cast<float>(power) *
detail::power_const(meas.value_, power - 1)) *
meas.uncertainty_;
return uncertain_measurement{
new_value, new_tol, meas.units_.pow(power)};
}
Expand Down Expand Up @@ -1177,7 +1185,8 @@ class uncertain_measurement {
{
const double ntol = v2.uncertainty() / v2.value();
const double nval = v1.value() / v2.value();
return uncertain_measurement(nval, nval * ntol, v1.units() / v2.units_);
return uncertain_measurement(
nval, detail::absolute_value(nval * ntol), v1.units() / v2.units_);
}

friend constexpr uncertain_measurement
Expand All @@ -1197,23 +1206,24 @@ class uncertain_measurement {
{
const double ntol = v2.uncertainty() / v2.value();
const double nval = v1 / v2.value();
return uncertain_measurement(nval, nval * ntol, v2.units_.inv());
return uncertain_measurement(
nval, detail::absolute_value(nval * ntol), v2.units_.inv());
}

friend UNITS_CPP14_CONSTEXPR_METHOD uncertain_measurement
operator/(float v1, const uncertain_measurement& v2)
{
const float ntol = v2.uncertainty_ / v2.value_;
const float nval = v1 / v2.value_;
return {nval, nval * ntol, v2.units_.inv()};
return {nval, detail::absolute_value(nval * ntol), v2.units_.inv()};
}

friend UNITS_CPP14_CONSTEXPR_METHOD uncertain_measurement
operator/(int v1, const uncertain_measurement& v2)
{
const float ntol = v2.uncertainty_ / v2.value_;
const float nval = static_cast<float>(v1) / v2.value_;
return {nval, nval * ntol, v2.units_.inv()};
return {nval, detail::absolute_value(nval * ntol), v2.units_.inv()};
}

private:
Expand Down
6 changes: 6 additions & 0 deletions units/units_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,12 @@ namespace detail {
{
return a * a;
}
/// constexpr absolute value
template<typename T>
constexpr T absolute_value(T a)
{
return (a < T{0}) ? -a : a;
}
template<typename X>
constexpr X power_const_positive(X val, unsigned int power)
{
Expand Down