Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <resourcemodel/Fraction.hxx>
21 :
22 : namespace writerfilter {
23 : namespace resourcemodel {
24 :
25 0 : sal_uInt32 gcd(sal_uInt32 a, sal_uInt32 b)
26 : {
27 0 : if (a == 0 || b == 0)
28 0 : return a | b;
29 :
30 0 : sal_uInt32 nShift = 0;
31 0 : while (((a | b) & 1) == 0)
32 : {
33 0 : a >>= 1;
34 0 : b >>= 1;
35 0 : ++nShift;
36 : }
37 :
38 0 : while ((a & 1) == 0)
39 0 : a >>= 1;
40 :
41 0 : do
42 : {
43 0 : while ((b & 1) == 0)
44 0 : b >>= 1;
45 :
46 0 : if (a < b)
47 : {
48 0 : b -= a;
49 : }
50 : else
51 : {
52 0 : sal_uInt32 nDiff = a - b;
53 0 : a = b;
54 0 : b = nDiff;
55 : }
56 :
57 0 : b >>= 1;
58 : }
59 : while (b != 0);
60 :
61 0 : return a << nShift;
62 : }
63 :
64 0 : sal_uInt32 lcm(sal_Int32 a, sal_Int32 b)
65 : {
66 0 : return abs(a * b) / gcd(abs(a), abs(b));
67 : }
68 :
69 0 : Fraction::Fraction(sal_Int32 nNumerator, sal_Int32 nDenominator)
70 : {
71 0 : init(nNumerator, nDenominator);
72 0 : }
73 :
74 0 : Fraction::~Fraction()
75 : {
76 0 : }
77 :
78 0 : void Fraction::init(sal_Int32 nNumerator, sal_Int32 nDenominator)
79 : {
80 0 : sal_uInt32 nGCD = gcd(nNumerator, nDenominator);
81 :
82 0 : mnNumerator = nNumerator/ nGCD;
83 0 : mnDenominator = nDenominator / nGCD;
84 0 : }
85 :
86 0 : void Fraction::assign(const Fraction & rFraction)
87 : {
88 0 : init(rFraction.mnNumerator, rFraction.mnDenominator);
89 0 : }
90 :
91 0 : Fraction Fraction::inverse() const
92 : {
93 0 : return Fraction(mnDenominator, mnNumerator);
94 : }
95 :
96 0 : Fraction Fraction::operator + (const Fraction & rFraction) const
97 : {
98 0 : sal_uInt32 nLCM = lcm(mnDenominator, rFraction.mnDenominator);
99 :
100 0 : return Fraction(mnNumerator * nLCM / mnDenominator + rFraction.mnNumerator * nLCM / rFraction.mnDenominator, nLCM);
101 : }
102 :
103 0 : Fraction Fraction::operator - (const Fraction & rFraction) const
104 : {
105 0 : sal_uInt32 nLCM = lcm(mnDenominator, rFraction.mnDenominator);
106 :
107 0 : return Fraction(mnNumerator * nLCM / mnDenominator - rFraction.mnNumerator * nLCM / rFraction.mnDenominator, nLCM);
108 : }
109 :
110 0 : Fraction Fraction::operator * (const Fraction & rFraction) const
111 : {
112 0 : return Fraction(mnNumerator * rFraction.mnNumerator, mnDenominator * rFraction.mnDenominator);
113 : }
114 :
115 0 : Fraction Fraction::operator / (const Fraction & rFraction) const
116 : {
117 0 : return *this * rFraction.inverse();
118 : }
119 :
120 0 : Fraction Fraction::operator = (const Fraction & rFraction)
121 : {
122 0 : assign(rFraction);
123 :
124 0 : return *this;
125 : }
126 :
127 0 : Fraction::operator sal_Int32() const
128 : {
129 0 : return mnNumerator / mnDenominator;
130 : }
131 :
132 0 : Fraction::operator float() const
133 : {
134 0 : return static_cast<float>(mnNumerator) / static_cast<float>(mnDenominator);
135 : }
136 :
137 : }}
138 :
139 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|