level 1
float Abs(float f)//绝对值
{
return f < 0 ? -f : f;
}
float Floor(float f)//向下取整
{
return (int)f;
}
float Ceil(float f)//向上取整
{
return (int)f + 1;
}
float Round(float f)//四舍五入
{
float ff = Floor(f);
return f - ff < 0.5 ? ff : ff + 1;
}
float Rpt(float t, float range)//重复
{
return t - Floor(t / range) * range;
}
float Pipo(float t, float range)//乒乓
{
float tr2 = Rpt(t, range * 2);
return tr2 > range ? range * 2 - tr2 : tr2;
//return range - Abs(range - Rpt(t, range * 2));
}
float Clamp(float value, float min, float max)//钳制
{
return value < min ? min : (value > max ? max : value);
}
float Lerp(float a, float b, float t)//偏向
{
t = Clamp(t, 0, 1);
return b * t + a * (1 - t);
}
float Sqrt0(float x)//二分法求平方根
{
float epsilon = 1e-6f; //代表精度
float low = 0f;
float high = x;
float mid = (low + high) / 2;
while ((high - low) > epsilon)
{
if (mid * mid > x) high = mid; else low = mid;
mid = (high + low) / 2;
}
return mid;
}
float Sqrt1(float x)//牛顿迭代法求平方根
{
if (x == 0) return 0;
float last = 0.0f;
float res = 1.0f;
while (res != last)
{
last = res;
res = (res + x / res) / 2;
}
return res;
}
static float pi = 3.14
15926535897
9324f;
readonly float[] angle = new float[13]{pi/4,pi/8,pi/16,
pi/32,pi/64,pi/128,
pi/256,pi/512,pi/1024,
pi/2048,pi/4096,pi/8192,pi/16384};//暂时大约只迭代到第4位小数
readonly float[] tang = new float[13]{1,0.4142135623731f,0.19891236737966f,
0.098491403357164f,0.049126849769467f,
0.024548622108925f,0.012272462379566f,
0.006
13600015762
34f,0.0030679712014227f,
0.00
15339819910
887f,0.00076699054434309f,
0.00038349521577144f,0.00019174760083571f};//对应的正切值
float Sin(float a)//CORDIC 算法求正弦
{
if (a <= (pi / 16384)) return a; //因为a的值太小,sin a 约等于 a
bool negitive = a < 0; //负数反号 sin(-a) = -sin(a)
float x = 10;
float y = 0;
float theta = 0;
for (int i = 0; i < 13; i++)//开始迭代
{
float orix = 0, oriy = 0;
while (theta < a)//当前角度小于a
{
orix = x;
oriy = y;
x = orix - tang[i] * oriy;//坐标旋转
y = tang[i] * orix + oriy;//坐标旋转
theta += angle[i];
}
if (theta == a) return y / Sqrt1(x * x + y * y) * (negitive ? -1 : 1);
theta -= angle[i];//旋转的弧度超过了a,退回原来增加的角度,同时进入下一次迭代,共13次
x = orix;
y = oriy;
}
return y / Sqrt1(x * x + y * y) * (negitive ? -1 : 1);
}
2020年07月09日 02点07分
2
level 1
排序算法之 快排,归并
public void QuickSort(int[] arr, int iBegin, int iEnd)//快速排序
{
if (iBegin >= iEnd) return;//如果剩余1返回
int iLeft = iBegin, iRight = iEnd, iPivot = arr[iBegin];//左指针,右指针,基准值(拿出来)
while (iLeft < iRight)//如果左小于右
{
while (iLeft < iRight && arr[iRight] >= iPivot) iRight--;//从右至左找小于基准
if (iLeft < iRight) arr[iLeft++] = arr[iRight];//左指针位值赋值为右指针值,左++
while (iLeft < iRight && arr[iLeft] <= iPivot) iLeft++;//从左至右找大于基准
if (iLeft < iRight) arr[iRight--] = arr[iLeft];//右指针位值赋值为左指针值,右--
}
arr[iLeft] = iPivot;//值放入左指针
QuickSort(arr, iBegin, iLeft - 1);//递归左中
QuickSort(arr, iRight + 1, iEnd);//递归中右
}
public void MergeSort(int[] arr, int first, int end)//归并排序
{
if (first >= end) return;//起如果小于终,表示1个数以上(可以再分)
int mid = (first + end) / 2;//求中间索引,奇数会稍稍靠左
MergeSort(arr, first, mid);//左中分治,直到只有一个数
MergeSort(arr, mid + 1, end);//中右分治,直到只有一个数
int[] nArr = new int[end - first + 1];//t是新数组,长度为尾-首+1,开始回溯归并
int ftm = first, mte = mid + 1, nap = 0;//首到中,含中,中到尾,不含中,nap为新数组指针
while (ftm <= mid && mte <= end)//只要中尾针不大于尾, 首中针不大于中,表示有数可比较
if (arr[ftm] > arr[mte]) nArr[nap++] = arr[mte++];//有数比较的时候,新数组谁小装谁,指针加加
else nArr[nap++] = arr[ftm++];//有数比较的时候,新数组谁小装谁,指针加加
while (mte < end + 1) nArr[nap++] = arr[mte++];//有一个已空,新数组装另一个,指针加加
while (ftm < mid + 1) nArr[nap++] = arr[ftm++];//有一个已空,新数组装另一个,指针加加
for (nap = 0, ftm = first; ftm < end + 1; nap++, ftm++) arr[ftm] = nArr[nap];//把t值放入对应的a位置
}
2020年07月09日 02点07分
4
level 1
sorry! 没考虑到负数。。。
更正如下:
public static float Abs(float f)//绝对值
{
return f < 0 ? -f : f;
}
public static float Floor(float f)//向下取整
{
float intf = (int)f;
return intf > f ? intf - 1 : intf;
}
public static float Ceil(float f)//向上取整
{
float intf = (int)f;
return intf < f ? intf + 1 : intf;
}
public static float Round(float f)//四舍五入
{
float ff = Floor(f);
return f - ff < 0.5 ? ff : ff + 1;
}
public static float Rpt(float t, float range)//重复
{
return t - Floor(t / range) * range;//和向下取整之差不会为负
}
public static float Pipo(float t, float range)//乒乓
{
float tr2 = Rpt(t, range * 2);
return tr2 > range ? range * 2 - tr2 : tr2;//区间函数
}
2020年07月10日 07点07分
5
level 1
public static int Add(int a, int b) //加法
{
int a_copy = a, b_copy = b, sum = 0, carry = 0;
lable:
sum = a_copy ^ b_copy;
carry = (a_copy & b_copy) << 1;
a_copy = sum;
b_copy = carry;
if (carry != 0) goto lable;
return sum;
}
public static int Rev(int x)//取补码
{
return Add(~x, 1);
}
2020年07月13日 09点07分
7
level 1
public static float Sin(float x)
{
x = Repeat(x, 2 * (float)Math.PI);
if (x < 1e-8f) return x;
float sum = x, fz = x, fm = 1, t = 1, ts = 1;
int n = 0;
while (t >= 1e-8f)
{
n++; ts = -ts;
fz = fz * x * x;
fm = fm * 2 * n * (2 * n + 1);
t = fz / fm;
sum += t * ts;
}
return sum;
}
public static float Cos(float x)
{
x = Repeat(x, 2 * (float)Math.PI);
if (x < 1e-8f) return x;
float sum = 1, fz = 1, fm = 1, t = 1, ts = 1;
int n = 0;
while (t >= 1e-8f)
{
n++; ts = -ts;
fz = fz * x * x;
fm = fm * 2 * n * (2 * n - 1);
t = fz / fm;
sum += t * ts;
}
return sum;
}
2020年07月13日 09点07分
8