2019-12-06 12:18:39 +01:00
|
|
|
// 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.
|
|
|
|
|
2019-12-11 21:38:44 +01:00
|
|
|
#include <units/physical/cgs/velocity.h>
|
2019-12-17 12:29:19 +01:00
|
|
|
#include <units/physical/si/velocity.h>
|
|
|
|
#include <units/physical/us/velocity.h>
|
2019-12-06 12:18:39 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr units::si::velocity<units::si::metre_per_second, int>
|
|
|
|
fixed_int_si_avg_speed(units::si::length<units::si::metre, int> d,
|
|
|
|
units::si::time<units::si::second, int> t)
|
|
|
|
{
|
|
|
|
return d / t;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr units::si::velocity<units::si::metre_per_second>
|
|
|
|
fixed_double_si_avg_speed(units::si::length<units::si::metre> d,
|
|
|
|
units::si::time<units::si::second> t)
|
|
|
|
{
|
|
|
|
return d / t;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U1, typename R1, typename U2, typename R2>
|
|
|
|
constexpr units::Velocity AUTO si_avg_speed(units::si::length<U1, R1> d,
|
|
|
|
units::si::time<U2, R2> t)
|
|
|
|
{
|
|
|
|
return d / t;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr units::Velocity AUTO avg_speed(units::Length AUTO d, units::Time AUTO t)
|
|
|
|
{
|
|
|
|
return d / t;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<units::Length D, units::Time T, units::Velocity V>
|
|
|
|
void print_result(D distance, T duration, V velocity)
|
|
|
|
{
|
2019-12-11 21:38:44 +01:00
|
|
|
const auto result_in_kmph = units::quantity_cast<units::si::velocity<units::si::kilometre_per_hour>>(velocity);
|
2019-12-06 12:18:39 +01:00
|
|
|
std::cout << "Average speed of a car that makes " << distance << " in "
|
|
|
|
<< duration << " is " << result_in_kmph << ".\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void example()
|
|
|
|
{
|
|
|
|
using namespace units;
|
|
|
|
|
|
|
|
// SI (int)
|
|
|
|
{
|
2019-12-11 21:38:44 +01:00
|
|
|
using namespace units::si::literals;
|
2019-12-06 12:18:39 +01:00
|
|
|
constexpr Length AUTO distance = 220km; // constructed from a UDL
|
|
|
|
constexpr si::time<si::hour, int> duration(2); // constructed from a value
|
|
|
|
|
|
|
|
std::cout << "SI units with 'int' as representation\n";
|
|
|
|
|
|
|
|
print_result(distance, duration, fixed_int_si_avg_speed(distance, duration));
|
|
|
|
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
2019-12-11 21:38:44 +01:00
|
|
|
print_result(distance, duration, si_avg_speed(distance, duration));
|
|
|
|
print_result(distance, duration, avg_speed(distance, duration));
|
2019-12-06 12:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SI (double)
|
|
|
|
{
|
2019-12-11 21:38:44 +01:00
|
|
|
using namespace units::si::literals;
|
2019-12-06 12:18:39 +01:00
|
|
|
constexpr Length AUTO distance = 220.km; // constructed from a UDL
|
|
|
|
constexpr si::time<si::hour> duration(2); // constructed from a value
|
|
|
|
|
|
|
|
std::cout << "\nSI units with 'double' as representation\n";
|
|
|
|
|
|
|
|
// conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed
|
|
|
|
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<int>(distance), quantity_cast<int>(duration)));
|
|
|
|
|
|
|
|
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
|
|
|
print_result(distance, duration, si_avg_speed(distance, duration));
|
|
|
|
print_result(distance, duration, avg_speed(distance, duration));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Customary Units (int)
|
|
|
|
{
|
Split the various non-si length units into their own namespaces.
new namespaces are
international ( combination of us and imperial + Canada etc)
iau (https://www.iau.org/
imperial ( old imperial units)
typographical ( for sizes of printing fonts etc)
These namespace are based on some research , mainly on wikipedia.
Look in src/include/units/physical/si/length.h to see links to see the references to documentation justifying the change.
Unfortunately there are 3 foot units for example, an old imperial version, an old us version and an international version, which is more recent and attempts to unify the two previous ones. All versions have slight changes in value, so I opted to use the international version
The main change in the layout is that inch,foot and yard have been moved from us to international.
With this modification, I also modified the physical/us/area.hpp, volume.hpp and volume.hpp headers to refer to the international units.
This may not be correct, but if the modified us::foot (rather than international foot is used as a basis for these units, then
there is a ratio integer overflow during compilation, probably due to taking 3rd power of a ratio. After this commit. I will try to show that on another branch.
2020-01-10 02:03:27 +00:00
|
|
|
using namespace units::international::literals;
|
2019-12-06 12:18:39 +01:00
|
|
|
constexpr Length AUTO distance = 140mi; // constructed from a UDL
|
|
|
|
constexpr si::time<si::hour, int> duration(2); // constructed from a value
|
|
|
|
|
|
|
|
std::cout << "\nUS Customary Units with 'int' as representation\n";
|
|
|
|
|
|
|
|
// it is not possible to make a lossless conversion of miles to meters on an integral type
|
|
|
|
// (explicit cast needed)
|
|
|
|
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<si::metre>(distance), duration));
|
|
|
|
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
2019-12-11 21:38:44 +01:00
|
|
|
print_result(distance, duration, si_avg_speed(distance, duration));
|
|
|
|
print_result(distance, duration, avg_speed(distance, duration));
|
2019-12-06 12:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Customary Units (double)
|
|
|
|
{
|
Split the various non-si length units into their own namespaces.
new namespaces are
international ( combination of us and imperial + Canada etc)
iau (https://www.iau.org/
imperial ( old imperial units)
typographical ( for sizes of printing fonts etc)
These namespace are based on some research , mainly on wikipedia.
Look in src/include/units/physical/si/length.h to see links to see the references to documentation justifying the change.
Unfortunately there are 3 foot units for example, an old imperial version, an old us version and an international version, which is more recent and attempts to unify the two previous ones. All versions have slight changes in value, so I opted to use the international version
The main change in the layout is that inch,foot and yard have been moved from us to international.
With this modification, I also modified the physical/us/area.hpp, volume.hpp and volume.hpp headers to refer to the international units.
This may not be correct, but if the modified us::foot (rather than international foot is used as a basis for these units, then
there is a ratio integer overflow during compilation, probably due to taking 3rd power of a ratio. After this commit. I will try to show that on another branch.
2020-01-10 02:03:27 +00:00
|
|
|
using namespace units::international::literals;
|
2019-12-06 12:18:39 +01:00
|
|
|
constexpr Length AUTO distance = 140.mi; // constructed from a UDL
|
|
|
|
constexpr si::time<si::hour> duration(2); // constructed from a value
|
|
|
|
|
|
|
|
std::cout << "\nUS Customary Units with 'double' as representation\n";
|
|
|
|
|
|
|
|
// conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed
|
|
|
|
// also it is not possible to make a lossless conversion of miles to meters on an integral type
|
|
|
|
// (explicit cast needed)
|
2019-12-08 16:24:29 +01:00
|
|
|
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<si::length<si::metre, int>>(distance), quantity_cast<int>(duration)));
|
2019-12-06 12:18:39 +01:00
|
|
|
|
|
|
|
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
|
|
|
print_result(distance, duration, si_avg_speed(distance, duration));
|
|
|
|
print_result(distance, duration, avg_speed(distance, duration));
|
|
|
|
}
|
2019-12-11 21:38:44 +01:00
|
|
|
|
|
|
|
// CGS (int)
|
|
|
|
{
|
|
|
|
using namespace units::cgs::literals;
|
|
|
|
constexpr Length AUTO distance = 22'000'000cm; // constructed from a UDL
|
|
|
|
constexpr cgs::time<si::hour, int> duration(2); // constructed from a value
|
|
|
|
|
|
|
|
std::cout << "\nCGS units with 'int' as representation\n";
|
|
|
|
|
|
|
|
// it is not possible to make a lossless conversion of centimeters to meters on an integral type
|
|
|
|
// (explicit cast needed)
|
|
|
|
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<si::metre>(distance), duration));
|
|
|
|
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
|
|
|
|
|
|
|
// not possible to convert both a dimension and a unit with implicit cast
|
|
|
|
print_result(distance, duration, si_avg_speed(quantity_cast<si::dim_length>(distance), duration));
|
|
|
|
|
|
|
|
print_result(distance, duration, avg_speed(distance, duration));
|
|
|
|
}
|
|
|
|
|
|
|
|
// CGS (double)
|
|
|
|
{
|
|
|
|
using namespace units::cgs::literals;
|
|
|
|
constexpr Length AUTO distance = 22'000'000.cm; // constructed from a UDL
|
|
|
|
constexpr cgs::time<si::hour> duration(2); // constructed from a value
|
|
|
|
|
|
|
|
std::cout << "\nCGS units with 'double' as representation\n";
|
|
|
|
|
|
|
|
// conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed
|
|
|
|
// it is not possible to make a lossless conversion of centimeters to meters on an integral type
|
|
|
|
// (explicit cast needed)
|
|
|
|
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<si::length<si::metre, int>>(distance), quantity_cast<int>(duration)));
|
|
|
|
|
|
|
|
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
|
|
|
|
|
|
|
|
// not possible to convert both a dimension and a unit with implicit cast
|
|
|
|
print_result(distance, duration, si_avg_speed(quantity_cast<si::dim_length>(distance), duration));
|
|
|
|
|
|
|
|
print_result(distance, duration, avg_speed(distance, duration));
|
|
|
|
}
|
|
|
|
|
2019-12-06 12:18:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
example();
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex) {
|
|
|
|
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
std::cerr << "Unhandled unknown exception caught\n";
|
|
|
|
}
|
|
|
|
}
|