Rhino.DToA.JS_dtobasestr C# (CSharp) Method

JS_dtobasestr() static private method

static private JS_dtobasestr ( int @base, double d ) : string
@base int
d double
return string
		internal static string JS_dtobasestr(int @base, double d)
		{
			if (!(2 <= @base && @base <= 36))
			{
				throw new ArgumentException("Bad base: " + @base);
			}
			if (double.IsNaN(d))
			{
				return "NaN";
			}
			else
			{
				if (System.Double.IsInfinity(d))
				{
					return (d > 0.0) ? "Infinity" : "-Infinity";
				}
				else
				{
					if (d == 0)
					{
						// ALERT: should it distinguish -0.0 from +0.0 ?
						return "0";
					}
				}
			}
			bool negative;
			if (d >= 0.0)
			{
				negative = false;
			}
			else
			{
				negative = true;
				d = -d;
			}
			string intDigits;
			double dfloor = Math.Floor(d);
			long lfloor = (long)dfloor;
			if (lfloor == dfloor)
			{
				// int part fits long
				intDigits = System.Convert.ToString((negative) ? -lfloor : lfloor, @base);
			}
			else
			{
				// BigInteger should be used
				long floorBits = System.BitConverter.DoubleToInt64Bits(dfloor);
				int exp = (int)(floorBits >> Exp_shiftL) & Exp_mask_shifted;
				long mantissa;
				if (exp == 0)
				{
					mantissa = (floorBits & Frac_maskL) << 1;
				}
				else
				{
					mantissa = (floorBits & Frac_maskL) | Exp_msk1L;
				}
				if (negative)
				{
					mantissa = -mantissa;
				}
				exp -= 1075;
				BigInteger x = Sharpen.Extensions.ValueOf(mantissa);
				if (exp > 0)
				{
					x = x.ShiftLeft(exp);
				}
				else
				{
					if (exp < 0)
					{
						x = x.ShiftRight(-exp);
					}
				}
				intDigits = x.ToString(@base);
			}
			if (d == dfloor)
			{
				// No fraction part
				return intDigits;
			}
			else
			{
				StringBuilder buffer;
				int digit;
				double df;
				BigInteger b;
				buffer = new StringBuilder();
				buffer.Append(intDigits).Append('.');
				df = d - dfloor;
				long dBits = System.BitConverter.DoubleToInt64Bits(d);
				int word0 = (int)(dBits >> 32);
				int word1 = (int)(dBits);
				int[] e = new int[1];
				int[] bbits = new int[1];
				b = D2b(df, e, bbits);
				//            JS_ASSERT(e < 0);
				int s2 = -((int)(((uint)word0) >> Exp_shift1) & Exp_mask >> Exp_shift1);
				if (s2 == 0)
				{
					s2 = -1;
				}
				s2 += Bias + P;
				//            JS_ASSERT(-s2 < e);
				BigInteger mlo = Sharpen.Extensions.ValueOf(1);
				BigInteger mhi = mlo;
				if ((word1 == 0) && ((word0 & Bndry_mask) == 0) && ((word0 & (Exp_mask & Exp_mask << 1)) != 0))
				{
					s2 += Log2P;
					mhi = Sharpen.Extensions.ValueOf(1 << Log2P);
				}
				b = b.ShiftLeft(e[0] + s2);
				BigInteger s = Sharpen.Extensions.ValueOf(1);
				s = s.ShiftLeft(s2);
				BigInteger bigBase = Sharpen.Extensions.ValueOf(@base);
				bool done = false;
				do
				{
					b = System.Numerics.BigInteger.Multiply(b, bigBase);
					BigInteger[] divResult = b.DivideAndRemainder(s);
					b = divResult[1];
					digit = (char)(System.Convert.ToInt32(divResult[0]));
					if (mlo == mhi)
					{
						mlo = mhi = System.Numerics.BigInteger.Multiply(mlo, bigBase);
					}
					else
					{
						mlo = System.Numerics.BigInteger.Multiply(mlo, bigBase);
						mhi = System.Numerics.BigInteger.Multiply(mhi, bigBase);
					}
					int j = b.CompareTo(mlo);
					BigInteger delta = System.Numerics.BigInteger.Subtract(s, mhi);
					int j1 = (delta.Sign <= 0) ? 1 : b.CompareTo(delta);
					if (j1 == 0 && ((word1 & 1) == 0))
					{
						if (j > 0)
						{
							digit++;
						}
						done = true;
					}
					else
					{
						if (j < 0 || (j == 0 && ((word1 & 1) == 0)))
						{
							if (j1 > 0)
							{
								b = b.ShiftLeft(1);
								j1 = b.CompareTo(s);
								if (j1 > 0)
								{
									digit++;
								}
							}
							done = true;
						}
						else
						{
							if (j1 > 0)
							{
								digit++;
								done = true;
							}
						}
					}
					//                JS_ASSERT(digit < (uint32)base);
					buffer.Append(BASEDIGIT(digit));
				}
				while (!done);
				return buffer.ToString();
			}
		}