public static FloatI InverseLerp(FloatI from, FloatI to, FloatI value) { if (from < to) { if (value < from) { return(0f); } if (value > to) { return(1f); } value -= from; value /= to - from; return(value); } else { if (from <= to) { return(0f); } if (value < to) { return(1f); } if (value > from) { return(0f); } return(1f - (value - to) / (from - to)); } }
public static FloatI operator -(FloatI a) { FloatI ret = new FloatI(); ret.m_numerator = -a.m_numerator; return(ret); }
public static FloatI operator +(FloatI a, FloatI b) { FloatI ret = new FloatI(); ret.m_numerator = a.m_numerator + b.m_numerator; return(ret); }
public static FloatI operator /(FloatI a, FloatI b) { FloatI ret = new FloatI(); if (b.m_numerator == 0) { UnityEngine.Debug.LogError("FloatL / 0"); if (a.m_numerator >= 0) { return(MaxValue); } else if (a.m_numerator < 0) { return(MinValue); } // else // { // return 0; // } } // 可以应对小数除大数时不为0 long tmp = (a.m_numerator * m_denominator) / b.m_numerator; ret.m_numerator = (int)tmp; return(ret); }
// public static FloatL SinOld(FloatL f) // { // return Math.Sin(f.ToDouble()); // } /// <summary> /// 牛顿法求平方根 /// </summary> /// <param name="c"></param> /// <returns></returns> public static FloatI SqrtOld(FloatI c) { if (c == 0) { return(0); } if (c < new FloatI(0)) { return(new FloatI(-1)); } FloatI err = FloatI.Epsilon; FloatI t = c; int count = 0; while (FloatIMath.Abs(t - c / t) > err * t) { count++; t = (c / t + t) / new FloatI(2.0f); if (count >= 20) { UnityEngine.Debug.LogWarning("FixPoint Sqrt " + c + " current sqrt " + t); break; } } return(t); }
public static FloatI Tan(FloatI f) { FloatI sin = Sin(f); FloatI cos = Cos(f); return(sin / cos); }
public static FloatI operator %(FloatI lhs, FloatI rhs) { FloatI ret = new FloatI(); ret.m_numerator = lhs.m_numerator % rhs.m_numerator; return(ret); }
public static FloatI Cos(FloatI f) { // int index = SinCosLookupTable.getIndex(f.m_numerator, FloatL.m_denominator); // return new FloatL(SinCosLookupTable.cos_table[index]) / new FloatL(SinCosLookupTable.FACTOR); FloatI ret = Math.Cos((double)f); return(ret); }
public static FloatI MoveTowards(FloatI current, FloatI target, FloatI maxDelta) { if (FloatIMath.Abs(target - current) <= maxDelta) { return(target); } return(current + FloatIMath.Sign(target - current) * maxDelta); }
public static FloatI Asin(FloatI f) { //return Math.Asin(f.ToDouble()); // arcsinX = pi / 2 - arccosX; FloatI arccosX = Acos(f); return(FloatIMath.PI / 2 - arccosX); }
public static FloatI Sqrt(FloatI c) { ulong value = Sqrt64((ulong)(c.m_numerator * FloatI.m_denominator)); FloatI ret = new FloatI(); ret.m_numerator = (int)value; return(ret); }
public static FloatI Acos(FloatI f) { // int num = (int)FixPointMath.Divide(f.m_numerator * (long)AcosLookupTable.HALF_COUNT, FloatL.m_denominator) + AcosLookupTable.HALF_COUNT; // num = FixPointMath.Clamp(num, 0, AcosLookupTable.COUNT); // return new FloatL((long)AcosLookupTable.table[num]) / new FloatL(10000); FloatI ret = Math.Acos((double)f); return(ret); }
public override bool Equals(object other) { if (!(other is FloatI)) { return(false); } FloatI otherFloatL = (FloatI)other; return(this.m_numerator == otherFloatL.m_numerator); }
public static FloatI LerpAngle(FloatI a, FloatI b, FloatI t) { FloatI num = FloatIMath.Repeat(b - a, 360f); if (num > 180f) { num -= 360f; } return(a + num * FloatIMath.Clamp01(t)); }
public static FloatI Abs(FloatI f) { FloatI ret = f; if (ret.m_numerator < 0) { ret.m_numerator = -ret.m_numerator; } return(ret); }
public static FloatI DeltaAngle(FloatI current, FloatI target) { FloatI num = FloatIMath.Repeat(target - current, 360f); if (num > 180f) { num -= 360f; } return(num); }
public static FloatI Clamp01(FloatI value) { if (value < 0) { return(0); } if (value > 1) { return(1); } return(value); }
public static FloatI Clamp(FloatI value, FloatI min, FloatI max) { if (value < min) { value = min; } else if (value > max) { value = max; } return(value); }
public static FloatI Atan2(FloatI yL, FloatI xL) { // //return Math.Atan2(y.ToDouble(), x.ToDouble()); // long y = yL.m_numerator; // long x = xL.m_numerator; // // int num; // int num2; // if (x < 0) // { // if (y < 0) // { // x = -x; // y = -y; // num = 1; // } // else // { // x = -x; // num = -1; // } // num2 = -31416; // } // else // { // if (y < 0) // { // y = -y; // num = -1; // } // else // { // num = 1; // } // num2 = 0; // } // int dIM = Atan2LookupTable.DIM; // long num3 = (long)(dIM - 1); // long b = (long)((x >= y) ? x : y); // int num4 = (int)FixPointMath.Divide((long)x * num3, b); // int num5 = (int)FixPointMath.Divide((long)y * num3, b); // int num6 = Atan2LookupTable.table[num5 * dIM + num4]; // return new FloatL((num6 + num2) * num) / new FloatL(10000); return(Math.Atan2((double)yL, (double)xL)); }
public static FloatI Min(params FloatI[] values) { int num = values.Length; if (num == 0) { return(0f); } FloatI num2 = values[0]; for (int i = 1; i < num; i++) { if (values[i] < num2) { num2 = values[i]; } } return(num2); }
public static FloatI operator *(FloatI a, FloatI b) { // if (a.m_numerator > m_maybeOverflow // && b.m_numerator > m_maybeOverflow) // { // FloatLow ret = new FloatLow(); // FloatLow tmpA = a.ToFloatLow(); // FloatLow tmpB = b.ToFloatLow(); // ret = tmpA * tmpB; // return ret; // } // else if (a.m_numerator > m_maybeOverflow) // { // FloatLow tmpA = a.ToFloatLow(); // FloatL ret = new FloatL(); // ret.m_numerator = tmpA.m_numerator * b.m_numerator / FloatLow.m_denominator; // return ret; // } // else if (b.m_numerator > m_maybeOverflow) // { // FloatLow tmpB = b.ToFloatLow(); // FloatL ret = new FloatL(); // ret.m_numerator = a.m_numerator * tmpB.m_numerator / FloatLow.m_denominator; // return ret; // } // else // { // FloatL ret = new FloatL(); // ret.m_numerator = ((a.m_numerator * b.m_numerator) / m_denominator); // return ret; // } long tmp = a.m_numerator * b.m_numerator / m_denominator; FloatI ret = new FloatI(); ret.m_numerator = (int)tmp; return(ret); }
public static FloatI Repeat(FloatI t, FloatI length) { return(t - FloatIMath.Floor(t / length) * length); }
public static FloatI Atan(FloatI f) { //return Math.Atan(f.ToDouble()); throw new Exception("不应该使用atan, 考虑用atan2替代"); }
public static FloatI MoveTowardsAngle(FloatI current, FloatI target, FloatI maxDelta) { target = current + FloatIMath.DeltaAngle(current, target); return(FloatIMath.MoveTowards(current, target, maxDelta)); }
public static bool Approximately(FloatI a, FloatI b) { return(a == b); }
public static FloatI PingPong(FloatI t, FloatI length) { t = FloatIMath.Repeat(t, length * 2f); return(length - FloatIMath.Abs(t - length)); }
public static FloatI Sign(FloatI f) { return((f < 0f) ? -1f : 1f); }
public static int RoundToInt(FloatI f) { //return (int)Math.Round(f.ToDouble()); return((int)(f + 0.5d)); }
public static FloatI Round(FloatI f) { //return Math.Round(f.ToDouble()); return(f + 0.5d); }
public static FloatI Pow(FloatI f, FloatI p) { return(Math.Pow((double)f, (double)p)); }