Пример #1
0
        private static LexoDecimal Middle(LexoDecimal left, LexoDecimal right)
        {
            var sum   = left.Add(right);
            var mid   = sum.Multiply(LexoDecimal.Half(left.GetSystem()));
            var scale = left.GetScale() > right.GetScale() ? left.GetScale() : right.GetScale();

            if (mid.GetScale() > scale)
            {
                var roundDown = mid.SetScale(scale, false);
                if (roundDown.CompareTo(left) > 0)
                {
                    return(roundDown);
                }

                var roundUp = mid.SetScale(scale, true);
                if (roundUp.CompareTo(right) < 0)
                {
                    return(roundUp);
                }
            }

            return(mid);
        }
Пример #2
0
        private static LexoDecimal Between(LexoDecimal oLeft, LexoDecimal oRight)
        {
            if (oLeft.GetSystem() != oRight.GetSystem())
            {
                throw new LexoException("Expected same system");
            }

            var         left  = oLeft;
            var         right = oRight;
            LexoDecimal nLeft;

            if (oLeft.GetScale() < oRight.GetScale())
            {
                nLeft = oRight.SetScale(oLeft.GetScale(), false);
                if (oLeft.CompareTo(nLeft) >= 0)
                {
                    return(Middle(oLeft, oRight));
                }

                right = nLeft;
            }

            if (oLeft.GetScale() > right.GetScale())
            {
                nLeft = oLeft.SetScale(right.GetScale(), true);
                if (nLeft.CompareTo(right) >= 0)
                {
                    return(Middle(oLeft, oRight));
                }

                left = nLeft;
            }

            LexoDecimal nRight;

            for (var scale = left.GetScale(); scale > 0; right = nRight)
            {
                var nScale1 = scale - 1;
                var nLeft1  = left.SetScale(nScale1, true);
                nRight = right.SetScale(nScale1, false);
                var cmp = nLeft1.CompareTo(nRight);
                if (cmp == 0)
                {
                    return(CheckMid(oLeft, oRight, nLeft1));
                }

                if (nLeft1.CompareTo(nRight) > 0)
                {
                    break;
                }

                scale = nScale1;
                left  = nLeft1;
            }

            var mid = Middle(oLeft, oRight, left, right);

            int nScale;

            for (var mScale = mid.GetScale(); mScale > 0; mScale = nScale)
            {
                nScale = mScale - 1;
                var nMid = mid.SetScale(nScale);
                if (oLeft.CompareTo(nMid) >= 0 || nMid.CompareTo(oRight) >= 0)
                {
                    break;
                }

                mid = nMid;
            }

            return(mid);
        }