Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ratio.h Source File

ratio.h

Go to the documentation of this file.
00001 ///\file
00002 
00003 /******************************************************************************
00004 The MIT License(MIT)
00005 
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 
00010 Copyright(c) 2016 jwellbelove
00011 
00012 Permission is hereby granted, free of charge, to any person obtaining a copy
00013 of this software and associated documentation files(the "Software"), to deal
00014 in the Software without restriction, including without limitation the rights
00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00016 copies of the Software, and to permit persons to whom the Software is
00017 furnished to do so, subject to the following conditions :
00018 
00019 The above copyright notice and this permission notice shall be included in all
00020 copies or substantial portions of the Software.
00021 
00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00028 SOFTWARE.
00029 ******************************************************************************/
00030 
00031 #ifndef __ETL_RATIO__
00032 #define __ETL_RATIO__
00033 
00034 #include <stdint.h>
00035 
00036 ///\defgroup ratio ratio
00037 ///\ingroup utilities
00038 
00039 namespace etl
00040 {
00041   template <const size_t NUM, const size_t DEN = 1>
00042   struct ratio
00043   {
00044     static const std::intmax_t num = NUM;
00045     static const std::intmax_t den = DEN;
00046   };
00047 
00048   #if INT_MAX > INT32_MAX
00049     typedef ratio<1, 1000000000000000000000000> yocto;
00050     typedef ratio<1, 1000000000000000000000>    zepto;
00051     typedef ratio<1, 1000000000000000000>       atto
00052     typedef ratio<1, 1000000000000000>          femto
00053     typedef ratio<1, 1000000000000>             pico;
00054   #endif
00055 
00056   #if (INT_MAX >= INT32_MAX)
00057     typedef ratio<1, 1000000000>                nano;
00058     typedef ratio<1, 1000000>                   micro;
00059   #endif
00060 
00061   #if (INT_MAX >= INT16_MAX)
00062     typedef ratio<1, 1000>                      milli;
00063     typedef ratio<1, 100>                       centi;
00064     typedef ratio<1, 10>                        deci;
00065     typedef ratio<10, 1>                        deca;
00066     typedef ratio<100, 1>                       hecto
00067     typedef ratio<1000, 1>                      kilo
00068   #endif
00069 
00070   #if (INT_MAX >= INT32_MAX)
00071     typedef ratio<1000000, 1>                   mega;
00072     typedef ratio<1000000000, 1>                giga;
00073   #endif
00074 
00075   #if INT_MAX > INT32_MAX
00076     typedef ratio<1000000000000, 1>             tera;
00077     typedef ratio<1000000000000000, 1>          peta;
00078     typedef ratio<1000000000000000000, 1>       exa;
00079     typedef ratio<1000000000000000000000, 1>    zetta;
00080     typedef ratio<1000000000000000000000000, 1> yotta;
00081   #endif
00082 
00083   /// An approximation of PI to 6 digits.
00084   typedef ratio<355, 113> ratio_pi;
00085 
00086   /// An approximation of root 2.
00087   typedef ratio<239, 169> ratio_root2;
00088 
00089   /// An approximation of e.
00090   typedef ratio<326, 120> ratio_e;
00091 }
00092 
00093 #endif
00094 
00095