Exemplo n.º 1
0
        public void Abst_ShouldReturnAbsoluteValues()
        {
            // Arrange
            sbyte   aByte    = -10;
            short   aShort   = -20;
            int     aInt     = -30;
            long    aLong    = -40;
            float   aFloat   = -50.0F;
            double  aDouble  = -60.0;
            decimal aDecimal = (decimal) - 70.0;


            // Act

            short   absByte    = aByte.Abs();
            short   absShort   = aShort.Abs();
            int     absInt     = aInt.Abs();
            long    absLong    = aLong.Abs();
            float   absFloat   = aFloat.Abs();
            double  absDouble  = aDouble.Abs();
            decimal absDecimal = aDecimal.Abs();

            // Assert
            Assert.Equal(10, absByte);
            Assert.Equal(20, absShort);
            Assert.Equal(30, absInt);
            Assert.Equal(40, absLong);
            Assert.Equal(50, absFloat);
            Assert.Equal(60, absDouble);
            Assert.Equal(70, absDecimal);
        }
Exemplo n.º 2
0
    public static long ContrainAxisTo16Angles(this long axis)
    {
        int sign = axis.Sign();

        axis = axis.Abs();

        if (axis <= ZERO_THRESHOLD)
        {
            axis = 0;
        }
        else if (axis <= MIN_THRESHOLD)
        {
            axis = MIN;
        }
        else if (axis <= MED_THRESHOLD)
        {
            axis = MED;
        }
        else if (axis <= MAX_THRESHOLD)
        {
            axis = MAX;
        }
        else
        {
            axis = FixedMath.ONE;
        }

        return(axis * sign);
    }
        /// <summary>
        /// Function to return a formatted string containing the memory amount.
        /// </summary>
        /// <param name="amount">Amount of memory in bytes to format.</param>
        /// <returns>A string containing the formatted amount of memory.</returns>
        /// <remarks>
        /// This will produce a string value with the number of bytes suffixed with the word 'bytes' if less than 1023, the number of kilobytes suffixed with the abbreviation 'KB' if the value is
        /// within the range of 1024 to 1048575, the number of megabytes suffixed with the abbreviation MB if the value is within the range of 1048576 to 1073741823, the number of gigabytes
        /// with the suffix of 'GB' if the value is within the range of 1073741824 to 1099511627775, the number of terabytes with the suffix 'TB' if the value is within the range of 1099511627776
        /// to 1125899906842623, of the number of petabytes with the suffix 'PB' if the value is greater than 1125899906842623.
        /// <code language="csharp">
        /// long bytes = 999;
        /// long kilobytes = 2048;
        /// long megabytes = 3145728;
        /// long gigabytes = 4294967296;
        /// long terabytes = 5497558138880;
        /// long petabytes = 6755399441055744;
        ///
        /// Console.WriteLine(bytes.FormatMemory());
        /// Console.WriteLine(kilobytes.FormatMemory());
        /// Console.WriteLine(megabytes.FormatMemory());
        /// Console.WriteLine(gigabytes.FormatMemory());
        /// Console.WriteLine(terabytes.FormatMemory());
        /// Console.WriteLine(petabytes.FormatMemory());
        ///
        /// // Produces: "128 bytes", "2.0 KB", "3.0 MB", "4.0 GB", "5.0 TB", and "6.0 PB".
        /// </code>
        /// <para>
        /// If the value cannot be represented with a string suffix, then the number of bytes will be displayed as default.
        /// </para>
        /// </remarks>
        public static string FormatMemory(this long amount)
        {
            double scale = amount.Abs() / 1125899906842624.0;

            if (scale >= 1.0)
            {
                return(GetCultureString(scale >= 1.0 ? scale : amount, Resources.GOR_UNIT_MEM_PB));
            }

            scale = amount / 1099511627776.0;

            if (scale >= 1.0)
            {
                return(GetCultureString(scale >= 1.0 ? scale : amount, Resources.GOR_UNIT_MEM_TB));
            }

            scale = amount / 1073741824.0;

            if (scale >= 1.0)
            {
                return(GetCultureString(scale >= 1.0 ? scale : amount, Resources.GOR_UNIT_MEM_GB));
            }

            scale = amount / 1048576.0;

            if (scale >= 1.0)
            {
                return(GetCultureString(scale >= 1.0 ? scale : amount, Resources.GOR_UNIT_MEM_MB));
            }

            scale = amount / 1024.0;

            return(GetCultureString(scale >= 1.0 ? scale : amount, scale >= 1.0 ? Resources.GOR_UNIT_MEM_KB : Resources.GOR_UNIT_MEM_BYTES, false));
        }
Exemplo n.º 4
0
        public static long MagnitudeByDivision(this long x)
        {
            x = x.Abs();
            long magnitude = 1L;

            while ((x /= 10L) > 0L)
            {
                magnitude *= 10L;
            }
            return(magnitude);
        }
Exemplo n.º 5
0
        public static long MagnitudeByString(this long x)
        {
            x = x.Abs();
            long magnitude = 1L;
            int  power     = x.ToString().Length;

            for (; power > 1L; power--)
            {
                magnitude *= 10L;
            }
            return(magnitude);
        }
Exemplo n.º 6
0
        private Task GambleOne(CommandContext ctx, Balance userBalance)
        {
            bool won        = TryOdds(0.75f);
            long newBalance = -1;

            userBalance.ExecuteTransaction(
                balance =>
            {
                newBalance    = balance.Value + (won ? 1 : -1);
                balance.Value = newBalance;
            });

            string message = won
                ? $"You won a point to climb all the way up to {newBalance.Abs()}"
                : $"You lost a point to drop down to {newBalance.Abs()}";

            if (newBalance < 0)
            {
                message += $"point{(newBalance == -1 ? "" : "s")} of debt";
            }

            return(ctx.SendMention(message));
        }
Exemplo n.º 7
0
        public static long MagnitudeByMultiplication(this long x)
        {
            x = x.Abs();
            // Prevent overflow
            if (x >= MAX_MAGNITUDE)
            {
                return(MAX_MAGNITUDE);
            }
            long magnitude = 1L;

            for (long m = 1L; m <= x; m *= 10L)
            {
                magnitude = m;
            }
            return(magnitude);
        }
Exemplo n.º 8
0
        public static long MagnitudeByMultiplication3(this long x)
        {
            x = x.Abs();
            // Prevent overflow
            if (x >= MAX_MAGNITUDE)
            {
                return(MAX_MAGNITUDE);
            }
            long magnitude = 10L;

            while (magnitude <= x)
            {
                magnitude *= 10L;
            }
            return(magnitude / 10L);
        }
 public static long GreatestCommonDivisor(long a, long b)
 {
     a = a.Abs();
     b = b.Abs();
     while (a != 0 && b != 0)
     {
         if (a > b)
         {
             a %= b;
         }
         else
         {
             b %= a;
         }
     }
     return(a == 0 ? b : a);
 }
Exemplo n.º 10
0
        public static bool CheckCircle_Poly(LSBody circle, LSBody poly)
        {
            int EdgeCount = poly.EdgeNorms.Length;

            ClosestDist = long.MaxValue;
            for (int i = 0; i < EdgeCount; i++)
            {
                Vector2d axis             = poly.EdgeNorms [i];
                long     CircleProjection = circle._position.Dot(axis.x, axis.y);
                long     CircleMin        = CircleProjection - circle.Radius;
                long     CircleMax        = CircleProjection + circle.Radius;

                long PolyMin;
                long PolyMax;
                ProjectPolygon(axis.x, axis.y, poly, out PolyMin, out PolyMax);
                //TODO: Cache PolyMin and PolyMax?
                if (CheckOverlap(CircleMin, CircleMax, PolyMin, PolyMax))
                {
                    long dist1          = PolyMax - CircleMin;
                    long dist2          = CircleMax - PolyMin;
                    long localCloseDist = 0;
                    if (dist1 <= dist2)
                    {
                        localCloseDist = dist1;
                    }
                    else
                    {
                        localCloseDist = -dist2;
                    }
                    if (localCloseDist.Abs() < ClosestDist.Abs())
                    {
                        ClosestDist           = localCloseDist;
                        ClosestAxis           = axis;
                        ClosestAxisProjection = CircleProjection;
                    }
                }
                else
                {
                    return(false);
                }
            }


            return(true);
        }
Exemplo n.º 11
0
        public static bool LineLineIntersection(FixedLine2 line1, FixedLine2 line2, out FixedVector2 intersection)
        {
            intersection = new FixedVector2(long.MinValue, long.MinValue);

            //standard form of line 1
            long a1 = line1.end.y - line1.start.y;
            long b1 = line1.start.x - line1.end.x;
            long c1 = a1.Mul(line1.start.x) - b1.Mul(line1.start.y);

            //standard form of line 2
            long a2 = line2.end.y - line2.start.y;
            long b2 = line2.start.x - line2.end.x;
            long c2 = a2.Mul(line2.start.x) - b2.Mul(line2.start.y);

            //		float delta = A1*B2 - A2*B1;
            //		if(delta == 0)
            //			throw new ArgumentException("Lines are parallel");
            //
            //		float x = (B2*C1 - B1*C2)/delta;
            //		float y = (A1*C2 - A2*C1)/delta;

            long determinant = a1.Mul(b2) - a2.Mul(b1);

            //line are parallel
            if (determinant.Abs() < FixedMath.Hundredth)
            {
                return(false);
            }

            long x = (b2.Mul(c1) - b1.Mul(c2)).Div(determinant);
            long y = (a1.Mul(c2) - a2.Mul(c1)).Div(determinant);

            intersection = new FixedVector2(x, y);

            //		UnityEngine.Debug.DrawLine (new FixedVector2 (0, 0).ToVector3 (), intersection.ToVector3 ());

            if ((FixedMath.Min(line1.start.x, line1.end.x) < x && x < FixedMath.Max(line1.start.x, line1.end.x)) &&
                (FixedMath.Min(line1.start.y, line1.end.y) < y && y < FixedMath.Max(line1.start.y, line1.end.y)))
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 12
0
        public static long MagnitudeBySwitch(this long x)
        {
            x = x.Abs();
            switch (x)
            {
            case long y when y < 10L: return(1L);

            case long y when y < 100L: return(10L);

            case long y when y < 1000L: return(100L);

            case long y when y < 10000L: return(1000L);

            case long y when y < 100000L: return(10000L);

            case long y when y < 1000000L: return(100000L);

            case long y when y < 10000000L: return(1000000L);

            case long y when y < 100000000L: return(10000000L);

            case long y when y < 1000000000L: return(100000000L);

            case long y when y < 10000000000L: return(1000000000L);

            case long y when y < 100000000000L: return(10000000000L);

            case long y when y < 1000000000000L: return(100000000000L);

            case long y when y < 10000000000000L: return(1000000000000L);

            case long y when y < 100000000000000L: return(10000000000000L);

            case long y when y < 1000000000000000L: return(100000000000000L);

            case long y when y < 10000000000000000L: return(1000000000000000L);

            case long y when y < 100000000000000000L: return(10000000000000000L);

            case long y when y < MAX_MAGNITUDE: return(100000000000000000L);

            default: return(MAX_MAGNITUDE);
            }
        }
Exemplo n.º 13
0
        public static long MagnitudeByLog(this long x)
        {
            x = x.Abs();
            long magnitude = 1L;

            // Fix double rounding errors for large numbers
            while (x >= 999999999999998)
            {
                x         /= 10;
                magnitude *= 10;
            }
            int power = (int)(Math.Log10(x));

            while (power-- >= 1L)
            {
                magnitude *= 10L;
            }
            return(magnitude);
        }
Exemplo n.º 14
0
     /// <summary>
     /// Converts a long to the custom base system.
     /// </summary>
     /// <param name="value">The long to convert.</param>
     /// <returns>The custome base number version of the long.</returns>
     public string LongToString(long value)
     {
         if (value == 0) return Convert.ToString(_characterSet[0]);
         long number = value.Abs();
         int remainder;
         StringBuilder text = new StringBuilder((int)Math.Ceiling(
            Math.Log(long.MaxValue, (double)_characterSet.Length)) + 
            value < 0 ? 1 : 0);
 
         while (number != 0)
         {
             remainder = (int)(number % _characterSet.Length);
             text.Insert(0, _characterSet[remainder]);
             number -= remainder;
             number /= _characterSet.Length;
         }
 
         if (value < 0) text.Insert(0, "-");
         return text.ToString();
     }
Exemplo n.º 15
0
 public static long MagnitudeByRange(this long x)
 {
     x = x.Abs();
     if (x < 10L)
     {
         return(1L);
     }
     if (x < 100L)
     {
         return(10L);
     }
     if (x < 1000L)
     {
         return(100L);
     }
     if (x < 10000L)
     {
         return(1000L);
     }
     if (x < 100000L)
     {
         return(10000L);
     }
     if (x < 1000000L)
     {
         return(100000L);
     }
     if (x < 10000000L)
     {
         return(1000000L);
     }
     if (x < 100000000L)
     {
         return(10000000L);
     }
     if (x < 1000000000L)
     {
         return(100000000L);
     }
     if (x < 10000000000L)
     {
         return(1000000000L);
     }
     if (x < 100000000000L)
     {
         return(10000000000L);
     }
     if (x < 1000000000000L)
     {
         return(100000000000L);
     }
     if (x < 10000000000000L)
     {
         return(1000000000000L);
     }
     if (x < 100000000000000L)
     {
         return(10000000000000L);
     }
     if (x < 1000000000000000L)
     {
         return(100000000000000L);
     }
     if (x < 10000000000000000L)
     {
         return(1000000000000000L);
     }
     if (x < 100000000000000000L)
     {
         return(10000000000000000L);
     }
     if (x < MAX_MAGNITUDE)
     {
         return(100000000000000000L);
     }
     return(MAX_MAGNITUDE);
 }
Exemplo n.º 16
0
 public static bool AbsMoreThan(this long f1, long f2)
 {
     return(f1.Abs() > f2);
 }
Exemplo n.º 17
0
            public GridNode GetNode()
            {
                //Calculated closest side to raycast in first
                long xDif = OffsettedPos.x - XGrid;

                xDif = xDif.ClampOne();
                long yDif = OffsettedPos.y - YGrid;

                yDif = yDif.ClampOne();
                long nodeHalfWidth = FixedMath.One / 2;

                //Check to see if we should raycast towards corner first
                if ((xDif.Abs() >= nodeHalfWidth / 2) &&
                    (yDif.Abs() >= nodeHalfWidth / 2))
                {
                    dirX = FixedMath.RoundToInt(xDif);
                    dirY = FixedMath.RoundToInt(yDif);
                }
                else
                {
                    if (xDif.Abs() < yDif.Abs())
                    {
                        dirX = 0;
                        dirY = yDif.RoundToInt();
                    }
                    else
                    {
                        dirX = xDif.RoundToInt();
                        dirY = 0;
                    }
                }

                int layerStartX = dirX,
                    layerStartY = dirY;
                int iterations  = 0; // <- this is for debugging

                for (layer = 1; layer <= this.MaxTestDistance;)
                {
                    this.CheckPathNode(GridManager.GetNode(XGrid + dirX, YGrid + dirY));
                    if (this.castNodeFound)
                    {
                        return(this.closestNode);
                    }
                    AdvanceRotation();
                    //If we make a full loop
                    if (layerStartX == dirX && layerStartY == dirY)
                    {
                        layer++;
                        //Advance a layer instead of rotation
                        if (dirX > 0)
                        {
                            dirX = layer;
                        }
                        else if (dirX < 0)
                        {
                            dirX = -layer;
                        }
                        if (dirY > 0)
                        {
                            dirY = layer;
                        }
                        else if (dirY < 0)
                        {
                            dirY = -layer;
                        }
                        layerStartX = dirX;
                        layerStartY = dirY;
                    }
                    iterations++;
                    if (iterations > 500)
                    {
                        Debug.Log("tew many");
                        break;
                    }
                }

                //If the cast node is found or the side has been checked, do not raycast on that side

                if (!castNodeFound)
                {
                    return(null);
                }
                return(closestNode);
            }
Exemplo n.º 18
0
 public static bool AreCoprime(long a, long b) => Gcd(a.Abs(), b.Abs()) == 1;
        public bool Overlaps(FastList <Vector2d> outputIntersectionPoints)
        {
            outputIntersectionPoints.FastClear();
            //Checks if this object overlaps the line formed by p1 and p2
            switch (this.Shape)
            {
            case ColliderType.Circle:
            {
                bool overlaps = false;
                //Check if the circle completely fits between the line
                long projPos = this._position.Dot(cacheAxis.x, cacheAxis.y);
                //Circle withing bounds?
                if (projPos >= axisMin && projPos <= axisMax)
                {
                    long projPerp = this._position.Dot(cacheAxisNormal.x, cacheAxisNormal.y);
                    long perpDif  = (cacheProjPerp - projPerp);
                    long perpDist = perpDif.Abs();
                    if (perpDist <= _radius)
                    {
                        overlaps = true;
                    }
                    if (overlaps)
                    {
                        long sin = (perpDif);
                        long cos = FixedMath.Sqrt(_radius.Mul(_radius) - sin.Mul(sin));
                        if (cos == 0)
                        {
                            outputIntersectionPoints.Add((cacheAxis * projPos) + perpVector);
                        }
                        else
                        {
                            outputIntersectionPoints.Add(cacheAxis * (projPos - cos) + perpVector);
                            outputIntersectionPoints.Add(cacheAxis * (projPos + cos) + perpVector);
                        }
                    }
                }
                else
                {
                    //If not, check distances to points
                    long p1Dist = _position.FastDistance(cacheP1.x, cacheP2.y);
                    if (p1Dist <= this.FastRadius)
                    {
                        outputIntersectionPoints.Add(cacheP1);
                        overlaps = true;
                    }
                    long p2Dist = _position.FastDistance(cacheP2.x, cacheP2.y);
                    if (p2Dist <= this.FastRadius)
                    {
                        outputIntersectionPoints.Add(cacheP2);
                        overlaps = true;
                    }
                }
                return(overlaps);
            }
            break;

            case ColliderType.AABox:
            {
            }
            break;

            case ColliderType.Polygon:
            {
                bool intersected = false;


                for (int i = 0; i < this.Vertices.Length; i++)
                {
                    int      edgeIndex = i;
                    Vector2d pivot     = this.RealPoints [edgeIndex];
                    Vector2d edge      = this.Edges [edgeIndex];
                    long     proj1     = 0;
                    int      nextIndex = edgeIndex + 1 < this.RealPoints.Length ? edgeIndex + 1 : 0;
                    Vector2d nextPoint = RealPoints [nextIndex];
                    long     proj2     = (nextPoint - pivot).Dot(edge);

                    long min;
                    long max;
                    if (proj1 < proj2)
                    {
                        min = proj1;
                        max = proj2;
                    }
                    else
                    {
                        min = proj2;
                        max = proj1;
                    }

                    long lineProj1 = (cacheP1 - pivot).Dot(edge);
                    long lineProj2 = (cacheP2 - pivot).Dot(edge);

                    long lineMin;
                    long lineMax;
                    if (lineProj1 < lineProj2)
                    {
                        lineMin = lineProj1;
                        lineMax = lineProj2;
                    }
                    else
                    {
                        lineMin = lineProj2;
                        lineMax = lineProj1;
                    }
                    if (CollisionPair.CheckOverlap(min, max, lineMin, lineMax))
                    {
                        Vector2d edgeNorm      = this.EdgeNorms [edgeIndex];
                        long     normProj      = 0;
                        long     normLineProj1 = (cacheP1 - pivot).Dot(edgeNorm);
                        long     normLineProj2 = (cacheP2 - pivot).Dot(edgeNorm);

                        long normLineMin;
                        long normLineMax;

                        if (normLineProj1 < normLineProj2)
                        {
                            normLineMin = normLineProj1;
                            normLineMax = normLineProj2;
                        }
                        else
                        {
                            normLineMin = normLineProj2;
                            normLineMax = normLineProj1;
                        }

                        if (normProj >= normLineMin && normProj <= normLineMax)
                        {
                            long revProj1 = pivot.Dot(LSBody.cacheAxisNormal);
                            long revProj2 = nextPoint.Dot(cacheAxisNormal);

                            long revMin;
                            long revMax;
                            if (revProj1 < revProj2)
                            {
                                revMin = revProj1;
                                revMax = revProj2;
                            }
                            else
                            {
                                revMin = revProj2;
                                revMax = revProj1;
                            }

                            if (LSBody.cacheProjPerp >= revMin && LSBody.cacheProjPerp <= revMax)
                            {
                                intersected = true;
                                if (LSBody.calculateIntersections)
                                {
                                    long fraction         = normLineProj1.Abs().Div(normLineMax - normLineMin);
                                    long intersectionProj = FixedMath.Lerp(lineProj1, lineProj2, fraction);
                                    outputIntersectionPoints.Add(edge * intersectionProj + pivot);

                                    if (outputIntersectionPoints.Count == 2)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                return(intersected);
            }
            break;
            }
            return(false);
        }
Exemplo n.º 20
0
 public void Abs_ShouldBehaveAsMathAbs_IfLong(long value) => Assert.Equal(Math.Abs(value), value.Abs());