00001 #include "log.h"
00002 #include "format.h"
00003 #include "numUtil.h"
00004
00005
00016 int roundingScale(int a, int p, int q)
00017 {
00018 if (a > 100000 || p > 100000)
00019 {
00020 double x = a;
00021 x *= p;
00022 x /= q;
00023 return round(x);
00024 }
00025 else
00026 return (a*p + q/2)/q;
00027 }
00028
00029
00030
00034 int rangeIntersection(int start1, int end1, int start2, int end2)
00035 {
00036 int s = MAX(start1,start2);
00037 int e = MIN(end1,end2);
00038 return e-s;
00039 }
00040
00041
00042
00046 int positiveRangeIntersection(int start1, int end1, int start2, int end2)
00047
00048 {
00049 int ret = rangeIntersection(start1,end1,start2,end2);
00050 if (ret < 0)
00051 ret = 0;
00052 return ret;
00053 }
00054
00055
00056
00060 unsigned byteSwap32(unsigned a)
00061 {
00062 union {unsigned whole; unsigned char bytes[4];} u,v;
00063 u.whole = a;
00064 v.bytes[0] = u.bytes[3];
00065 v.bytes[1] = u.bytes[2];
00066 v.bytes[2] = u.bytes[1];
00067 v.bytes[3] = u.bytes[0];
00068 return v.whole;
00069 }
00070
00071
00072
00076 int digitsBaseTwo(unsigned long x)
00077 {
00078 int digits = 0;
00079 while (x)
00080 {
00081 digits += 1;
00082 x >>= 1;
00083 }
00084 return digits;
00085 }
00086
00087
00088
00092 int digitsBaseTen(int x)
00093 {
00094 int digCount = 1;
00095 if (x < 0)
00096 {
00097 digCount = 2;
00098 x = -x;
00099 }
00100 while (x >= 10)
00101 {
00102 digCount += 1;
00103 x /= 10;
00104 }
00105 return digCount;
00106 }