Unit Conversion and Dimensional Analysis Library 3.6.1
A compile-time, header-only C++23 dimensional-analysis library
Loading...
Searching...
No Matches
angle.h
1//--------------------------------------------------------------------------------------------------
2//
3// UnitConversion: A compile-time c++23 unit conversion library with no dependencies
4//
5//--------------------------------------------------------------------------------------------------
6//
7// The MIT License (MIT)
8//
9// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10// and associated documentation files (the "Software"), to deal in the Software without
11// restriction, including without limitation the rights to use, copy, modify, merge, publish,
12// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
13// Software is furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in all copies or
16// substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
19// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23//
24//--------------------------------------------------------------------------------------------------
25//
26// Copyright (c) 2016 Nic Holthaus
27//
28//--------------------------------------------------------------------------------------------------
29//
30// ATTRIBUTION:
31// Parts of this work have been adapted from:
32// http://stackoverflow.com/questions/35069778/create-comparison-trait-for-template-classes-whose-parameters-are-in-a-different
33// http://stackoverflow.com/questions/28253399/check-traits-for-all-variadic-template-arguments/28253503
34// http://stackoverflow.com/questions/36321295/rational-approximation-of-square-root-of-stdratio-at-compile-time?noredirect=1#comment60266601_36321295
35// https://github.com/swatanabe/cppnow17-units
36//
37//--------------------------------------------------------------------------------------------------
38//
41//
42//--------------------------------------------------------------------------------------------------
43
44#pragma once
45
46#ifndef units_angle_h_
47#define units_angle_h_
48
49#include <units/core.h>
50
51namespace units
52{
62 UNIT_ADD(angle, degrees, deg, conversion_factor<std::ratio<1, 180>, radians<>, std::ratio<1>>)
63 UNIT_ADD(angle, arcminutes, arcmin, conversion_factor<std::ratio<1, 60>, degrees<>>)
64 UNIT_ADD(angle, arcseconds, arcsec, conversion_factor<std::ratio<1, 60>, arcminutes<>>)
65 UNIT_ADD(angle, milliarcseconds, mas, milli<arcseconds<>>)
66 UNIT_ADD(angle, turns, tr, conversion_factor<std::ratio<2>, radians<>, std::ratio<1>>)
67 UNIT_ADD(angle, gradians, gon, conversion_factor<std::ratio<1, 400>, turns<>>)
68
69 UNIT_ADD(angle, angular_mils, amil, conversion_factor<std::ratio<1, 6400>, turns<>>)
70 UNIT_ADD(angle, compass_points, cpt, conversion_factor<std::ratio<1, 32>, turns<>>)
71
73
74 //----------------------------------
75 // UNIT-ENABLED CMATH FUNCTIONS
76 //----------------------------------
77
78 //----------------------------------
79 // TRIGONOMETRIC FUNCTIONS
80 //----------------------------------
81
90 template<class AngleUnit, std::enable_if_t<traits::is_angle_unit_v<AngleUnit>, int> = 0>
91 dimensionless<detail::floating_point_promotion_t<typename AngleUnit::underlying_type>> cos(const AngleUnit angle) noexcept
92 {
93 return std::cos(convert<radians<detail::floating_point_promotion_t<typename AngleUnit::underlying_type>>>(angle).value());
94 }
95
104 template<class AngleUnit, std::enable_if_t<traits::is_angle_unit_v<AngleUnit>, int> = 0>
105 dimensionless<detail::floating_point_promotion_t<typename AngleUnit::underlying_type>> sin(const AngleUnit angle) noexcept
106 {
107 return std::sin(convert<radians<detail::floating_point_promotion_t<typename AngleUnit::underlying_type>>>(angle).value());
108 }
109
117 template<class AngleUnit, std::enable_if_t<traits::is_angle_unit_v<AngleUnit>, int> = 0>
118 dimensionless<detail::floating_point_promotion_t<typename AngleUnit::underlying_type>> tan(const AngleUnit angle) noexcept
119 {
120 return std::tan(convert<radians<detail::floating_point_promotion_t<typename AngleUnit::underlying_type>>>(angle).value());
121 }
122
130 template<class dimensionlessUnit, std::enable_if_t<traits::is_dimensionless_unit_v<dimensionlessUnit>, int> = 0>
131 radians<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>> acos(const dimensionlessUnit x) noexcept
132 {
133 return radians<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>(
134 std::acos(x.template to<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>()));
135 }
136
144 template<class dimensionlessUnit, std::enable_if_t<traits::is_dimensionless_unit_v<dimensionlessUnit>, int> = 0>
145 radians<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>> asin(const dimensionlessUnit x) noexcept
146 {
147 return radians<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>(
148 std::asin(x.template to<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>()));
149 }
150
162 template<class dimensionlessUnit, std::enable_if_t<traits::is_dimensionless_unit_v<dimensionlessUnit>, int> = 0>
163 radians<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>> atan(const dimensionlessUnit x) noexcept
164 {
165 return radians<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>(
166 std::atan(x.template to<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>()));
167 }
168
178 template<class Y, class X, std::enable_if_t<traits::is_dimensionless_unit_v<decltype(std::declval<Y>() / std::declval<X>())>, int> = 0>
179 radians<detail::floating_point_promotion_t<std::common_type_t<typename X::underlying_type, typename Y::underlying_type>>> atan2(
180 const Y y, const X x) noexcept
181 {
182 using CommonUnit = std::common_type_t<X, Y>;
183 // X and Y could be different length units, so normalize them
184 return radians<detail::floating_point_promotion_t<typename CommonUnit::underlying_type>>(std::atan2(CommonUnit(y).value(), CommonUnit(x).value()));
185 }
186
187 //----------------------------------
188 // HYPERBOLIC TRIG FUNCTIONS
189 //----------------------------------
190
201 template<class dimensionlessUnit, std::enable_if_t<traits::is_dimensionless_unit_v<dimensionlessUnit>, int> = 0>
202 dimensionless<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>> cosh(const dimensionlessUnit x) noexcept
203 {
204 return std::cosh(x.template to<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>());
205 }
206
217 template<class dimensionlessUnit, std::enable_if_t<traits::is_dimensionless_unit_v<dimensionlessUnit>, int> = 0>
218 dimensionless<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>> sinh(const dimensionlessUnit x) noexcept
219 {
220 return std::sinh(x.template to<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>());
221 }
222
233 template<class dimensionlessUnit, std::enable_if_t<traits::is_dimensionless_unit_v<dimensionlessUnit>, int> = 0>
234 dimensionless<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>> tanh(const dimensionlessUnit x) noexcept
235 {
236 return std::tanh(x.template to<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>());
237 }
238
248 template<class dimensionlessUnit, std::enable_if_t<traits::is_dimensionless_unit_v<dimensionlessUnit>, int> = 0>
249 dimensionless<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>> acosh(const dimensionlessUnit x) noexcept
250 {
251 return dimensionless<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>(
252 std::acosh(x.template to<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>()));
253 }
254
263 template<class dimensionlessUnit, std::enable_if_t<traits::is_dimensionless_unit_v<dimensionlessUnit>, int> = 0>
264 dimensionless<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>> asinh(const dimensionlessUnit x) noexcept
265 {
266 return dimensionless<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>(
267 std::asinh(x.template to<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>()));
268 }
269
279 template<class dimensionlessUnit, std::enable_if_t<traits::is_dimensionless_unit_v<dimensionlessUnit>, int> = 0>
280 dimensionless<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>> atanh(const dimensionlessUnit x) noexcept
281 {
282 return dimensionless<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>(
283 std::atanh(x.template to<detail::floating_point_promotion_t<typename dimensionlessUnit::underlying_type>>()));
284 }
285} // namespace units
286
287#endif // units_angle_h_
unit, dimensional analysis, generic cmath functions, traits (not dimension-specific),...
#define UNIT_ADD(namespaceName, namePlural, abbreviation,...)
Macro for generating the boilerplate code needed for a new unit.
Definition core.h:447
#define UNIT_ADD_WITH_METRIC_PREFIXES(namespaceName, namePlural, abbreviation,...)
Macro for generating the boilerplate code needed for a new unit, including its metric prefixes from f...
Definition core.h:529
#define UNIT_ADD_DIMENSION_TRAIT(unitdimension, ConceptName)
Macro to create the is_dimension_unit type trait and the ConceptName concept.
Definition core.h:488
constexpr To convert(const From &value) noexcept
converts a value from an unit to another.
Definition core.h:2345
typename detail::prefix< std::milli, Cf >::type milli
< Represents the type of class Cf with the metric 'micro' prefix appended.
Definition core.h:1998
radians< detail::floating_point_promotion_t< typename dimensionlessUnit::underlying_type > > asin(const dimensionlessUnit x) noexcept
Compute arc sine.
Definition angle.h:145
dimensionless< detail::floating_point_promotion_t< typename AngleUnit::underlying_type > > tan(const AngleUnit angle) noexcept
Compute tangent.
Definition angle.h:118
dimensionless< detail::floating_point_promotion_t< typename dimensionlessUnit::underlying_type > > asinh(const dimensionlessUnit x) noexcept
Compute arc hyperbolic sine.
Definition angle.h:264
dimensionless< detail::floating_point_promotion_t< typename AngleUnit::underlying_type > > sin(const AngleUnit angle) noexcept
Compute sine.
Definition angle.h:105
radians< detail::floating_point_promotion_t< typename dimensionlessUnit::underlying_type > > acos(const dimensionlessUnit x) noexcept
Compute arc cosine.
Definition angle.h:131
dimensionless< detail::floating_point_promotion_t< typename dimensionlessUnit::underlying_type > > sinh(const dimensionlessUnit x) noexcept
Compute hyperbolic sine.
Definition angle.h:218
dimensionless< detail::floating_point_promotion_t< typename dimensionlessUnit::underlying_type > > tanh(const dimensionlessUnit x) noexcept
Compute hyperbolic tangent.
Definition angle.h:234
dimensionless< detail::floating_point_promotion_t< typename dimensionlessUnit::underlying_type > > acosh(const dimensionlessUnit x) noexcept
Compute arc hyperbolic cosine.
Definition angle.h:249
dimensionless< detail::floating_point_promotion_t< typename dimensionlessUnit::underlying_type > > atanh(const dimensionlessUnit x) noexcept
Compute arc hyperbolic tangent.
Definition angle.h:280
dimensionless< detail::floating_point_promotion_t< typename dimensionlessUnit::underlying_type > > cosh(const dimensionlessUnit x) noexcept
Compute hyperbolic cosine.
Definition angle.h:202
dimensionless< detail::floating_point_promotion_t< typename AngleUnit::underlying_type > > cos(const AngleUnit angle) noexcept
Compute cosine.
Definition angle.h:91
radians< detail::floating_point_promotion_t< typename dimensionlessUnit::underlying_type > > atan(const dimensionlessUnit x) noexcept
Compute arc tangent.
Definition angle.h:163
radians< detail::floating_point_promotion_t< std::common_type_t< typename X::underlying_type, typename Y::underlying_type > > > atan2(const Y y, const X x) noexcept
Compute arc tangent with two parameters.
Definition angle.h:179
STL namespace.
namespace for unit types and containers representing angle values
make_dimension< angle_tag > angle
< Represents a quantity with no dimension.
Definition core.h:1334
namespace representing type traits which can access the properties of types provided by the units lib...
Definition core.h:197
Unit Conversion Library namespace.
Definition units.h:106
Type representing an arbitrary conversion factor between units.
Definition core.h:1563