| 1 |
/************************ |
|---|
| 2 |
|
|---|
| 3 |
Boost Software License - Version 1.0 - August 17th, 2003 |
|---|
| 4 |
|
|---|
| 5 |
Permission is hereby granted, free of charge, to any person or organization |
|---|
| 6 |
obtaining a copy of the software and accompanying documentation covered by |
|---|
| 7 |
this license (the "Software") to use, reproduce, display, distribute, |
|---|
| 8 |
execute, and transmit the Software, and to prepare derivative works of the |
|---|
| 9 |
Software, and to permit third-parties to whom the Software is furnished to |
|---|
| 10 |
do so, all subject to the following: |
|---|
| 11 |
|
|---|
| 12 |
The copyright notices in the Software and this entire statement, including |
|---|
| 13 |
the above license grant, this restriction and the following disclaimer, |
|---|
| 14 |
must be included in all copies of the Software, in whole or in part, and |
|---|
| 15 |
all derivative works of the Software, unless such copies or derivative |
|---|
| 16 |
works are solely in the form of machine-executable object code generated by |
|---|
| 17 |
a source language processor. |
|---|
| 18 |
|
|---|
| 19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 20 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 21 |
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
|---|
| 22 |
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
|---|
| 23 |
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
|---|
| 24 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|---|
| 25 |
DEALINGS IN THE SOFTWARE. |
|---|
| 26 |
|
|---|
| 27 |
****************/ |
|---|
| 28 |
|
|---|
| 29 |
module rational; |
|---|
| 30 |
|
|---|
| 31 |
template Tpl(T...){alias T Tpl;} |
|---|
| 32 |
|
|---|
| 33 |
/// |
|---|
| 34 |
uint gcd(int n, uint d) |
|---|
| 35 |
{ |
|---|
| 36 |
if(n < 0) n = -n; |
|---|
| 37 |
while(d) |
|---|
| 38 |
{ |
|---|
| 39 |
int t = d; |
|---|
| 40 |
d = n%d; |
|---|
| 41 |
n = t; |
|---|
| 42 |
} |
|---|
| 43 |
return n; |
|---|
| 44 |
} |
|---|
| 45 |
static assert(7 == gcd(14,21)); |
|---|
| 46 |
|
|---|
| 47 |
/// |
|---|
| 48 |
template Reduce(int n, uint d) |
|---|
| 49 |
{ |
|---|
| 50 |
static assert(d != 0); |
|---|
| 51 |
static if(n != 0) |
|---|
| 52 |
{ |
|---|
| 53 |
//pragma(msg,n.stringof~"/"~d.stringof); |
|---|
| 54 |
const int N = n / cast(int)gcd(n,d); |
|---|
| 55 |
const int D = cast(uint)(d / gcd(n,d)); |
|---|
| 56 |
const bool Reduced = (gcd(n,d) == 1); |
|---|
| 57 |
} |
|---|
| 58 |
else |
|---|
| 59 |
{ |
|---|
| 60 |
const int N = 0; |
|---|
| 61 |
const int D = 1; |
|---|
| 62 |
const bool Reduced = (D == 1); |
|---|
| 63 |
} |
|---|
| 64 |
alias Tpl!(N,D) V; |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
static assert(!Reduce!(-14,21).Reduced); |
|---|
| 68 |
static assert( Reduce!(-16,21).Reduced); |
|---|