Example #1
0
        public static void Write(double value, BinaryWriter writer)
        {
            if (double.IsNaN(value))
            {
                writer.Write(nan_symbol_);
                return;
            }
            if (double.IsInfinity(value))
            {
                if (value < 0)
                {
                    writer.Write((byte)'-');
                }
                writer.Write(infinity_symbol_);
                return;
            }

            if (value == 0.0)
            {
                writer.Write((byte)'0');
                return;
            }

            if (value < 0.0)
            {
                writer.Write((byte)'-');
                value = -value;
            }

            byte[] decimal_rep = ts_decimal_rep;
            if (decimal_rep == null)
            {
                decimal_rep = ts_decimal_rep = new byte[kBase10MaximalLength + 1];
            }

            GrisuDouble v = new GrisuDouble(value);
            DiyFp       w = v.AsNormalizedDiyFp();
            // boundary_minus and boundary_plus are the boundaries between v and its
            // closest floating-point neighbors. Any number strictly between
            // boundary_minus and boundary_plus will round to v when convert to a double.
            // Grisu3 will never output representations that lie exactly on a boundary.
            DiyFp boundary_minus, boundary_plus;

            v.NormalizedBoundaries(out boundary_minus, out boundary_plus);

            int decimal_rep_length;
            int decimal_exponent;

            if (Grisu3(w, boundary_minus, boundary_plus, decimal_rep, out decimal_rep_length, out decimal_exponent))
            {
                CreateRepresentation(decimal_rep, decimal_rep_length, decimal_rep_length + decimal_exponent, writer);
            }
            else
            {
                writer.Write(Encoding.ASCII.GetBytes(value.ToString("R", CultureInfo.InvariantCulture)));
            }
        }
Example #2
0
File: Grisu.cs Project: klenin/Yuzu
        public static void Write(double value, BinaryWriter writer)
        {
            if (double.IsNaN(value)) {
                writer.Write(nan_symbol_);
                return;
            }
            if (double.IsInfinity(value)) {
                if (value < 0)
                    writer.Write((byte)'-');
                writer.Write(infinity_symbol_);
                return;
            }

            if (value == 0.0) {
                writer.Write((byte)'0');
                return;
            }

            if (value < 0.0) {
                writer.Write((byte)'-');
                value = -value;
            }

            byte[] decimal_rep = ts_decimal_rep;
            if (decimal_rep == null)
                decimal_rep = ts_decimal_rep = new byte[kBase10MaximalLength + 1];

            GrisuDouble v = new GrisuDouble(value);
            DiyFp w = v.AsNormalizedDiyFp();
            // boundary_minus and boundary_plus are the boundaries between v and its
            // closest floating-point neighbors. Any number strictly between
            // boundary_minus and boundary_plus will round to v when convert to a double.
            // Grisu3 will never output representations that lie exactly on a boundary.
            DiyFp boundary_minus, boundary_plus;
            v.NormalizedBoundaries(out boundary_minus, out boundary_plus);

            int decimal_rep_length;
            int decimal_exponent;

            if (Grisu3(w, boundary_minus, boundary_plus, decimal_rep, out decimal_rep_length, out decimal_exponent))
                CreateRepresentation(decimal_rep, decimal_rep_length, decimal_rep_length + decimal_exponent, writer);
            else
                writer.Write(Encoding.ASCII.GetBytes(value.ToString("R", CultureInfo.InvariantCulture)));
        }