AuROXTL/Include/auROXTL/auUnitUtils.hpp
2022-04-01 05:06:53 +01:00

112 lines
2.7 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: auUnitUtils.hpp
Date: 2022-3-13
Author: Reece
***/
#pragma once
/**
It is not unusual for one to desire a unit-conversion constant when the developer understands
[*] An FPU isn't available
[*] Using a framework to normalize to a base unit(?), apply a translation, and return the desired reuslt is considered overkill
These functions are not to be converted into such a convoluted templated potentially-constexpr castable mess
The intention is to keep these utility functions as simple as the dumb operations they implement
*/
template <class Inch, class Precision = Inch, class Feet>
constexpr const Inch AuFeetToInch(Feet feet)
{
return Inch(Precision(feet) * Precision(12));
}
template <class Feet, class Precision = Feet, class Inch>
constexpr const Feet AuInchToFeet(Inch inch)
{
return Feet(Precision(inch) / Precision(12));
}
template <class Inch, class CM>
constexpr const Inch AuCMToInch(CM cm)
{
return Inch(Inch(cm) * Inch(100) / Inch(254));
}
template <class CM, class Inch>
constexpr const CM AuInchToCM(Inch inch)
{
return CM(Inch(inch) * Inch(254) / Inch(100));
}
template <class M, class CM>
constexpr const M AuCMToM(CM cm)
{
return M(CM(cm) / CM(100));
}
template <class CM, class M>
constexpr const CM AuMToCM(M m)
{
return CM(CM(m) * CM(100));
}
template <class CM, class MM>
constexpr const CM AuMMToCM(MM mm)
{
return CM(MM(mm) / MM(10));
}
template <class MM, class CM>
constexpr const MM AuCMToMM(CM cm)
{
return MM(MM(cm) * MM(10));
}
template <class S, class MS>
constexpr const S AuMSToS(MS ms)
{
return S(MS(ms) / MS(1'000));
}
template <class MS, class S>
constexpr const MS AuSToMS(S s)
{
return MS(MS(s) * MS(1'000));
}
template <class MS, class NS>
constexpr const MS AuNSToMS(NS ns)
{
return MS(NS(ns) / NS(1'000'000));
}
template <class NS, class MS>
constexpr const NS AuMSToNS(MS ms)
{
return NS(NS(ms) * NS(1'000'000));
}
template <class F, class Precision = AuInt16, class C>
constexpr const F AuCToF(C c)
{
return F(Precision((Precision(c) * Precision(100)) / Precision(5) * Precision(9) + Precision(3200)) / Precision(100));
}
template <class C, class Precision = AuInt16, class F>
constexpr const C AuFToC(F f)
{
return C(((Precision(f) * Precision(100)) - Precision(3200)) * Precision(5) / Precision(9) / Precision(100));
}
template <class K, class Precision = AuInt16, class C>
constexpr const K AuCToK(C c)
{
return C(Precision(c) + Precision(273.15));
}
template <class C, class Precision = AuInt16, class K>
constexpr const C AuKToC(K k)
{
return C(Precision(k) - Precision(273.15));
}