mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-31 02:47:16 +02:00
Third party examples : add box example
Add si::density quantity header . Add si::resistance quantity header. Update si::capacitance header with mF, uF, nF, pF. Update si::voltage header with mV, uV, nV,pV Third party example : add capacitor time curve example Add incoherent length units, TODO move them out from si header. Third party examples : add clcpp_response showing effectivenes of typed units for physical quantity library Third party examples : add conversion factor example Add third party examples to cmake Third party examples : box example : Add air_density constant for clarity remove explicit this-> and tidy up. Third party examples : in clcpp response example, change base unit from km to m for single type or all units example. Third party examples : conversion_factor , add inline constexpr to units_str function. Third party examples : box_example, change quantity::unit syntax to quantity::unit<> to allow generic(default double) value_type. examples : remove examples from third party to main examples directory. Update cmake. physical/si/resistance.hpp : remove underscores from kiloohm etc, UDL collision with 'R' so prefix with underscore
This commit is contained in:
committed by
Mateusz Pusz
parent
a01c811f5f
commit
868842bd46
@ -29,3 +29,7 @@ add_example(avg_velocity)
|
||||
add_example(hello_units)
|
||||
add_example(measurement)
|
||||
add_example(unknown_dimension)
|
||||
add_example(box_example)
|
||||
add_example(capacitor_time_curve)
|
||||
add_example(clcpp_response)
|
||||
add_example(conversion_factor)
|
||||
|
125
example/box_example.cpp
Normal file
125
example/box_example.cpp
Normal file
@ -0,0 +1,125 @@
|
||||
|
||||
|
||||
#include <units/physical/si/acceleration.h>
|
||||
#include <units/physical/si/length.h>
|
||||
#include <units/physical/si/volume.h>
|
||||
#include <units/physical/si/time.h>
|
||||
#include <units/physical/si/force.h>
|
||||
#include <units/physical/si/mass.h>
|
||||
#include <units/physical/si/density.h>
|
||||
#include <cassert>
|
||||
|
||||
namespace{
|
||||
|
||||
namespace length{
|
||||
|
||||
template <typename Rep = double>
|
||||
using m = units::si::length<units::si::metre,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using mm = units::si::length<units::si::millimetre,Rep>;
|
||||
}
|
||||
|
||||
namespace acceleration{
|
||||
|
||||
template <typename Rep = double>
|
||||
using m_per_s2 = units::si::acceleration<units::si::metre_per_second_sq,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
constexpr m_per_s2<> g{static_cast<Rep>(9.80665)};
|
||||
}
|
||||
|
||||
namespace force{
|
||||
|
||||
template <typename Rep = double>
|
||||
using N = units::si::force<units::si::newton,Rep>;
|
||||
}
|
||||
|
||||
namespace mass {
|
||||
|
||||
template <typename Rep = double>
|
||||
using kg = units::si::mass<units::si::kilogram,Rep>;
|
||||
}
|
||||
|
||||
namespace density {
|
||||
|
||||
template <typename Rep = double>
|
||||
using kg_per_m3 = units::si::density<units::si::kilogram_per_metre_cub,Rep>;
|
||||
}
|
||||
|
||||
namespace volume {
|
||||
|
||||
template <typename Rep = double>
|
||||
using m3 = units::si::volume<units::si::cubic_metre,Rep>;
|
||||
}
|
||||
}
|
||||
|
||||
struct Box{
|
||||
|
||||
Box(length::m<> const& l,
|
||||
length::m<> const& w,
|
||||
length::m<> const& h
|
||||
): length{l},width{w},height{h}{}
|
||||
|
||||
force::N<> filled_weight()const
|
||||
{
|
||||
volume::m3<> const volume
|
||||
= length * width * height;
|
||||
mass::kg<> const mass = contents.density * volume;
|
||||
return mass * acceleration::g<>;
|
||||
}
|
||||
|
||||
length::m<> fill_level(mass::kg<> const & measured_mass)const
|
||||
{
|
||||
return height
|
||||
* (measured_mass * acceleration::g<>) / filled_weight();
|
||||
}
|
||||
|
||||
volume::m3<> spare_capacity(mass::kg<> const & measured_mass)const
|
||||
{
|
||||
return (height - fill_level(measured_mass)) * width * length;
|
||||
}
|
||||
|
||||
struct contents{
|
||||
contents():density{air_density}{}
|
||||
density::kg_per_m3<> density;
|
||||
}contents;
|
||||
|
||||
void set_contents_density(density::kg_per_m3<> const & density_in)
|
||||
{
|
||||
assert( density_in > air_density );
|
||||
contents.density = density_in;
|
||||
}
|
||||
|
||||
static constexpr density::kg_per_m3<> air_density{1.225};
|
||||
|
||||
length::m<> length;
|
||||
length::m<> width;
|
||||
length::m<> height;
|
||||
};
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace units::si::literals;
|
||||
int main()
|
||||
{
|
||||
auto box = Box{1000.0mm, 500.0mm, 200.0mm};
|
||||
box.set_contents_density(1000.0kg_per_m3);
|
||||
|
||||
auto fill_time = 200.0s; // time since starting fill
|
||||
auto measured_mass = 20.0kg; // measured mass at fill_time
|
||||
|
||||
std::cout << "mpusz/units box example...\n";
|
||||
std::cout << "fill height at " << fill_time << " = "
|
||||
<< box.fill_level(measured_mass) << "( " << (box.fill_level(measured_mass)/ box.height)*100 << "% full)\n";
|
||||
std::cout << "spare_capacity at " << fill_time << " = "
|
||||
<< box.spare_capacity(measured_mass) <<'\n';
|
||||
std::cout << "input flow rate after " << fill_time
|
||||
<< " = " << measured_mass / fill_time <<'\n';
|
||||
std::cout << "float rise rate = "
|
||||
<< box.fill_level(measured_mass) / fill_time <<'\n';
|
||||
auto fill_time_left
|
||||
= (box.height / box.fill_level(measured_mass) - 1) * fill_time ;
|
||||
std::cout << "box full E.T.A. at current flow rate = " << fill_time_left <<'\n';
|
||||
|
||||
}
|
76
example/capacitor_time_curve.cpp
Normal file
76
example/capacitor_time_curve.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
|
||||
/*
|
||||
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./
|
||||
*/
|
||||
|
||||
/*
|
||||
capacitor discharge curve using compile_time
|
||||
physical_quantities
|
||||
*/
|
||||
|
||||
#include <units/physical/si/capacitance.h>
|
||||
#include <units/physical/si/resistance.h>
|
||||
#include <units/physical/si/voltage.h>
|
||||
#include <units/physical/si/time.h>
|
||||
#include <cmath>
|
||||
|
||||
namespace {
|
||||
namespace voltage {
|
||||
|
||||
template <typename Rep = double>
|
||||
using V = units::si::voltage<units::si::volt,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using mV = units::si::voltage<units::si::millivolt,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using uV = units::si::voltage<units::si::microvolt,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using nV = units::si::voltage<units::si::nanovolt,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using pV = units::si::voltage<units::si::picovolt,Rep>;
|
||||
}
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace units::si::literals;
|
||||
int main()
|
||||
{
|
||||
std::cout << "mpusz/units capacitor time curve example...\n";
|
||||
std::cout.setf(std::ios_base::fixed,std::ios_base::floatfield);
|
||||
std::cout.precision(3);
|
||||
|
||||
auto constexpr C = 0.47uF;
|
||||
auto constexpr V0 = 5.0V;
|
||||
auto constexpr R = 4.7kR;
|
||||
|
||||
for ( auto t = 0ms ; t <= 50ms; ++t ){
|
||||
|
||||
auto const Vt = V0 * std::exp(-t / (R * C));
|
||||
|
||||
std::cout << "at " << t << " voltage is " ;
|
||||
|
||||
if ( Vt >= 1V ) std::cout << Vt ;
|
||||
else if( Vt >= 1mV ) std::cout << voltage::mV<>{Vt};
|
||||
else if( Vt >= 1uV ) std::cout << voltage::uV<>{Vt};
|
||||
else if( Vt >= 1nV ) std::cout << voltage::nV<>{Vt};
|
||||
else std::cout << voltage::pV<>{Vt};
|
||||
std::cout << "\n";
|
||||
}
|
||||
}
|
230
example/clcpp_response.cpp
Normal file
230
example/clcpp_response.cpp
Normal file
@ -0,0 +1,230 @@
|
||||
|
||||
/*
|
||||
Copyright (c) 2003-2019 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/physical/si/length.h>
|
||||
#include <units/physical/us/length.h>
|
||||
#include <units/physical/si/area.h>
|
||||
#include <units/physical/si/volume.h>
|
||||
#include <units/physical/si/time.h>
|
||||
|
||||
namespace {
|
||||
namespace length{
|
||||
|
||||
template <typename Rep = double>
|
||||
using m = units::si::length<units::si::metre,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using mm = units::si::length<units::si::millimetre,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using fm = units::si::length<units::si::femtometre,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using km = units::si::length<units::si::kilometre,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using AU = units::si::length<units::si::astronomical_unit,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using in = units::si::length<units::us::inch,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using angstrom = units::si::length<units::si::angstrom,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using ch = units::si::length<units::si::chain,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using fathom = units::si::length<units::si::fathom,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using fathom_us = units::si::length<units::si::fathom_us,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using ft = units::si::length<units::us::foot,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using ft_us = units::si::length<units::si::foot_us,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using ly = units::si::length<units::si::light_year,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using mi = units::si::length<units::si::mile,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using mi_naut = units::si::length<units::si::nautical_mile,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using pc = units::si::length<units::si::parsec,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using pica_comp = units::si::length<units::si::pica_comp,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using pica_prn = units::si::length<units::si::pica_prn,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using point_comp = units::si::length<units::si::point_comp,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using point_prn = units::si::length<units::si::point_prn,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using rd = units::si::length<units::si::rod,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using yd = units::si::length<units::us::yard,Rep>;
|
||||
|
||||
}
|
||||
|
||||
namespace time{
|
||||
|
||||
template <typename Rep = double>
|
||||
using s = units::si::time<units::si::second,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using min = units::si::time<units::si::minute,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using h = units::si::time<units::si::hour,Rep>;
|
||||
}
|
||||
|
||||
namespace area{
|
||||
|
||||
template <typename Rep = double>
|
||||
using m2 = units::si::area<units::si::square_metre,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using fm2 = units::si::area<units::si::square_femtometre,Rep>;
|
||||
}
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace units::si::literals;
|
||||
|
||||
void simple_quantities()
|
||||
{
|
||||
using distance = length::m<>;
|
||||
using time = time::s<>;
|
||||
distance constexpr km = 1.0km;
|
||||
distance constexpr miles = 1q_mi;
|
||||
|
||||
time constexpr sec = 1s;
|
||||
time constexpr min = 1min;
|
||||
time constexpr hr = 1h;
|
||||
|
||||
std::cout << "A physical quantities library can choose the simple\n";
|
||||
std::cout << "option to provide output using a single type for each base unit:\n\n";
|
||||
std::cout << km << '\n';
|
||||
std::cout << miles << '\n';
|
||||
std::cout << sec << '\n';
|
||||
std::cout << min << '\n';
|
||||
std::cout << hr << "\n\n";
|
||||
}
|
||||
|
||||
void quantities_with_typed_units()
|
||||
{
|
||||
length::km<> constexpr km = 1km;
|
||||
|
||||
length::mi<> constexpr miles = 1q_mi;
|
||||
|
||||
std::cout.precision(6);
|
||||
|
||||
time::s<> constexpr sec = 1s;
|
||||
time::min<> constexpr min = 1min;
|
||||
time::h<> constexpr hr = 1h;
|
||||
|
||||
std::cout << "A more flexible option is to provide separate types for each unit,\n\n";
|
||||
std::cout << km << '\n';
|
||||
std::cout << miles << '\n';
|
||||
std::cout << sec << '\n';
|
||||
std::cout << min << '\n';
|
||||
std::cout << hr << "\n\n";
|
||||
|
||||
length::m<> constexpr meter = 1m;
|
||||
std::cout << "then a wide range of pre-defined units can be defined and converted,\n"
|
||||
" for consistency and repeatability across applications:\n\n";
|
||||
|
||||
std::cout << meter << '\n' ;
|
||||
|
||||
std::cout << " = " << length::AU<>(meter) << '\n';
|
||||
std::cout << " = " << length::angstrom<>(meter) << '\n';
|
||||
std::cout << " = " << length::ch<>(meter) << '\n';
|
||||
std::cout << " = " << length::fathom<>(meter) << '\n';
|
||||
std::cout << " = " << length::fathom_us<>(meter) << '\n';
|
||||
std::cout << " = " << length::ft<>(meter) << '\n';
|
||||
std::cout << " = " << length::ft_us<>(meter) << '\n';
|
||||
std::cout << " = " << length::in<>(meter) << '\n';
|
||||
std::cout << " = " << length::ly<>(meter) << '\n';
|
||||
std::cout << " = " << length::mi<>(meter) << '\n';
|
||||
std::cout << " = " << length::mi_naut<>(meter) << '\n';
|
||||
std::cout << " = " << length::pc<>(meter) << '\n';
|
||||
std::cout << " = " << length::pica_comp<>(meter) << '\n';
|
||||
std::cout << " = " << length::pica_prn<>(meter) << '\n';
|
||||
std::cout << " = " << length::point_comp<>(meter) << '\n';
|
||||
std::cout << " = " << length::point_prn<>(meter) << '\n';
|
||||
std::cout << " = " << length::rd<>(meter) << '\n';
|
||||
std::cout << " = " << length::yd<>(meter) << '\n';
|
||||
}
|
||||
|
||||
void calcs_comparison()
|
||||
{
|
||||
std::cout.precision(20);
|
||||
std::cout << "\nA distinct unit for each type is efficient and accurate\n"
|
||||
"when adding two values of the same very big\n"
|
||||
"or very small type:\n\n";
|
||||
|
||||
length::fm<float> L1A = 2fm;
|
||||
length::fm<float> L2A = 3fm;
|
||||
length::fm<float> LrA = L1A + L2A;
|
||||
|
||||
std::cout << L1A << " + " << L2A << " = " << LrA << "\n\n";
|
||||
|
||||
std::cout << "The single unit method must convert large\n"
|
||||
"or small values in other units to the base unit.\n"
|
||||
"This is both inefficient and inaccurate\n\n";
|
||||
|
||||
length::m<float> L1B = L1A;
|
||||
length::m<float> L2B = L2A;
|
||||
length::m<float> LrB = L1B + L2B;
|
||||
|
||||
std::cout << L1B << " + " << L2B << " = " << LrB << "\n\n";
|
||||
|
||||
std::cout << "In multiplication and division:\n\n";
|
||||
|
||||
area::fm2<float> ArA = L1A * L2A ;
|
||||
std::cout << L1A << " * " << L2A << " = " << ArA << "\n\n";
|
||||
|
||||
std::cout <<"similar problems arise\n\n";
|
||||
|
||||
area::m2<float> ArB = L1B * L2B;
|
||||
std::cout << L1B << " * " << L2B << "\n = " << ArB << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "This demo was originally posted on com.lang.c++.moderated in 2006\n";
|
||||
std::cout << "http://compgroups.net/comp.lang.c++.moderated/dimensional-analysis-units/51712\n";
|
||||
std::cout << "Here converted to use mpusz/units library.\n\n";
|
||||
|
||||
simple_quantities();
|
||||
quantities_with_typed_units();
|
||||
calcs_comparison();
|
||||
}
|
91
example/conversion_factor.cpp
Normal file
91
example/conversion_factor.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
|
||||
/*
|
||||
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/physical/si/length.h>
|
||||
|
||||
/*
|
||||
get conversion factor from one dimensionally equivalent
|
||||
quantity type to another
|
||||
*/
|
||||
|
||||
namespace {
|
||||
|
||||
template <
|
||||
units::Quantity Target,
|
||||
units::Quantity Source
|
||||
>
|
||||
requires units::equivalent_dim<typename Source::dimension,typename Target::dimension>
|
||||
constexpr inline
|
||||
std::common_type_t<
|
||||
typename Target::rep,
|
||||
typename Source::rep
|
||||
>
|
||||
conversion_factor(Target , Source)
|
||||
{
|
||||
// get quantities looking like inputs but with Q::rep that doesnt 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}}.count();
|
||||
}
|
||||
|
||||
// get at the units text of the quantity, without its numeric value
|
||||
auto inline constexpr units_str( units::Quantity const & q)
|
||||
{
|
||||
typedef std::remove_cvref_t<decltype(q)> qtype;
|
||||
return units::detail::unit_text<typename qtype::dimension, typename qtype::unit>();
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
namespace length{
|
||||
|
||||
template <typename Rep = double>
|
||||
using m = units::si::length<units::si::metre,Rep>;
|
||||
|
||||
template <typename Rep = double>
|
||||
using mm = units::si::length<units::si::millimetre,Rep>;
|
||||
}
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace units::si::literals;
|
||||
int main()
|
||||
{
|
||||
std::cout << "conversion factor in mpusz/units...\n\n";
|
||||
|
||||
length::m<> constexpr lengthA = 2.0m;
|
||||
length::mm<> constexpr lengthB = lengthA;
|
||||
|
||||
std::cout << "lengthA( " << lengthA << " ) and lengthB( " << lengthB << " )\n"
|
||||
"represent the same length in different units.\n\n";
|
||||
|
||||
std::cout << "therefore ratio lengthA / lengthB == " << lengthA / lengthB << "\n\n";
|
||||
|
||||
std::cout << "conversion factor from "
|
||||
"lengthA::unit of " << units_str(lengthA)
|
||||
<< " to lengthB::unit of " << units_str(lengthB) << " :\n\n"
|
||||
"lengthB.count( " << lengthB.count() << " ) == "
|
||||
"lengthA.count( " << lengthA.count() << " ) * "
|
||||
"conversion_factor( " << conversion_factor(lengthB, lengthA) << " )\n";
|
||||
}
|
86
example/sandbox.cpp
Normal file
86
example/sandbox.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
|
||||
/*
|
||||
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/physical/si/length.h>
|
||||
|
||||
/*
|
||||
get conversion factor from one dimensionally-equivalent
|
||||
quantity type to another
|
||||
*/
|
||||
|
||||
namespace {
|
||||
|
||||
|
||||
template <
|
||||
units::Quantity Target,
|
||||
units::Quantity Source
|
||||
>
|
||||
requires units::equivalent_dim<typename Source::dimension,typename Target::dimension>
|
||||
constexpr inline
|
||||
std::common_type_t<
|
||||
typename Target::rep,
|
||||
typename Source::rep
|
||||
>
|
||||
conversion_factor()
|
||||
{
|
||||
// get quantities looking like inputs but with Q::rep that doesnt 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}}.count();
|
||||
}
|
||||
|
||||
auto units_str( units::Quantity const & q)
|
||||
{
|
||||
typedef std::remove_cvref_t<decltype(q)> qtype;
|
||||
return units::detail::unit_text<typename qtype::dimension, typename qtype::unit>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
namespace length{
|
||||
using m = units::si::length<units::si::metre,double>;
|
||||
using mm = units::si::length<units::si::millimetre,double>;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace units::si::literals;
|
||||
int main()
|
||||
{
|
||||
length::m constexpr plankA = 2.0m;
|
||||
length::mm constexpr plankB = 1000.0mm;
|
||||
|
||||
std::cout << "ratio plankA / plankB = " << plankA / plankB << '\n';
|
||||
|
||||
std::cout << "conversion factor to convert from vS in " << units_str(plankA) ;
|
||||
std::cout << " to vT in " << units_str(plankB) << " : vT = vS * ";
|
||||
std::cout << conversion_factor<length::mm,length::m >() << '\n';
|
||||
|
||||
// can be evaluated at compile time
|
||||
static_assert(conversion_factor<length::mm,length::m>() == 1000,"error");
|
||||
|
||||
}
|
@ -83,12 +83,18 @@ struct dim_force : derived_dimension<Child, U, exp<M, 1>, exp<A, 1>> {};
|
||||
template<typename Child, Unit U, DimensionOf<dim_force> F, DimensionOf<dim_length> L>
|
||||
struct dim_energy : derived_dimension<Child, U, exp<F, 1>, exp<L, 1>> {};
|
||||
|
||||
template<typename Child, Unit U, DimensionOf<dim_mass> M, DimensionOf<dim_length> L>
|
||||
struct dim_density : derived_dimension<Child, U, exp<M, 1>, exp<L, -3>> {};
|
||||
|
||||
template<typename Child, Unit U, DimensionOf<dim_energy> E, DimensionOf<dim_time> T>
|
||||
struct dim_power : derived_dimension<Child, U, exp<E, 1>, exp<T, -1>> {};
|
||||
|
||||
template<typename Child, Unit U, DimensionOf<dim_power> P, DimensionOf<dim_electric_current> C>
|
||||
struct dim_voltage : derived_dimension<Child, U, exp<P, 1>, exp<C, -1>> {};
|
||||
|
||||
template <typename Child,Unit U, DimensionOf<dim_voltage> V, DimensionOf<dim_electric_current> C>
|
||||
struct dim_resistance : derived_dimension<Child,U, exp<V, 1>, exp<C , -1>> {};
|
||||
|
||||
template<typename Child, Unit U, DimensionOf<dim_time> T, DimensionOf<dim_electric_current> C>
|
||||
struct dim_electric_charge : derived_dimension<Child, U, exp<T, 1>, exp<C, 1>> {};
|
||||
|
||||
|
@ -33,6 +33,7 @@ struct dim_area : physical::dim_area<dim_area, square_metre, dim_length> {};
|
||||
|
||||
struct square_millimetre : deduced_unit<square_millimetre, dim_area, millimetre> {};
|
||||
struct square_centimetre : deduced_unit<square_centimetre, dim_area, centimetre> {};
|
||||
struct square_femtometre : deduced_unit<square_femtometre, dim_area, femtometre> {};
|
||||
struct square_kilometre : deduced_unit<square_kilometre, dim_area, kilometre> {};
|
||||
|
||||
struct hectare : deduced_unit<hectare, dim_area, hectometre> {};
|
||||
@ -54,6 +55,10 @@ constexpr auto operator"" mm2(long double l) { return area<square_millimetre, lo
|
||||
constexpr auto operator"" cm2(unsigned long long l) { return area<square_centimetre, std::int64_t>(l); }
|
||||
constexpr auto operator"" cm2(long double l) { return area<square_centimetre, long double>(l); }
|
||||
|
||||
// fm2
|
||||
constexpr auto operator"" fm2(unsigned long long l) { return area<square_femtometre, std::int64_t>(l); }
|
||||
constexpr auto operator"" fm2(long double l) { return area<square_femtometre, long double>(l); }
|
||||
|
||||
// km2
|
||||
constexpr auto operator"" km2(unsigned long long l) { return area<square_kilometre, std::int64_t>(l); }
|
||||
constexpr auto operator"" km2(long double l) { return area<square_kilometre, long double>(l); }
|
||||
|
@ -32,6 +32,11 @@ namespace units::si {
|
||||
|
||||
struct farad : named_unit<farad, "F", prefix> {};
|
||||
|
||||
struct millifarad : prefixed_unit<millifarad, milli, farad> {};
|
||||
struct microfarad : prefixed_unit<microfarad, micro, farad> {};
|
||||
struct nanofarad : prefixed_unit<nanofarad, nano, farad> {};
|
||||
struct picofarad : prefixed_unit<picofarad, pico, farad> {};
|
||||
|
||||
struct dim_capacitance : physical::dim_capacitance<dim_capacitance, farad, dim_electric_charge, dim_voltage> {};
|
||||
|
||||
template<Unit U, Scalar Rep = double>
|
||||
@ -43,6 +48,18 @@ inline namespace literals {
|
||||
constexpr auto operator""F(unsigned long long l) { return capacitance<farad, std::int64_t>(l); }
|
||||
constexpr auto operator""_F(long double l) { return capacitance<farad, long double>(l); }
|
||||
|
||||
constexpr auto operator""mF(unsigned long long l) { return capacitance<millifarad, std::int64_t>(l); }
|
||||
constexpr auto operator""mF(long double l) { return capacitance<millifarad, long double>(l); }
|
||||
|
||||
constexpr auto operator""uF(unsigned long long l) { return capacitance<microfarad, std::int64_t>(l); }
|
||||
constexpr auto operator""uF(long double l) { return capacitance<microfarad, long double>(l); }
|
||||
|
||||
constexpr auto operator""nF(unsigned long long l) { return capacitance<nanofarad, std::int64_t>(l); }
|
||||
constexpr auto operator""nF(long double l) { return capacitance<nanofarad, long double>(l); }
|
||||
|
||||
constexpr auto operator""pF(unsigned long long l) { return capacitance<picofarad, std::int64_t>(l); }
|
||||
constexpr auto operator""pF(long double l) { return capacitance<picofarad, long double>(l); }
|
||||
|
||||
} // namespace literals
|
||||
|
||||
} // namespace units::si
|
||||
|
47
src/include/units/physical/si/density.h
Normal file
47
src/include/units/physical/si/density.h
Normal file
@ -0,0 +1,47 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2018 Mateusz Pusz
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/physical/dimensions.h>
|
||||
#include <units/physical/si/mass.h>
|
||||
#include <units/physical/si/length.h>
|
||||
#include <units/physical/si/prefixes.h>
|
||||
#include <units/quantity.h>
|
||||
|
||||
namespace units::si {
|
||||
|
||||
struct kilogram_per_metre_cub : unit<kilogram_per_metre_cub> {};
|
||||
|
||||
struct dim_density : physical::dim_density<dim_density, kilogram_per_metre_cub,dim_mass, dim_length> {};
|
||||
|
||||
template<Unit U, Scalar Rep = double>
|
||||
using density = quantity<dim_density, U, Rep>;
|
||||
|
||||
inline namespace literals {
|
||||
|
||||
constexpr auto operator"" kg_per_m3(unsigned long long l) { return density<kilogram_per_metre_cub, std::int64_t>(l); }
|
||||
constexpr auto operator"" kg_per_m3(long double l) { return density<kilogram_per_metre_cub, long double>(l); }
|
||||
|
||||
} // namespace literals
|
||||
|
||||
} // namespace units::si
|
@ -34,8 +34,34 @@ struct centimetre : prefixed_unit<centimetre, centi, metre> {};
|
||||
struct decimetre : prefixed_unit<decimetre, deci, metre> {};
|
||||
struct hectometre : prefixed_unit<hectometre, hecto, metre> {};
|
||||
struct kilometre : prefixed_unit<kilometre, kilo, metre> {};
|
||||
struct femtometre : prefixed_unit<femtometre,femto,metre> {};
|
||||
|
||||
struct astronomical_unit : named_scaled_unit<astronomical_unit, "au", no_prefix, ratio<149'597'870'700>, metre> {};
|
||||
#if 0
|
||||
struct inch : named_scaled_unit<inch,"in(imp)",no_prefix,ratio<127,50,-2>, metre> {};
|
||||
struct foot : named_scaled_unit<foot,"ft(imp)",no_prefix,ratio<381,125,-1>, metre> {};
|
||||
struct yard : named_scaled_unit<yard,"yd(imp)",no_prefix,ratio<1143,125,-1>, metre> {};
|
||||
#endif
|
||||
struct foot_us : named_scaled_unit<foot_us,"ft(us)",no_prefix,ratio<1524003,500000,-1>, metre> {};
|
||||
struct fathom : named_scaled_unit<fathom,"fathom(imp)",no_prefix,ratio<1143,625>, metre> {};
|
||||
struct fathom_us : named_scaled_unit<fathom_us,"fathom(us)",no_prefix,ratio<457201,250000>,metre> {};
|
||||
struct chain : named_scaled_unit<chain,"ch(us)",no_prefix,ratio<502921,250000,1>, metre> {};
|
||||
|
||||
#if 0
|
||||
struct thou : named_scaled_unit<thou,"thou",no_prefix,ratio<127,50,-5>,metre> {};
|
||||
#endif
|
||||
struct mil : named_scaled_unit<mil,"mil",no_prefix,ratio<127,50,-5>,metre> {};
|
||||
|
||||
struct light_year : named_scaled_unit<light_year,"ly(iau)",no_prefix,ratio<946073,100000,15>,metre> {};
|
||||
struct mile : named_scaled_unit<mile,"mi(imp)",no_prefix,ratio<25146,15625,3>,metre> {};
|
||||
struct nautical_mile : named_scaled_unit<nautical_mile,"mi(naut)",no_prefix, ratio<463,250,3>,metre> {};
|
||||
struct parsec : named_scaled_unit<parsec,"pc(iau)",no_prefix,ratio<1542839,500000,16>,metre> {};
|
||||
struct pica_comp : named_scaled_unit<pica_comp,"pica(comp)",no_prefix,ratio<4233333,1000000,-3>,metre> {};
|
||||
struct pica_prn : named_scaled_unit<pica_prn,"pica(prn)",no_prefix,ratio<2108759,500000,-3>,metre> {};
|
||||
struct point_comp : named_scaled_unit<point_comp,"point(comp)",no_prefix,ratio<1763889,500000,-4>,metre> {};
|
||||
struct point_prn : named_scaled_unit<point_prn,"point(prn)", no_prefix, ratio<1757299,500000,-4>,metre> {};
|
||||
struct rod : named_scaled_unit<rod,"rd(us)",no_prefix, ratio<502921,100000>,metre> {};
|
||||
struct angstrom : named_scaled_unit<angstrom,"angstrom(nru)",no_prefix,ratio<1,1,-10>,metre> {};
|
||||
|
||||
struct dim_length : physical::dim_length<metre> {};
|
||||
|
||||
@ -52,6 +78,10 @@ constexpr auto operator"" m(long double l) { return length<metre, long double>(l
|
||||
constexpr auto operator"" mm(unsigned long long l) { return length<millimetre, std::int64_t>(l); }
|
||||
constexpr auto operator"" mm(long double l) { return length<millimetre, long double>(l); }
|
||||
|
||||
// fm
|
||||
constexpr auto operator"" fm(unsigned long long l) { return length<femtometre, std::int64_t>(l); }
|
||||
constexpr auto operator"" fm(long double l) { return length<femtometre, long double>(l); }
|
||||
|
||||
// cm
|
||||
constexpr auto operator"" cm(unsigned long long l) { return length<centimetre, std::int64_t>(l); }
|
||||
constexpr auto operator"" cm(long double l) { return length<centimetre, long double>(l); }
|
||||
@ -71,6 +101,64 @@ constexpr auto operator"" km(long double l) { return length<kilometre, long doub
|
||||
// au
|
||||
constexpr auto operator"" au(unsigned long long l) { return length<astronomical_unit, std::int64_t>(l); }
|
||||
constexpr auto operator"" au(long double l) { return length<astronomical_unit, long double>(l); }
|
||||
#if 0
|
||||
constexpr auto operator"" q_in(unsigned long long l) { return length<inch, std::int64_t>(l); }
|
||||
constexpr auto operator"" q_in(long double l) { return length<inch, long double>(l); }
|
||||
|
||||
constexpr auto operator"" q_ft(unsigned long long l) { return length<foot, std::int64_t>(l); }
|
||||
constexpr auto operator"" q_ft(long double l) { return length<foot, long double>(l); }
|
||||
|
||||
constexpr auto operator"" q_yd(unsigned long long l) { return length<yard, std::int64_t>(l); }
|
||||
constexpr auto operator"" q_yd(long double l) { return length<yard, long double>(l); }
|
||||
#endif
|
||||
constexpr auto operator"" ft_us(unsigned long long l) { return length<foot_us, std::int64_t>(l); }
|
||||
constexpr auto operator"" ft_us(long double l) { return length<foot_us, long double>(l); }
|
||||
|
||||
constexpr auto operator"" fathom(unsigned long long l) { return length<fathom, std::int64_t>(l); }
|
||||
constexpr auto operator"" fathom(long double l) { return length<fathom, long double>(l); }
|
||||
|
||||
constexpr auto operator"" fathom_us(unsigned long long l) { return length<fathom_us, std::int64_t>(l); }
|
||||
constexpr auto operator"" fathom_us(long double l) { return length<fathom_us, long double>(l); }
|
||||
|
||||
constexpr auto operator"" ch(unsigned long long l) { return length<chain, std::int64_t>(l); }
|
||||
constexpr auto operator"" ch(long double l) { return length<chain, long double>(l); }
|
||||
|
||||
#if 0
|
||||
constexpr auto operator"" thou(unsigned long long l) { return length<thou, std::int64_t>(l); }
|
||||
constexpr auto operator"" thou(long double l) { return length<thou, long double>(l); }
|
||||
#endif
|
||||
constexpr auto operator"" mil(unsigned long long l) { return length<mil, std::int64_t>(l); }
|
||||
constexpr auto operator"" mil(long double l) { return length<mil, long double>(l); }
|
||||
|
||||
constexpr auto operator"" ly(unsigned long long l) { return length<light_year, std::int64_t>(l); }
|
||||
constexpr auto operator"" ly(long double l) { return length<light_year, long double>(l); }
|
||||
|
||||
constexpr auto operator"" q_mi(unsigned long long l) { return length<mile, std::int64_t>(l); }
|
||||
constexpr auto operator"" q_mi(long double l) { return length<mile, long double>(l); }
|
||||
|
||||
constexpr auto operator"" pc(unsigned long long l) { return length<parsec, std::int64_t>(l); }
|
||||
constexpr auto operator"" pc(long double l) { return length<parsec, long double>(l); }
|
||||
|
||||
constexpr auto operator"" mi_naut(unsigned long long l) { return length<nautical_mile, std::int64_t>(l); }
|
||||
constexpr auto operator"" mi_naut(long double l) { return length<nautical_mile, long double>(l); }
|
||||
|
||||
constexpr auto operator"" pica_comp(unsigned long long l) { return length<pica_comp, std::int64_t>(l); }
|
||||
constexpr auto operator"" pica_comp(long double l) { return length<pica_comp, long double>(l); }
|
||||
|
||||
constexpr auto operator"" pica_prn(unsigned long long l) { return length<pica_prn, std::int64_t>(l); }
|
||||
constexpr auto operator"" pica_prn(long double l) { return length<pica_prn, long double>(l); }
|
||||
|
||||
constexpr auto operator"" point_comp(unsigned long long l) { return length<point_comp, std::int64_t>(l); }
|
||||
constexpr auto operator"" point_comp(long double l) { return length<point_comp, long double>(l); }
|
||||
|
||||
constexpr auto operator"" point_prn(unsigned long long l) { return length<point_prn, std::int64_t>(l); }
|
||||
constexpr auto operator"" point_prn(long double l) { return length<point_prn, long double>(l); }
|
||||
|
||||
constexpr auto operator"" rd(unsigned long long l) { return length<rod, std::int64_t>(l); }
|
||||
constexpr auto operator"" rd(long double l) { return length<rod, long double>(l); }
|
||||
|
||||
constexpr auto operator"" angstrom(unsigned long long l) { return length<angstrom, std::int64_t>(l); }
|
||||
constexpr auto operator"" angstrom(long double l) { return length<angstrom, long double>(l); }
|
||||
|
||||
} // namespace literals
|
||||
|
||||
|
63
src/include/units/physical/si/resistance.h
Normal file
63
src/include/units/physical/si/resistance.h
Normal file
@ -0,0 +1,63 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2018 Mateusz Pusz
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <units/physical/dimensions.h>
|
||||
#include <units/physical/si/voltage.h>
|
||||
#include <units/physical/si/current.h>
|
||||
#include <units/physical/si/prefixes.h>
|
||||
#include <units/quantity.h>
|
||||
|
||||
namespace units::si {
|
||||
|
||||
struct ohm : named_unit<ohm, "R", prefix> {};
|
||||
struct milliohm : prefixed_unit<milliohm, milli, ohm> {};
|
||||
struct kiloohm : prefixed_unit<kiloohm, kilo, ohm> {};
|
||||
struct megaohm : prefixed_unit<megaohm, mega, ohm> {};
|
||||
|
||||
struct dim_resistance : physical::dim_resistance<dim_resistance, ohm, dim_voltage, dim_electric_current> {};
|
||||
|
||||
template<Unit U, Scalar Rep = double>
|
||||
using resistance = quantity<dim_resistance, U, Rep>;
|
||||
|
||||
inline namespace literals {
|
||||
|
||||
//R
|
||||
constexpr auto operator""R(unsigned long long l) { return resistance<ohm, std::int64_t>(l); }
|
||||
constexpr auto operator""_R(long double l) { return resistance<ohm, long double>(l); }
|
||||
|
||||
//mR
|
||||
constexpr auto operator""mR(unsigned long long l) { return resistance<milliohm, std::int64_t>(l); }
|
||||
constexpr auto operator""mR(long double l) { return resistance<milliohm, long double>(l); }
|
||||
|
||||
//kR
|
||||
constexpr auto operator""kR(unsigned long long l) { return resistance<kiloohm, std::int64_t>(l); }
|
||||
constexpr auto operator""kR(long double l) { return resistance<kiloohm, long double>(l); }
|
||||
|
||||
// MR
|
||||
constexpr auto operator""MR(unsigned long long l) { return resistance<megaohm, std::int64_t>(l); }
|
||||
constexpr auto operator""MR(long double l) { return resistance<megaohm, long double>(l); }
|
||||
|
||||
} // namespace literals
|
||||
|
||||
} // namespace units::si
|
@ -31,6 +31,10 @@
|
||||
namespace units::si {
|
||||
|
||||
struct volt : named_unit<volt, "V", prefix> {};
|
||||
struct millivolt : prefixed_unit<millivolt, milli, volt> {};
|
||||
struct microvolt : prefixed_unit<microvolt, micro, volt> {};
|
||||
struct nanovolt : prefixed_unit<nanovolt, nano, volt> {};
|
||||
struct picovolt : prefixed_unit<picovolt, pico, volt> {};
|
||||
|
||||
struct dim_voltage : physical::dim_voltage<dim_voltage, volt, dim_power, dim_electric_current> {};
|
||||
|
||||
@ -43,6 +47,18 @@ inline namespace literals {
|
||||
constexpr auto operator""V(unsigned long long l) { return voltage<volt, std::int64_t>(l); }
|
||||
constexpr auto operator""V(long double l) { return voltage<volt, long double>(l); }
|
||||
|
||||
constexpr auto operator""mV(unsigned long long l) { return voltage<millivolt, std::int64_t>(l); }
|
||||
constexpr auto operator""mV(long double l) { return voltage<millivolt, long double>(l); }
|
||||
|
||||
constexpr auto operator""uV(unsigned long long l) { return voltage<microvolt, std::int64_t>(l); }
|
||||
constexpr auto operator""uV(long double l) { return voltage<microvolt, long double>(l); }
|
||||
|
||||
constexpr auto operator""nV(unsigned long long l) { return voltage<nanovolt, std::int64_t>(l); }
|
||||
constexpr auto operator""nV(long double l) { return voltage<nanovolt, long double>(l); }
|
||||
|
||||
constexpr auto operator""pV(unsigned long long l) { return voltage<picovolt, std::int64_t>(l); }
|
||||
constexpr auto operator""pV(long double l) { return voltage<picovolt, long double>(l); }
|
||||
|
||||
} // namespace literals
|
||||
|
||||
} // namespace units::si
|
||||
|
Reference in New Issue
Block a user