// ratio standard header
#ifndef _RATIO_
#define _RATIO_
#include <yvals.h>
#include <stdint.h>
#include <type_traits>
_STD_BEGIN
template<bool _Pred>
	struct _Static_assert_failed;

template<>
	struct _Static_assert_failed<true>
	{	// tests assert statically
	};

template<int>
	struct _Test_assert
	{	// tests if assert valid
	};

 #define _CHECK_ASSERTY(expr, line)	\
	typedef _Test_assert<sizeof(_Static_assert_failed<bool(expr)>)> \
		_Static_assert_type ## line
 #define _CHECK_ASSERTX(expr, line)	_CHECK_ASSERTY(expr, line)
 #define _CHECK_ASSERT(expr, mesg)	_CHECK_ASSERTX(expr, __LINE__)

#define _ABS(a)	((a) < 0 ? -(a) : (a))
#define _MULT_OK(a, b)	(_ABS(a) <= INTMAX_MAX / _ABS(b))
#define _ADD_OK(a, b)	((a) <= 0 && 0 <= (b) || (b) < 0 && 0 < (a) \
	|| _ABS(b) <= INTMAX_MAX - _ABS(a))

	// CLASS TEMPLATE _Abs
template<intmax_t _Val>
	struct _Abs
	{   // computes absolute value of _Val
	static const intmax_t _Value = _Val < 0 ? -_Val : _Val;
	};

	// CLASS TEMPLATE _Gcd
template<intmax_t _Ax,
	intmax_t _Bx>
	struct _GcdX
	{   // computes greatest common denominator of _Ax and _Bx
	static const intmax_t _Value = _GcdX<_Bx, _Ax % _Bx>::_Value;
	};

template<intmax_t _Ax>
	struct _GcdX<_Ax, 0>
	{   // compute greatest common denominator of _Ax and 0
	static const intmax_t _Value = _Ax;
	};

template<intmax_t _Ax,
	intmax_t _Bx>
	struct _Gcd
	{   // computes greatest common denominator of _Ax and _Bx
	static const intmax_t _Value =
		_GcdX<_Abs<_Ax>::_Value, _Abs<_Bx>::_Value>::_Value;
	};

	// CLASS TEMPLATE ratio
template<intmax_t _Nx, intmax_t _Dx = 1>
	struct ratio
	{   // holds the ratio of _Nx to _Dx
	_CHECK_ASSERT(_Dx != 0, "zero denominator");
	_CHECK_ASSERT(-INTMAX_MAX <= _Nx && -INTMAX_MAX <= _Dx,
		"value not representable");

	static const intmax_t num = _Nx < 0 && _Dx < 0 || 0 <= _Nx && 0 <= _Dx
		? _Abs<_Nx>::_Value
			/ _Gcd<_Abs<_Nx>::_Value, _Abs<_Dx>::_Value>::_Value
		: -_Abs<_Nx>::_Value
			/ _Gcd<_Abs<_Nx>::_Value, _Abs<_Dx>::_Value>::_Value;
	static const intmax_t den = _Abs<_Dx>::_Value
		/ _Gcd<_Abs<_Nx>::_Value, _Abs<_Dx>::_Value>::_Value;
	};

	// CLASS TEMPLATE ratio_add
template<intmax_t _N1,
	intmax_t _D1,
	intmax_t _N2,
	intmax_t _D2,
	intmax_t _Gx>
	struct _Ratio_add
	{	// scale large values by gcd(_D1, _D2) to help avoid overflow
	_CHECK_ASSERT(_MULT_OK(_N1, _D2), "integer arithmetic overflow");
	_CHECK_ASSERT(_MULT_OK(_N2, _D1), "integer arithmetic overflow");
	_CHECK_ASSERT(_ADD_OK(_N1 * _D2, _N2 * _D1),
		"integer arithmetic overflow");
	_CHECK_ASSERT(_MULT_OK(_D1, _D2), "integer arithmetic overflow");
	_CHECK_ASSERT(_MULT_OK(_D1 * _D2, _Gx), "integer arithmetic overflow");

	typedef ratio<_N1 * _D2 + _N2 * _D1, _D1 * _D2 * _Gx> _Type;
	};

template<class _R1,
	class _R2>
	struct ratio_add;

template<intmax_t _N1,
	intmax_t _D1,
	intmax_t _N2,
	intmax_t _D2>
	struct ratio_add<ratio<_N1, _D1>, ratio<_N2, _D2> >
	{   // add two ratios
	static const intmax_t _Gx = _Gcd<_D1, _D2>::_Value;

	typedef typename _Ratio_add<_N1, _D1 / _Gx,
		_N2, _D2 / _Gx, _Gx>::_Type type;
	};

	// CLASS TEMPLATE ratio_subtract
template<class _R1,
	class _R2>
	struct ratio_subtract;

template<intmax_t _N1,
	intmax_t _D1,
	intmax_t _N2,
	intmax_t _D2>
	struct ratio_subtract<ratio<_N1, _D1>, ratio<_N2, _D2> >
	{   // subtract two ratios
	typedef typename ratio_add<ratio<_N1, _D1>,
		ratio<-_N2, _D2> >::type type;
	};

	// CLASS TEMPLATE ratio_multiply
template<intmax_t _N1,
	intmax_t _D1,
	intmax_t _N2,
	intmax_t _D2>
	struct _Ratio_multiply
	{	// multiply two ratios
	_CHECK_ASSERT(_MULT_OK(_N1, _N2), "integer arithmetic overflow");
	_CHECK_ASSERT(_MULT_OK(_D1, _D2), "integer arithmetic overflow");

	typedef ratio<_N1 * _N2, _D1 * _D2> _Type;
	};

template<class _R1,
	class _R2>
	struct ratio_multiply;

template<intmax_t _N1,
	intmax_t _D1,
	intmax_t _N2,
	intmax_t _D2>
	struct ratio_multiply<ratio<_N1, _D1>, ratio<_N2, _D2> >
	{   // multiply two ratios
	static const intmax_t _G1 = _Gcd<_N1, _D2>::_Value;
	static const intmax_t _G2 = _Gcd<_N2, _D1>::_Value;

	typedef typename _Ratio_multiply<_N1 / _G1, _D1 / _G2,
		_N2 / _G2, _D2 / _G1>::_Type type;
	};

	// CLASS TEMPLATE ratio_divide
template<class _R1,
	class _R2>
	struct ratio_divide;

template<intmax_t _N1,
	intmax_t _D1,
	intmax_t _N2,
	intmax_t _D2>
	struct ratio_divide<ratio<_N1, _D1>, ratio<_N2, _D2> >
	{   // divide two ratios
	_CHECK_ASSERT(_N2 != 0, "division by zero");

	typedef typename ratio_multiply<ratio<_N1, _D1>,
		ratio<_D2, _N2> >::type type;
	};

	// CLASS TEMPLATE ratio_equal
template<class _R1,
	class _R2>
	struct ratio_equal;

template<intmax_t _N1,
	intmax_t _D1,
	intmax_t _N2,
	intmax_t _D2>
	struct ratio_equal<ratio<_N1, _D1>, ratio<_N2, _D2> >
		: public tr1::integral_constant<bool, _N1 == _N2 && _D1 == _D2>
	{	// tests if ratio == ratio
	};

	// CLASS TEMPLATE ratio_not_equal
template<class _R1,
	class _R2>
	struct ratio_not_equal
		: public tr1::integral_constant<bool, !ratio_equal<_R1, _R2>::value>
	{	// tests if ratio != ratio
	};

	// CLASS TEMPLATE ratio_less
template<class _R1,
	class _R2>
	struct ratio_less;

template<intmax_t _N1,
	intmax_t _D1,
	intmax_t _N2,
	intmax_t _D2>
	struct ratio_less<ratio<_N1, _D1>, ratio<_N2, _D2> >
		: public tr1::integral_constant<bool, _N1 * _D2 < _D1 * _N2>
	{	// tests if ratio < ratio
	};

	// CLASS TEMPLATE ratio_less_equal
template<class _R1,
	class _R2>
	struct ratio_less_equal
		: public tr1::integral_constant<bool, !ratio_less<_R2, _R1>::value>
	{	// tests if ratio <= ratio
	};

	// CLASS TEMPLATE ratio_greater
template<class _R1,
	class _R2>
	struct ratio_greater
		: public tr1::integral_constant<bool, ratio_less<_R2, _R1>::value>
	{	// tests if ratio > ratio
	};

	// CLASS TEMPLATE ratio_greater_equal
template<class _R1,
	class _R2>
	struct ratio_greater_equal
		: public tr1::integral_constant<bool, !ratio_less<_R1, _R2>::value>
	{	// tests if ratio >= ratio
	};

	// CONSTANTS

 #if 1000000 <= INTMAX_MAX / 1000000000 / 1000000000
typedef ratio<1, (_LONGLONG)1000000 * 1000000000 * 1000000000> yocto;
 #endif /* 1000000 <= INTMAX_MAX / 1000000000 / 1000000000 */

 #if 1000 <= INTMAX_MAX / 1000000000 / 1000000000
typedef ratio<1, (_LONGLONG)1000 * 1000000000 * 1000000000> zepto;
 #endif /* 1000 <= INTMAX_MAX / 1000000000 / 1000000000 */

typedef ratio<1, (_LONGLONG)1000000000 * 1000000000> atto;
typedef ratio<1, (_LONGLONG)1000000 * 1000000000> femto;
typedef ratio<1, (_LONGLONG)1000 * 1000000000> pico;
typedef ratio<1, 1000000000> nano;
typedef ratio<1, 1000000> micro;
typedef ratio<1, 1000> milli;
typedef ratio<1, 100> centi;
typedef ratio<1, 10> deci;
typedef ratio<10, 1> deca;
typedef ratio<100, 1> hecto;
typedef ratio<1000, 1> kilo;
typedef ratio<1000000, 1> mega;
typedef ratio<1000000000, 1> giga;
typedef ratio<(_LONGLONG)1000 * 1000000000, 1> tera;
typedef ratio<(_LONGLONG)1000000 * 1000000000, 1> peta;
typedef ratio<(_LONGLONG)1000000000 * 1000000000, 1> exa;

 #if 1000 <= INTMAX_MAX / 1000000000 / 1000000000
typedef ratio<(_LONGLONG)1000 * 1000000000 * 1000000000, 1> zetta;
 #endif /* 1000 <= INTMAX_MAX / 1000000000 / 1000000000 */

 #if 1000000 <= INTMAX_MAX / 1000000000 / 1000000000
typedef ratio<(_LONGLONG)1000000 * 1000000000 * 1000000000, 1> yotta;
 #endif /* 1000000 <= INTMAX_MAX / 1000000000 / 1000000000 */
_STD_END
#endif /* _RATIO_ */

/*
 * Copyright (c) 1992-2010 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
V5.30:0009 */
