private static string FormatDecimal(LexoDecimal dec) { var formatVal = dec.Format(); var val = new StringBuilder(formatVal); var partialIndex = formatVal.IndexOf(NumeralSystem.GetRadixPointChar()); var zero = NumeralSystem.ToChar(0); if (partialIndex < 0) { partialIndex = formatVal.Length; val.Append(NumeralSystem.GetRadixPointChar()); } while (partialIndex < 6) { val.Insert(0, zero); ++partialIndex; } while (val[val.Length - 1] == zero) { val.Length = val.Length - 1; } return(val.ToString()); }
private LexoRank(string value) { _value = value; var parts = _value.Split('|'); Bucket = LexoRankBucket.From(parts[0]); Decimal = LexoDecimal.Parse(parts[1], NumeralSystem); }
public static LexoRank From(LexoRankBucket bucket, LexoDecimal dec) { if (!dec.GetSystem().Name.Equals(NumeralSystem.Name)) { throw new LexoException("Expected different system"); } return(new LexoRank(bucket, dec)); }
private static LexoDecimal CheckMid(LexoDecimal lBound, LexoDecimal rBound, LexoDecimal mid) { if (lBound.CompareTo(mid) >= 0) { return(Middle(lBound, rBound)); } return(mid.CompareTo(rBound) >= 0 ? Middle(lBound, rBound) : mid); }
public LexoRank GenNext() { if (IsMin()) { return(new LexoRank(Bucket, InitialMinDecimal)); } var ceilInteger = Decimal.Ceil(); var ceilDecimal = LexoDecimal.From(ceilInteger); var nextDecimal = ceilDecimal.Add(EightDecimal); if (nextDecimal.CompareTo(MaxDecimal) >= 0) { nextDecimal = Between(Decimal, MaxDecimal); } return(new LexoRank(Bucket, nextDecimal)); }
public LexoRank GenPrev() { if (IsMax()) { return(new LexoRank(Bucket, InitialMaxDecimal)); } var floorInteger = Decimal.Floor(); var floorDecimal = LexoDecimal.From(floorInteger); var nextDecimal = floorDecimal.Subtract(EightDecimal); if (nextDecimal.CompareTo(MinDecimal) <= 0) { nextDecimal = Between(MinDecimal, Decimal); } return(new LexoRank(Bucket, nextDecimal)); }
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); }
private LexoRank(LexoRankBucket bucket, LexoDecimal dec) { _value = bucket.Format() + "|" + FormatDecimal(dec); Bucket = bucket; Decimal = dec; }
private static LexoDecimal Middle(LexoDecimal lBound, LexoDecimal rBound, LexoDecimal left, LexoDecimal right) { var mid = Middle(left, right); return(CheckMid(lBound, rBound, mid)); }
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); }