Skip to content

Commit 6d10a15

Browse files
committed
feat: 💥 pi and π is now a unit constant (pi magnitude constant renamed to pi_c)
1 parent 4da8a9d commit 6d10a15

17 files changed

Lines changed: 89 additions & 81 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ This page documents the version history and changes for the **mp-units** library
66

77
## mp-units
88

9+
### 2.6.0 <small>TBD</small> { id="2.6.0" }
10+
11+
- (!) feat: `pi` and `π` is now a unit constant
12+
- (!) refactor: `pi` magnitude constant renamed to `pi_c`
13+
914
### 2.5.0 <small>December 24, 2025</small> { id="2.5.0" }
1015

1116
- (!) feat: representation concepts improved

docs/examples/measurement.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ This showcases:
7878
2. **Kinematic calculations** - Multiplying _acceleration_ by _time_ with automatic unit
7979
conversions
8080
3. **Scalar multiplication** - Exact scaling preserves relative uncertainty
81-
4. **Faster-than-lightspeed constants** - Using `mag<π>` keeps π as an exact symbolic value,
81+
4. **Faster-than-lightspeed constants** - Using `π` keeps pi as an exact symbolic value,
8282
not a numeric approximation
8383
5. **Geometric calculations** - Computing _circumference_ and _area_ where π contributes
8484
zero uncertainty
@@ -93,8 +93,8 @@ Velocity calculation: V = 9.8 ± 0.1 m/s² * 1.2 ± 0.1 s = 11.76 ± 0.98732 m
9393
Scalar multiplication: d = 10 * 123 ± 1 m = 1230 ± 10 m
9494
Radius: r = 5 ± 0.1 m
9595
Circular circumference: 2πr = 5 ± 0.1 (2 π) m = 31.4159 ± 0.628319 m
96-
Circular area: πr² = 25 ± 1 (π) m² = 78.5398 ± 3.14159 m²
97-
Radius from area: A = 25 ± 1 (π m²) -> r = √(A/π) = 5 ± 0.1 m
96+
Circular area: πr² = 25 ± 1 π m² = 78.5398 ± 3.14159 m²
97+
Radius from area: A = 25 ± 1 π m² -> r = √(A/π) = 5 ± 0.1 m
9898
```
9999

100100
### Uncertainty Propagation Analysis

docs/getting_started/cheat_sheet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ constexpr Unit auto m3 = cubic(si::metre);
286286
constexpr Unit auto mps = m / s;
287287
288288
// Constants as a unit
289-
constexpr Unit auto two_pi = mag<2> * mag<pi> * one;
289+
constexpr Unit auto two_pi = mag<2> * π;
290290
```
291291

292292
1. Non-SI unit accepted for use with SI

docs/users_guide/framework_basics/faster_than_lightspeed_constants.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ inline constexpr struct speed_of_light_in_vacuum final :
4545
} // namespace si2019
4646

4747
inline constexpr struct magnetic_constant final :
48-
named_unit<{u8"μ₀", "u_0"}, mag<4> * mag<π> * mag_power<10, -7> * henry / metre> {} magnetic_constant;
48+
named_unit<{u8"μ₀", "u_0"}, mag<4> * mag_power<10, -7> * π * henry / metre> {} magnetic_constant;
4949

5050
} // namespace mp_units::si
5151
```

docs/users_guide/framework_basics/simple_and_typed_quantities.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ Let's see another example:
284284
public:
285285
constexpr CylindricalStorageTank(const quantity<si::metre>& radius,
286286
const quantity<si::metre>& height) :
287-
StorageTank(std::numbers::pi * pow<2>(radius), height)
287+
StorageTank(pow<2>(radius) * π, height)
288288
{
289289
}
290290
};
@@ -341,8 +341,7 @@ Let's see another example:
341341
public:
342342
constexpr CylindricalStorageTank(const quantity<isq::radius[si::metre]>& radius,
343343
const quantity<isq::height[si::metre]>& height) :
344-
StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)),
345-
height)
344+
StorageTank(quantity_cast<horizontal_area>(pow<2>(radius) * π), height)
346345
{
347346
}
348347
};
@@ -395,7 +394,7 @@ Let's see another example:
395394
public:
396395
constexpr CylindricalStorageTank(const quantity<si::metre>& radius,
397396
const quantity<si::metre>& height) :
398-
StorageTank(std::numbers::pi * pow<2>(radius), height)
397+
StorageTank(pow<2>(radius) * π, height)
399398
{
400399
}
401400
};
@@ -454,8 +453,7 @@ Let's see another example:
454453
public:
455454
constexpr CylindricalStorageTank(const quantity<isq::radius[si::metre]>& radius,
456455
const quantity<isq::height[si::metre]>& height) :
457-
StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)),
458-
height)
456+
StorageTank(quantity_cast<horizontal_area>(pow<2>(radius) * π), height)
459457
{
460458
}
461459
};
@@ -565,8 +563,7 @@ class CylindricalStorageTank : public StorageTank {
565563
public:
566564
constexpr CylindricalStorageTank(const quantity<isq::radius[m]>& radius,
567565
const quantity<isq::height[m]>& height) :
568-
StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)),
569-
height)
566+
StorageTank(quantity_cast<horizontal_area>(pow<2>(radius) * π), height)
570567
{
571568
}
572569
};

docs/users_guide/framework_basics/systems_of_units.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,13 @@ For some units, a magnitude might also be irrational. The best example here is a
277277
which is defined using a floating-point magnitude having a factor of the number π (Pi):
278278
279279
```cpp
280-
inline constexpr struct pi final : mag_constant<symbol_text{u8"π", "pi"}, std::numbers::pi_v<long double>> {} pi;
280+
inline constexpr struct pi_c final : mag_constant<symbol_text{u8"π", "pi"}, std::numbers::pi_v<long double>> {} pi_c;
281+
inline constexpr struct pi final : named_unit<symbol_text{u8"π", "pi"}, mag<pi_c> * one> {} pi;
281282
inline constexpr auto π = pi;
282283
```
283284

284285
```cpp
285-
inline constexpr struct degree final : named_unit<{u8"°", "deg"}, mag<π> / mag<180> * si::radian> {} degree;
286+
inline constexpr struct degree final : named_unit<{u8"°", "deg"}, mag_ratio<1, 180> * π * si::radian> {} degree;
286287
```
287288
288289

example/measurement.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ void example()
6464
std::cout << "Scalar multiplication: d = 10 * " << length << " = " << 10 * length << '\n';
6565

6666
const auto radius = measurement{5.0, 0.1} * m;
67-
const auto circumference = radius * (mag<2> * mag<π> * one);
68-
const auto area = pow<2>(radius) * (mag<π> * one);
67+
const auto circumference = radius * (mag<2> * π);
68+
const auto area = pow<2>(radius) * π;
6969
std::cout << "Radius: r = " << radius << '\n';
7070
std::cout << "Circular circumference: 2πr = " << circumference << " = " << circumference.in(m) << '\n';
7171
std::cout << "Circular area: πr² = " << area << " = " << area.in(m2) << '\n';
7272

73-
const auto area_measured = measurement{25.0, 1.0} * (mag<π> * m2);
74-
const auto radius_from_area = sqrt(area_measured / (mag<π> * one));
73+
const auto area_measured = measurement{25.0, 1.0} * π * m2;
74+
const auto radius_from_area = sqrt(area_measured / π);
7575
std::cout << "Radius from area: A = " << area_measured << " -> r = √(A/π) = " << radius_from_area << '\n';
7676
}
7777

example/storage_tank.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class StorageTank {
9797
class CylindricalStorageTank : public StorageTank {
9898
public:
9999
constexpr CylindricalStorageTank(const quantity<isq::radius[m]>& radius, const quantity<isq::height[m]>& height) :
100-
StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)), height)
100+
StorageTank(quantity_cast<horizontal_area>(pow<2>(radius) * π), height)
101101
{
102102
}
103103
};

src/core/include/mp-units/framework/unit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,9 @@ inline constexpr struct percent final : named_unit<"%", mag_ratio<1, 100> * one>
658658
inline constexpr struct per_mille final : named_unit<symbol_text{u8"" /* U+2030 PER MILLE SIGN */, "%o"}, mag_ratio<1, 1000> * one> {} per_mille;
659659
inline constexpr struct parts_per_million final : named_unit<"ppm", mag_ratio<1, 1'000'000> * one> {} parts_per_million;
660660
inline constexpr auto ppm = parts_per_million;
661+
// TODO make it a unit constant rather than a named unit when such support will be provided
662+
inline constexpr struct pi final : named_unit<symbol_text{u8"π" /* U+03C0 GREEK SMALL LETTER PI */, "pi"}, mag<pi_c> * one> {} pi;
663+
inline constexpr auto π /* U+03C0 GREEK SMALL LETTER PI */ = pi;
661664
// clang-format on
662665

663666

src/core/include/mp-units/framework/unit_magnitude.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,15 @@ constexpr UnitMagnitude auto mag_power = pow<Num, Den>(mag<Base>);
8787
* @brief A convenient Magnitude constant for pi, which we can manipulate like a regular number.
8888
*/
8989
#if defined MP_UNITS_COMP_CLANG && MP_UNITS_COMP_CLANG < 18
90-
inline constexpr struct pi final : mag_constant<symbol_text{u8"π" /* U+03C0 GREEK SMALL LETTER PI */, "pi"}> {
90+
inline constexpr struct pi_c final : mag_constant<symbol_text{u8"π" /* U+03C0 GREEK SMALL LETTER PI */, "pi"}> {
9191
static constexpr auto _value_ = std::numbers::pi_v<long double>;
9292
#else
93-
inline constexpr struct pi final :
93+
inline constexpr struct pi_c final :
9494
mag_constant<symbol_text{u8"π" /* U+03C0 GREEK SMALL LETTER PI */, "pi"}, std::numbers::pi_v<long double> > {
9595
#endif
96-
} pi;
97-
inline constexpr auto π /* U+03C0 GREEK SMALL LETTER PI */ = pi;
96+
} pi_c;
9897

99-
[[deprecated("2.3.0: Use `mag<pi>` instead")]] inline constexpr UnitMagnitude auto mag_pi = mag<pi>;
98+
[[deprecated("2.3.0: Use `mag<pi>` instead")]] inline constexpr UnitMagnitude auto mag_pi = mag<pi_c>;
10099

101100
MP_UNITS_EXPORT_END
102101

0 commit comments

Comments
 (0)