| 1 |
rtomiyasu |
3 |
/* |
| 2 |
|
|
* The MIT License |
| 3 |
|
|
|
| 4 |
|
|
Conograph (powder auto-indexing program) |
| 5 |
|
|
|
| 6 |
|
|
Copyright (c) <2012> <Ryoko Oishi-Tomiyasu, KEK> |
| 7 |
|
|
|
| 8 |
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
|
|
of this software and associated documentation files (the "Software"), to deal |
| 10 |
|
|
in the Software without restriction, including without limitation the rights |
| 11 |
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
|
|
copies of the Software, and to permit persons to whom the Software is |
| 13 |
|
|
furnished to do so, subject to the following conditions: |
| 14 |
|
|
|
| 15 |
|
|
The above copyright notice and this permission notice shall be included in |
| 16 |
|
|
all copies or substantial portions of the Software. |
| 17 |
|
|
|
| 18 |
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 |
|
|
THE SOFTWARE. |
| 25 |
|
|
* |
| 26 |
|
|
*/ |
| 27 |
|
|
#ifndef _GCD_HH_ |
| 28 |
|
|
#define _GCD_HH_ |
| 29 |
|
|
|
| 30 |
|
|
#include"../RietveldAnalysisTypes.hh" |
| 31 |
|
|
|
| 32 |
|
|
// Returns (m,n). |
| 33 |
|
|
// On output, mt - ns = (m,n). |
| 34 |
|
|
Int4 gcd(const Int4& m, const Int4& n, Int4& s, Int4& t); |
| 35 |
|
|
|
| 36 |
|
|
Int4 gcd(const Int4&, const Int4&); |
| 37 |
|
|
|
| 38 |
|
|
Int4 gcd(const vector<Int4>&, vector<Int4>&); |
| 39 |
|
|
|
| 40 |
|
|
inline Int4 iround_half_up(const double& value) |
| 41 |
|
|
{ |
| 42 |
|
|
if (value >= 0.0) |
| 43 |
|
|
return Int4(floor(+value + 0.5)+0.01); |
| 44 |
|
|
else |
| 45 |
|
|
return - Int4(floor(-value + 0.5)+0.01); |
| 46 |
|
|
} |
| 47 |
|
|
|
| 48 |
|
|
inline bool dbl2fraction(const Double& dbl, pair<Int4, Int4>& frac) |
| 49 |
|
|
{ |
| 50 |
|
|
const Int4 k = iround_half_up(dbl*48); |
| 51 |
rtomiyasu |
33 |
if( fabs( k - dbl*48 ) >= 1.0e-8 ) return false; |
| 52 |
rtomiyasu |
3 |
|
| 53 |
|
|
if( k == 0 ) |
| 54 |
|
|
{ |
| 55 |
|
|
frac.first = 0; |
| 56 |
|
|
frac.second = 1; |
| 57 |
|
|
} |
| 58 |
|
|
else |
| 59 |
|
|
{ |
| 60 |
|
|
const Int4 common_divisor = gcd(k, 48); |
| 61 |
|
|
frac.first = k/common_divisor; |
| 62 |
|
|
frac.second = 48/common_divisor; |
| 63 |
|
|
} |
| 64 |
|
|
return true; |
| 65 |
|
|
} |
| 66 |
|
|
|
| 67 |
|
|
#endif /*GCD_HH_*/ |