Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: c86fab23471c
Choose a base ref
...
head repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: cca0c41fc746
Choose a head ref
  • 3 commits
  • 3 files changed
  • 1 contributor

Commits on Aug 17, 2019

  1. Formatting of dimensions.

    pleroy committed Aug 17, 2019
    Copy the full SHA
    16f53d3 View commit details
  2. Lint.

    pleroy committed Aug 17, 2019
    Copy the full SHA
    5d8b987 View commit details
  3. Merge pull request #2286 from pleroy/2269Quantities

    Move the formatting of quantity dimensions to a common place
    pleroy authored Aug 17, 2019
    Copy the full SHA
    cca0c41 View commit details
Showing with 39 additions and 17 deletions.
  1. +2 −17 quantities/quantities_body.hpp
  2. +6 −0 quantities/si.hpp
  3. +31 −0 quantities/si_body.hpp
19 changes: 2 additions & 17 deletions quantities/quantities_body.hpp
Original file line number Diff line number Diff line change
@@ -192,18 +192,6 @@ constexpr Q NaN() {
return SIUnit<Q>() * std::numeric_limits<double>::quiet_NaN();
}

inline std::string FormatUnit(std::string const& name, int const exponent) {
switch (exponent) {
case 0:
return "";
break;
case 1:
return " " + name;
default:
return " " + name + "^" + std::to_string(exponent);
}
}

inline std::string DebugString(double const number, int const precision) {
char result[50];
#if OS_WIN && PRINCIPIA_COMPILER_MSVC && (_MSC_VER < 1900)
@@ -223,11 +211,8 @@ inline std::string DebugString(double const number, int const precision) {

template<typename D>
std::string DebugString(Quantity<D> const& quantity, int const precision) {
return DebugString(quantity / SIUnit<Quantity<D>>(), precision) +
FormatUnit("m", D::Length) + FormatUnit("kg", D::Mass) +
FormatUnit("s", D::Time) + FormatUnit("A", D::Current) +
FormatUnit("K", D::Temperature) + FormatUnit("mol", D::Amount) +
FormatUnit("cd", D::LuminousIntensity) + FormatUnit("rad", D::Angle);
return DebugString(quantity / SIUnit<Quantity<D>>(), precision) + " " +
si::Format<D>();
}

template<typename D>
6 changes: 6 additions & 0 deletions quantities/si.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

#pragma once

#include <string>

#include "quantities/named_quantities.hpp"
#include "quantities/numbers.hpp"
#include "quantities/quantities.hpp"
@@ -13,6 +15,10 @@ namespace quantities {
// with the SI.
namespace si {

// Formatting
template<typename D>
std::string Format();

// Prefixes
template<typename D> constexpr Quantity<D> Yotta(Quantity<D>);
template<typename D> constexpr Quantity<D> Zetta(Quantity<D>);
31 changes: 31 additions & 0 deletions quantities/si_body.hpp
Original file line number Diff line number Diff line change
@@ -3,10 +3,41 @@

#include "quantities/si.hpp"

#include <string>

namespace principia {
namespace quantities {
namespace si {

template<typename D>
std::string Format() {
auto const format_unit = [](std::string const& name,
int const exponent) -> std::string {
switch (exponent) {
case 0:
return "";
break;
case 1:
return " " + name;
default:
return " " + name + "^" + std::to_string(exponent);
}
};

// This string has a leading space if it's not empty.
auto const format =
format_unit("m", D::Length) + format_unit("kg", D::Mass) +
format_unit("s", D::Time) + format_unit("A", D::Current) +
format_unit("K", D::Temperature) + format_unit("mol", D::Amount) +
format_unit("cd", D::LuminousIntensity) + format_unit("rad", D::Angle);

if (format.empty()) {
return format;
} else {
return format.substr(1, format.size() - 1);
}
}

template<typename D>
constexpr Quantity<D> Yotta(Quantity<D> base) {
return 1e24 * base;