Skip to content

Commit

Permalink
Add STANDARD_GRAVITY Constant (#348)
Browse files Browse the repository at this point in the history
It took me a little while to figure out exactly what I wanted to do
here.

Initially, I was going to _deprecate_ the standard gravity unit.  Then I
realized we need it to define certain other units, such as the
correspondence between "pounds" as a force and a mass --- and I didn't
want to introduce a circular dependency between `:units` and
`:constants`.

Then I was going to deprecate only the _quantity maker_,
`standard_gravity`.  But then I realized it would be weird to create a
situation where certain units have quantity makers, and certain other
ones don't.

In the end, I think the right move is to just provide standard gravity
as a new constant.  This is the preferred way to interact with it, but
the old ways will still be there for the foreseeable future.

Follow-on to #90.
  • Loading branch information
chiphogg authored Dec 10, 2024
1 parent 07a7f4c commit 1b72b87
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions au/code/au/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ header_only_library(
constants/planck_constant.hh
constants/reduced_planck_constant.hh
constants/speed_of_light.hh
constants/standard_gravity.hh
stdx/experimental/is_detected.hh
stdx/functional.hh
stdx/type_traits.hh
Expand Down Expand Up @@ -239,6 +240,7 @@ gtest_based_test(
constants/test/planck_constant_test.cc
constants/test/reduced_planck_constant_test.cc
constants/test/speed_of_light_test.cc
constants/test/standard_gravity_test.cc
DEPS
au
testing
Expand Down
22 changes: 22 additions & 0 deletions au/code/au/constants/standard_gravity.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2024 Aurora Operations, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "au/constant.hh"
#include "au/units/standard_gravity.hh"

namespace au {

constexpr auto STANDARD_GRAVITY = make_constant(StandardGravity{});

} // namespace au
44 changes: 44 additions & 0 deletions au/code/au/constants/test/standard_gravity_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 Aurora Operations, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "au/constants/standard_gravity.hh"

#include "au/testing.hh"
#include "au/units/meters.hh"
#include "au/units/seconds.hh"
#include "gtest/gtest.h"

namespace au {
namespace {

using ::testing::StrEq;

TEST(StandardGravity, HasExpectedValue) {
// g_0 = 9.80665 m/s^2

// Test approximate value (guard against powers-of-10 type errors).
constexpr auto defining_units = meters / pow<2>(seconds);
constexpr auto val = defining_units(9.80665);
constexpr auto err = defining_units(0.00001);
EXPECT_THAT(STANDARD_GRAVITY.as<double>(defining_units), IsNear(val, err));

// Test exact value.
EXPECT_THAT(STANDARD_GRAVITY.in<int>(defining_units / pow<5>(mag<10>())),
SameTypeAndValue(980'665));
}

TEST(StandardGravity, HasExpectedLabel) { EXPECT_THAT(unit_label(STANDARD_GRAVITY), StrEq("g_0")); }

} // namespace
} // namespace au
1 change: 1 addition & 0 deletions docs/reference/constant.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Here are the constants that we include with Au:
| Planck constant | $h$ | $6.626\,070\,15 \times 10^{-34}\,\, \text{J} \cdot \text{s}$ | `planck_constant.hh` | `PLANCK_CONSTANT` |
| Reduced Planck constant | $\hbar$ | $1.054\,571\,817 \times 10^{-34}\,\, \text{J} \cdot \text{s}$ | `reduced_planck_constant.hh` | `REDUCED_PLANCK_CONSTANT` |
| Speed of light | $c$ | $299\,792\,458\,\, \text{m} / \text{s}$ | `speed_of_light.hh` | `SPEED_OF_LIGHT` |
| Standard Gravity | $g_0$ | $9.806\,65\,\, \text{m} / \text{s}^2$ | `standard_gravity.hh` | `STANDARD_GRAVITY` |

Our policy is to include only exactly defined constants with the library. This rules out many
useful constants, such as the universal gravitational constant $G$, the _new_ (post-2019) permeability
Expand Down

0 comments on commit 1b72b87

Please sign in to comment.