mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-29 18:07:16 +02:00
refactor: conversion_factor example moved to the main examples directory
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
conversion_factor
|
||||
=================
|
||||
|
||||
.. literalinclude:: ../../example/references/conversion_factor.cpp
|
||||
.. literalinclude:: ../../example/conversion_factor.cpp
|
||||
:caption: conversion_factor.cpp
|
||||
:start-at: #include
|
||||
:linenos:
|
||||
|
@ -22,11 +22,17 @@
|
||||
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
add_executable(hello_units hello_units.cpp)
|
||||
target_link_libraries(hello_units PRIVATE mp-units::core-fmt mp-units::core-io mp-units::si mp-units::si-international)
|
||||
#
|
||||
# add_example(target <depependencies>...)
|
||||
#
|
||||
function(add_example target)
|
||||
add_executable(${target} ${target}.cpp)
|
||||
target_link_libraries(${target} PRIVATE ${ARGN})
|
||||
endfunction()
|
||||
|
||||
add_executable(custom_systems custom_systems.cpp)
|
||||
target_link_libraries(custom_systems PRIVATE mp-units::core-io mp-units::si)
|
||||
add_example(conversion_factor mp-units::core-fmt mp-units::core-io mp-units::si)
|
||||
add_example(custom_systems mp-units::core-io mp-units::si)
|
||||
add_example(hello_units mp-units::core-fmt mp-units::core-io mp-units::si mp-units::si-international)
|
||||
|
||||
if(NOT UNITS_LIBCXX)
|
||||
add_subdirectory(glide_computer)
|
||||
|
@ -46,7 +46,7 @@ int main()
|
||||
|
||||
std::cout << "conversion factor in mp-units...\n\n";
|
||||
|
||||
constexpr length<metre> lengthA = 2.0_q_m;
|
||||
constexpr length<metre> lengthA(2.0);
|
||||
constexpr length<millimetre> lengthB = lengthA;
|
||||
|
||||
std::cout << fmt::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB)
|
@ -35,7 +35,6 @@ add_example(avg_speed mp-units::core-io mp-units::si mp-units::si-cgs mp-units::
|
||||
add_example(box_example mp-units::core-fmt mp-units::si)
|
||||
add_example(capacitor_time_curve mp-units::core-io mp-units::si)
|
||||
add_example(clcpp_response mp-units::core-fmt mp-units::core-io mp-units::si mp-units::si-iau mp-units::si-imperial mp-units::si-international mp-units::si-typographic mp-units::si-us)
|
||||
add_example(conversion_factor mp-units::core-fmt mp-units::core-io mp-units::si)
|
||||
add_example(experimental_angle mp-units::core-fmt mp-units::core-io mp-units::si)
|
||||
add_example(foot_pound_second mp-units::core-fmt mp-units::si-fps)
|
||||
add_example(kalman_filter-alpha_beta_filter_example2 mp-units::core-fmt mp-units::si)
|
||||
|
@ -35,7 +35,6 @@ add_example(avg_speed mp-units::core-io mp-units::si mp-units::si-cgs mp-units::
|
||||
add_example(box_example mp-units::core-fmt mp-units::si)
|
||||
add_example(capacitor_time_curve mp-units::core-io mp-units::si)
|
||||
add_example(clcpp_response mp-units::core-fmt mp-units::core-io mp-units::si mp-units::si-iau mp-units::si-imperial mp-units::si-international mp-units::si-typographic mp-units::si-us)
|
||||
add_example(conversion_factor mp-units::core-fmt mp-units::core-io mp-units::si)
|
||||
add_example(experimental_angle mp-units::core-fmt mp-units::core-io mp-units::si)
|
||||
add_example(foot_pound_second mp-units::core-fmt mp-units::si-fps)
|
||||
add_example(kalman_filter-alpha_beta_filter_example2 mp-units::core-fmt mp-units::si)
|
||||
|
@ -1,61 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2003-2020 Andy Little.
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see http://www.gnu.org/licenses./
|
||||
*/
|
||||
|
||||
#include <units/format.h>
|
||||
#include <units/isq/si/length.h>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
|
||||
/*
|
||||
get conversion factor from one dimensionally equivalent
|
||||
quantity type to another
|
||||
*/
|
||||
|
||||
namespace {
|
||||
|
||||
template<units::Quantity Target, units::Quantity Source>
|
||||
requires units::equivalent<typename Source::dimension, typename Target::dimension>
|
||||
inline constexpr std::common_type_t<typename Target::rep, typename Source::rep> conversion_factor(Target, Source)
|
||||
{
|
||||
// get quantities looking like inputs but with Q::rep that doesn't have narrowing conversion
|
||||
typedef std::common_type_t<typename Target::rep, typename Source::rep> rep;
|
||||
typedef units::quantity<typename Source::dimension, typename Source::unit, rep> source;
|
||||
typedef units::quantity<typename Target::dimension, typename Target::unit, rep> target;
|
||||
return target{source{1}}.number();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace units::isq::si;
|
||||
using namespace units::isq::si::references;
|
||||
|
||||
std::cout << "conversion factor in mp-units...\n\n";
|
||||
|
||||
constexpr auto lengthA = 2.0 * m;
|
||||
constexpr auto lengthB = quantity_cast<millimetre>(lengthA);
|
||||
|
||||
std::cout << fmt::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB)
|
||||
<< "represent the same length in different units.\n\n";
|
||||
|
||||
std::cout << fmt::format("therefore ratio lengthA / lengthB == {}\n\n", lengthA / lengthB);
|
||||
|
||||
std::cout << fmt::format("conversion factor from lengthA::unit of {:%q} to lengthB::unit of {:%q}:\n\n", lengthA, lengthB)
|
||||
<< fmt::format("lengthB.number( {} ) == lengthA.number( {} ) * conversion_factor( {} )\n",
|
||||
lengthB.number(), lengthA.number(), conversion_factor(lengthB, lengthA));
|
||||
}
|
Reference in New Issue
Block a user