aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWohlstand <admin@wohlnet.ru>2017-09-30 15:34:25 +0300
committerWohlstand <admin@wohlnet.ru>2017-09-30 15:34:25 +0300
commitbef9226b4c2aa629d5ddf0ce26a073b87e887cfe (patch)
tree72bb3216d546914e7ee84a5ebfdda951a86186e3 /src
parent3a63abfcac78e0b62f1af3d8b9961554896f59ff (diff)
downloadlibADLMIDI-bef9226b4c2aa629d5ddf0ce26a073b87e887cfe.tar.gz
libADLMIDI-bef9226b4c2aa629d5ddf0ce26a073b87e887cfe.tar.bz2
libADLMIDI-bef9226b4c2aa629d5ddf0ce26a073b87e887cfe.zip
Apply CLang warnings fix of fraction.h from WohlSoft/AudioCodecs repository
Diffstat (limited to 'src')
-rw-r--r--src/fraction.h40
1 files changed, 24 insertions, 16 deletions
diff --git a/src/fraction.h b/src/fraction.h
index f0943dd..1ec10ab 100644
--- a/src/fraction.h
+++ b/src/fraction.h
@@ -1,6 +1,9 @@
#ifndef bqw_fraction_h
#define bqw_fraction_h
+#include <cmath>
+#include <limits>
+
/* Fraction number handling.
* Copyright (C) 1992,2001 Bisqwit (http://iki.fi/bisqwit/)
*/
@@ -24,7 +27,7 @@ class fraction
#endif
public:
void set(inttype n, inttype d) { num1=n; num2=d; Optim(); }
-
+
fraction() : num1(0), num2(1) { }
fraction(inttype value) : num1(value), num2(1) { }
fraction(inttype n, inttype d) : num1(n), num2(d) { }
@@ -41,28 +44,28 @@ public:
self &operator-= (const self &b);
self &operator*= (const self &b) { Debug('*',b);num1*=b.nom(); num2*=b.denom(); Optim(); return *this; }
self &operator/= (const self &b) { Debug('/',b);num1*=b.denom(); num2*=b.nom(); Optim(); return *this; }
- self operator- () const { return self(-num1, num2); }
-
+ self operator- () const { return self(-num1, num2); }
+
#define fraction_blah_func(op1, op2) \
self operator op1 (const self &b) const { self tmp(*this); tmp op2 b; return tmp; }
-
+
fraction_blah_func( +, += )
fraction_blah_func( -, -= )
- fraction_blah_func( /, /= )
+ fraction_blah_func( /, /= )
fraction_blah_func( *, *= )
-
+
#undef fraction_blah_func
#define fraction_blah_func(op) \
bool operator op(const self &b) const { return value() op b.value(); } \
bool operator op(inttype b) const { return value() op b; }
-
+
fraction_blah_func( < )
fraction_blah_func( > )
fraction_blah_func( <= )
fraction_blah_func( >= )
#undef fraction_blah_func
-
+
const inttype &nom() const { return num1; }
const inttype &denom() const { return num2; }
inline bool operator == (inttype b) const { return denom() == 1 && nom() == b; }
@@ -83,11 +86,16 @@ template<typename inttype>
void fraction<inttype>::Optim()
{
/* Euclidean algorithm */
- inttype n1, n2;
- if(labs(num1) < labs(num2))
+ inttype n1, n2, nn1, nn2;
+
+ nn1 = std::numeric_limits<inttype>::is_signed ? (num1 >= 0 ? num1 : -num1) : num1;
+ nn2 = std::numeric_limits<inttype>::is_signed ? (num2 >= 0 ? num2 : -num2) : num2;
+
+ if(nn1 < nn2)
n1 = num1, n2 = num2;
else
n1 = num2, n2 = num1;
+
if(!num1) { num2 = 1; return; }
for(;;)
{
@@ -141,7 +149,7 @@ fraction<inttype> &fraction<inttype>::operator= (long double orig)
set(0, 0);
return *this;
}
-
+
inttype cf[25];
for(int maxdepth=1; maxdepth<25; ++maxdepth)
{
@@ -155,10 +163,10 @@ fraction<inttype> &fraction<inttype>::operator= (long double orig)
if(cf[i]-1 > cf[i])break;
a = 1.0 / (a - cf[i]);
}
-
+
for(viim=i-1; i < maxdepth; ++i)
cf[i] = 0;
-
+
u = cf[viim];
v = 1;
for(i = viim-1; i >= 0; --i)
@@ -169,19 +177,19 @@ fraction<inttype> &fraction<inttype>::operator= (long double orig)
}
virhe = (orig - (u / (long double)v)) / orig;
-
+
set(u, v);
//if(verbose > 4)
// cerr << "Guess: " << *this << " - error = " << virhe*100 << "%\n";
if(virhe < 1e-8 && virhe > -1e-8)break;
}
-
+
//if(verbose > 4)
//{
// cerr << "Fraction=" << orig << ": " << *this << endl;
//}
-
+
return *this;
}