0

I have a function that truncates significant digits by using the recommendations at: Round a double to x significant figures see: TruncateToSignificantDigits below.

I give it an input of 0.40678553 and 7 sig figs and it comes back with 0.406785499999999966558306141450884751975536346435546875

when I really want it to be represented as 0.4067855

PS I included Jon Skeet's DoubleConverter to display real numbers in the console.

using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        double Measure = TruncateToSignificantDigits(0.40678553, 7);
        Console.WriteLine(DoubleConverter.ToExactString(Measure));
    }

    internal static double TruncateToSignificantDigits(double? num, int digits)
        {

            if (num == 0)
                return 0;

            var scale = Math.Pow(10, Math.Floor(Math.Log10(Math.Abs(num.Value))) + 1 - digits);
            return scale * Math.Truncate(num.Value / scale);
        }

}


/// <summary>
/// A class to allow the conversion of doubles to string representations of
/// their exact decimal values. The implementation aims for readability over
/// efficiency.
/// </summary>
public class DoubleConverter
{    
    /// <summary>
    /// Converts the given double to a string representation of its
    /// exact decimal value.
    /// </summary>
    /// <param name="d">The double to convert.</param>
    /// <returns>A string representation of the double's exact decimal value.</return>
    public static string ToExactString (double d)
    {
        if (double.IsPositiveInfinity(d))
            return "+Infinity";
        if (double.IsNegativeInfinity(d))
            return "-Infinity";
        if (double.IsNaN(d))
            return "NaN";

        // Translate the double into sign, exponent and mantissa.
        long bits = BitConverter.DoubleToInt64Bits(d);
        // Note that the shift is sign-extended, hence the test against -1 not 1
        bool negative = (bits < 0);
        int exponent = (int) ((bits >> 52) & 0x7ffL);
        long mantissa = bits & 0xfffffffffffffL;

        // Subnormal numbers; exponent is effectively one higher,
        // but there's no extra normalisation bit in the mantissa
        if (exponent==0)
        {
            exponent++;
        }
        // Normal numbers; leave exponent as it is but add extra
        // bit to the front of the mantissa
        else
        {
            mantissa = mantissa | (1L<<52);
        }

        // Bias the exponent. It's actually biased by 1023, but we're
        // treating the mantissa as m.0 rather than 0.m, so we need
        // to subtract another 52 from it.
        exponent -= 1075;

        if (mantissa == 0) 
        {
            return "0";
        }

        /* Normalize */
        while((mantissa & 1) == 0) 
        {    /*  i.e., Mantissa is even */
            mantissa >>= 1;
            exponent++;
        }

        /// Construct a new decimal expansion with the mantissa
        ArbitraryDecimal ad = new ArbitraryDecimal (mantissa);

        // If the exponent is less than 0, we need to repeatedly
        // divide by 2 - which is the equivalent of multiplying
        // by 5 and dividing by 10.
        if (exponent < 0) 
        {
            for (int i=0; i < -exponent; i++)
                ad.MultiplyBy(5);
            ad.Shift(-exponent);
        } 
        // Otherwise, we need to repeatedly multiply by 2
        else
        {
            for (int i=0; i < exponent; i++)
                ad.MultiplyBy(2);
        }

        // Finally, return the string with an appropriate sign
        if (negative)
            return "-"+ad.ToString();
        else
            return ad.ToString();
    }

    /// <summary>Private class used for manipulating
    class ArbitraryDecimal
    {
        /// <summary>Digits in the decimal expansion, one byte per digit
        byte[] digits;
        /// <summary> 
        /// How many digits are *after* the decimal point
        /// </summary>
        int decimalPoint=0;

        /// <summary> 
        /// Constructs an arbitrary decimal expansion from the given long.
        /// The long must not be negative.
        /// </summary>
        internal ArbitraryDecimal (long x)
        {
            string tmp = x.ToString(CultureInfo.InvariantCulture);
            digits = new byte[tmp.Length];
            for (int i=0; i < tmp.Length; i++)
                digits[i] = (byte) (tmp[i]-'0');
            Normalize();
        }

        /// <summary>
        /// Multiplies the current expansion by the given amount, which should
        /// only be 2 or 5.
        /// </summary>
        internal void MultiplyBy(int amount)
        {
            byte[] result = new byte[digits.Length+1];
            for (int i=digits.Length-1; i >= 0; i--)
            {
                int resultDigit = digits[i]*amount+result[i+1];
                result[i]=(byte)(resultDigit/10);
                result[i+1]=(byte)(resultDigit%10);
            }
            if (result[0] != 0)
            {
                digits=result;
            }
            else
            {
                Array.Copy (result, 1, digits, 0, digits.Length);
            }
            Normalize();
        }

        /// <summary>
        /// Shifts the decimal point; a negative value makes
        /// the decimal expansion bigger (as fewer digits come after the
        /// decimal place) and a positive value makes the decimal
        /// expansion smaller.
        /// </summary>
        internal void Shift (int amount)
        {
            decimalPoint += amount;
        }

        /// <summary>
        /// Removes leading/trailing zeroes from the expansion.
        /// </summary>
        internal void Normalize()
        {
            int first;
            for (first=0; first < digits.Length; first++)
                if (digits[first]!=0)
                    break;
            int last;
            for (last=digits.Length-1; last >= 0; last--)
                if (digits[last]!=0)
                    break;

            if (first==0 && last==digits.Length-1)
                return;

            byte[] tmp = new byte[last-first+1];
            for (int i=0; i < tmp.Length; i++)
                tmp[i]=digits[i+first];

            decimalPoint -= digits.Length-(last+1);
            digits=tmp;
        }

        /// <summary>
        /// Converts the value to a proper decimal string representation.
        /// </summary>
        public override String ToString()
        {
            char[] digitString = new char[digits.Length];            
            for (int i=0; i < digits.Length; i++)
                digitString[i] = (char)(digits[i]+'0');

            // Simplest case - nothing after the decimal point,
            // and last real digit is non-zero, eg value=35
            if (decimalPoint==0)
            {
                return new string (digitString);
            }

            // Fairly simple case - nothing after the decimal
            // point, but some 0s to add, eg value=350
            if (decimalPoint < 0)
            {
                return new string (digitString)+
                       new string ('0', -decimalPoint);
            }

            // Nothing before the decimal point, eg 0.035
            if (decimalPoint >= digitString.Length)
            {
                return "0."+
                    new string ('0',(decimalPoint-digitString.Length))+
                    new string (digitString);
            }

            // Most complicated case - part of the string comes
            // before the decimal point, part comes after it,
            // eg 3.5
            return new string (digitString, 0, 
                               digitString.Length-decimalPoint)+
                "."+
                new string (digitString,
                            digitString.Length-decimalPoint, 
                            decimalPoint);
        }
    }
}
Community
  • 1
  • 1
LearningJrDev
  • 911
  • 2
  • 8
  • 27
  • What about just `Math.Round(number,7)`? – Spencer Wieczorek Apr 19 '17 at 21:01
  • @SpencerWieczorek So far the only thing I see in the question OP disagrees that particular number can't be represented as double... which is explained in linked duplicate. If you see some other question - edit the post to clarify and I'll definitely re-open. – Alexei Levenkov Apr 19 '17 at 21:07
  • @AlexeiLevenkov That's definitely related to the issue that's being caused, but it seems like the OP is just asking to transform `0.406785499999999966558306141450884751975536346435546875` to `0.4067855` would solve their issue. Rather than asking why the float is appearing as `0.40678549999999...`. But I might be misreading the question. – Spencer Wieczorek Apr 19 '17 at 21:11
  • Do you need the numerical number? Usually it makes more sense to represent sig fig numbers as a string, so you avoid the floating point issues. – msitt Apr 19 '17 at 21:15
  • @SpencerWieczorek let's wait for OP to clarify. Since there is no `double` value that is exactly 0.4067855 only OP would know what is acceptable alternative (maybe `decimal` instead of `double`) and what actual requirement is. (Otherwise question looks ok and actually more research than average q). – Alexei Levenkov Apr 19 '17 at 22:07
  • Yes I want an exact number that is `0.4067855` but I guess that's not possible. The main issue is that when I send the value to a request and it gets coded into javascript its coded as `0.4067854999` and not as ` `0.4067855` My main issue is that I get `0.40678553` from an ajax call, I parse it as a double and then I run it through `TruncateToSignificantDigits` and send it back in a request, but it's sent as `0.4067854999` rather than my desired `0.4067855` – LearningJrDev Apr 19 '17 at 22:58
  • Given how this problem can't be solved (or atleast by using doubles) I think the solution lies in how im encoding the data into JSON and should utilize javascripts .toPrecision() method if I need to send it to a precision of 7 places. This question can remain closed unless someone has insight on how this is no longer of encoding the number to be what I want – LearningJrDev Apr 19 '17 at 23:06
  • @LearningJrDev I don't see how this can't be solved, doing `Math.Round(0.406785499999999966558306141450884751975536346435546875,7)` will give you exactly `0.4067855` as you desired as; I stated this in my first comment. – Spencer Wieczorek Apr 20 '17 at 01:02

0 Answers0