Skip to content

Commit

Permalink
test some equation units and add some docs (#315)
Browse files Browse the repository at this point in the history
* test some equation units and add some docs

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
phlptp and pre-commit-ci[bot] authored Oct 26, 2023
1 parent 686278e commit bf4ed92
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 31 deletions.
4 changes: 4 additions & 0 deletions docs/user-guide/defined_units.rst
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,9 @@ Some special units that were not otherwise characterized in namespace `units::pr
- mach - mach number(multiplier of the speed of sound)
- rootHertz - square root of Hertz, this is a special handling unit that triggers some specific behavior to handle it.
- rootMeter - square root of meter, this is a special handling unit that triggers some specific behavior to handle it.
- degreeAPI - API scale for measuring liquid density typically petroleum based products
- degreeBaumeLight - scale for measuring liquid density for liquids lighter than water
- debreeBaumeHeavy - scale for measuring liquid density for liquids heavier than water


Other Units
Expand All @@ -807,6 +810,7 @@ Units related to climate in namespace `units::precise::climate`

- gwp - global warming potential
- gtp - global temperature potential
- odp - ozone depletion unit

Speed Units
============================
Expand Down
Binary file added test/files/fuzz_issues/rtrip_fail37
Binary file not shown.
64 changes: 48 additions & 16 deletions test/test_equation_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ TEST(logUnits, error)
20.0, precise::log::dBA * precise::m / precise::s, precise::m)));
}

TEST(otherUnits, prismDiopter)
TEST(otherEqUnits, prismDiopter)
{
EXPECT_NEAR(
convert(1, precise::deg, precise::clinical::prism_diopter),
Expand All @@ -369,7 +369,7 @@ TEST(otherUnits, prismDiopter)
0.005);
}

TEST(otherUnits, saffirSimpson)
TEST(otherEqUnits, saffirSimpson)
{
EXPECT_EQ(
std::floor(
Expand All @@ -395,7 +395,7 @@ TEST(otherUnits, saffirSimpson)
3.0);
}

TEST(otherUnits, saffirSimpson2Speed)
TEST(otherEqUnits, saffirSimpson2Speed)
{
EXPECT_NEAR(
convert(3.0, precise::special::sshws, precise::m / precise::s),
Expand Down Expand Up @@ -453,7 +453,7 @@ INSTANTIATE_TEST_SUITE_P(
beaufort,
::testing::ValuesIn(testBValues));

TEST(otherUnits, saffirSimpson2Sbeaufort)
TEST(otherEqUnits, saffirSimpson2Sbeaufort)
{
EXPECT_NEAR(
convert(12.1, precise::special::beaufort, precise::special::sshws),
Expand Down Expand Up @@ -494,7 +494,7 @@ INSTANTIATE_TEST_SUITE_P(
fujita,
::testing::ValuesIn(testFValues));

TEST(otherUnits, saffirSimpson2Sfujita)
TEST(otherEqUnits, saffirSimpson2Sfujita)
{
EXPECT_NEAR(
convert(1.0, precise::special::fujita, precise::special::sshws),
Expand All @@ -506,7 +506,7 @@ TEST(otherUnits, saffirSimpson2Sfujita)
0.05);
}

TEST(otherUnits, trits)
TEST(otherEqUnits, trits)
{
EXPECT_NEAR(
convert(1.0, precise::data::trit, precise::data::bit_s),
Expand All @@ -531,7 +531,7 @@ TEST(otherUnits, trits)
convert(9, precise::data::digits, precise::data::trit), 18.86, 0.01);
}

TEST(otherUnits, digits)
TEST(otherEqUnits, digits)
{
EXPECT_NEAR(
convert(12.0, precise::data::digits, precise::data::bit_s),
Expand All @@ -542,7 +542,7 @@ TEST(otherUnits, digits)
EXPECT_NEAR(convert(1.0, precise::data::digits, precise::one), 10.0, 0.01);
}

TEST(otherUnits, Richter)
TEST(otherEqUnits, Richter)
{
auto conv5 = convert(
5.0, precise::special::moment_magnitude, precise::N * precise::m);
Expand All @@ -567,7 +567,7 @@ TEST(otherUnits, Richter)
EXPECT_NEAR(conv7 / conv5, 1000.0, 10.0);
}

TEST(otherUnits, momentEnergy)
TEST(otherEqUnits, momentEnergy)
{
auto conv5 = convert(5.0, precise::special::moment_energy, precise::J);
EXPECT_FALSE(std::isnan(conv5));
Expand All @@ -588,21 +588,53 @@ TEST(otherUnits, momentEnergy)
EXPECT_NEAR(conv7 / conv5, 1000.0, 10.0);
}

TEST(otherUnits, unknownEQ)
TEST(otherEqUnits, unknownEQ)
{
auto eq18 = precise_unit(precise::custom::equation_unit(18));
auto eq19 = precise_unit(precise::custom::equation_unit(19));
EXPECT_EQ(convert(1.92, eq18, precise::one), 1.92);
auto eq20 = precise_unit(precise::custom::equation_unit(21));
auto eq21 = precise_unit(precise::custom::equation_unit(22));
EXPECT_EQ(convert(1.92, eq20, precise::one), 1.92);

auto conv7 = convert(7.0, eq18, eq19 * precise::W);
auto conv7 = convert(7.0, eq21, eq20 * precise::W);
EXPECT_TRUE(std::isnan(conv7));

EXPECT_EQ(convert(1.927, eq18 * precise::W, eq19 * precise::W), 1.927);
EXPECT_EQ(convert(1.927, eq20 * precise::W, eq20 * precise::W), 1.927);
}

TEST(otherEqUnits, APIgravity)
{
auto apiG = precise::special::degreeAPI;
// gasoline
EXPECT_NEAR(convert(56.98, apiG, precise::g / precise::mL), 0.7508, 0.0001);
EXPECT_NEAR(
convert(0.750743, precise::g / precise::mL, apiG), 56.98, 0.001);
// kerosene
EXPECT_NEAR(
convert(50.9, apiG, precise::g / precise::mL), 0.775768, 0.0001);
EXPECT_NEAR(convert(0.775768, precise::g / precise::mL, apiG), 50.9, 0.001);
}

TEST(otherEqUnits, degreeBaume)
{
auto degBL = precise::special::degreeBaumeLight;
EXPECT_NEAR(convert(10, degBL, precise::g / precise::mL), 1.0000, 0.0001);
EXPECT_NEAR(convert(1.0000, precise::g / precise::mL, degBL), 10.0, 0.001);
EXPECT_NEAR(convert(26.075, degBL, precise::g / precise::mL), 0.897, 0.001);
EXPECT_NEAR(convert(0.897, precise::g / precise::mL, degBL), 26.075, 0.001);
EXPECT_NEAR(convert(43.91, degBL, precise::g / precise::mL), 0.805, 0.001);
EXPECT_NEAR(convert(0.805, precise::g / precise::mL, degBL), 43.91, 0.01);

auto degBH = precise::special::degreeBaumeHeavy;
EXPECT_NEAR(convert(0, degBH, precise::g / precise::mL), 1.0000, 0.0001);
EXPECT_NEAR(convert(1.0000, precise::g / precise::mL, degBH), 0.0, 0.001);
EXPECT_NEAR(convert(15.0, degBH, precise::g / precise::mL), 1.115, 0.001);
EXPECT_NEAR(convert(1.115, precise::g / precise::mL, degBH), 14.955, 0.001);
EXPECT_NEAR(convert(69.0, degBH, precise::g / precise::mL), 1.908, 0.001);
EXPECT_NEAR(convert(1.908, precise::g / precise::mL, degBH), 69.0, 0.005);
}

#ifndef UNITS_HEADER_ONLY

TEST(otherUnits, strings)
TEST(otherEqUnits, strings)
{
precise_unit eq18(1.36, precise::custom::equation_unit(18));

Expand Down
6 changes: 3 additions & 3 deletions units/r20_conv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1613,9 +1613,9 @@ namespace precise {
unitD{"J10", "percent per millimetre", percent / mm},
unitD{"J12", "per mille per psi", {0.001, pressure::psi.inv()}},
unitD{"J13", "degree API", special::degreeAPI},
unitD{"J14", "degree Baume", special::degreeBaume},
unitD{"J15", "degree Baume (US heavy)", special::degreeBaume},
unitD{"J16", "degree Baume (US light)", special::degreeBaume},
unitD{"J14", "degree Baume", special::degreeBaumeLight},
unitD{"J15", "degree Baume (US heavy)", special::degreeBaumeLight},
unitD{"J16", "degree Baume (US light)", special::degreeBaumeHeavy},
unitD{
"J17",
"degree Balling",
Expand Down
18 changes: 11 additions & 7 deletions units/unit_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1162,9 +1162,10 @@ namespace precise {
return std::exp(val / 0.5);
case 16: // API Gravity
return 141.5 / (val + 131.5);
case 17: // degrees Baume
return (val > 0.0) ? (140.0 / (130.0 + val)) :
(145.0 / (145.0 - val));
case 17: // degrees Baume Light
return 140.0 / (130.0 + val);
case 18: // degrees Baume Heavy
return 145.0 / (145.0 - val);
case 22: // saffir simpson hurricane wind scale
{
double out = -0.17613636364;
Expand Down Expand Up @@ -1242,9 +1243,10 @@ namespace precise {
return 0.5 * (std::log)(val);
case 16: // API Gravity
return 141.5 / (val)-131.5;
case 17: // degree Baume
return (val > 1.0) ? (145.0 * (1.0 - 1 / val)) :
(140.0 / val - 130);
case 17: // degree Baume Light
return 140.0 / val - 130;
case 18: // degree Baume Heavy
return 145.0 * (1.0 - 1.0 / val);
case 22: // saffir simpson hurricane scale from wind speed
{ // using horners method on polynomial approximation of
// saffir-simpson wind speed scale
Expand Down Expand Up @@ -1412,8 +1414,10 @@ namespace precise {
// for measuring density in liquids(mainly petroleum products)
constexpr precise_unit degreeAPI =
precise_unit(custom::equation_unit(16)) * g / mL;
constexpr precise_unit degreeBaume =
constexpr precise_unit degreeBaumeLight =
precise_unit(custom::equation_unit(17)) * g / mL;
constexpr precise_unit degreeBaumeHeavy =
precise_unit(custom::equation_unit(18)) * g / mL;
} // namespace special

namespace other {
Expand Down
6 changes: 6 additions & 0 deletions units/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,12 @@ static const std::unordered_map<std::string, std::string> modifiers{
ckpair{"US dry", "US"},
ckpair{"USA", "US"},
ckpair{"USstatute", "US"},
ckpair{"USheavy", "heavy"},
ckpair{"USlight", "light"},
ckpair{"Heavy", "heavy"},
ckpair{"Light", "light"},
ckpair{"heavy", "heavy"},
ckpair{"light", "light"},
ckpair{"US statute", "US"},
ckpair{"statutory", "US"},
ckpair{"statute", "US"},
Expand Down
10 changes: 5 additions & 5 deletions units/units_conversion_maps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,14 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<
{"\xB0"
"API",
precise::special::degreeAPI}, // latin-1 degree
{"degBaume", precise::special::degreeBaume},
{"degBaume", precise::special::degreeBaumeLight},
{u8"\u00B0Baume",
precise::special::degreeBaume}, // unicode degree symbol
precise::special::degreeBaumeLight}, // unicode degree symbol
{"\xB0"
"Baume",
precise::special::degreeBaume}, // latin-1 degree
{"degBaume(USheavy)", precise::special::degreeBaume},
{"degBaume(USlight)", precise::special::degreeBaume},
precise::special::degreeBaumeLight}, // latin-1 degree
{"degBaume_heavy", precise::special::degreeBaumeHeavy},
{"degBaume_light", precise::special::degreeBaumeLight},
{"degBalling",
{1.0, precise::generate_custom_unit(109), commodities::sugar}},
{"degBrix",
Expand Down

0 comments on commit bf4ed92

Please sign in to comment.