This project is read-only.
1
Vote

Bug in Float.Parse

description

Failing unit test:
            var doubles = new double[] {1, 2, 3};
            var doublesString = Float.ToString(null, CultureInfo.InvariantCulture, doubles);
            var doublesCopy = Float.Parse(doublesString, NumberStyles.Any, CultureInfo.InvariantCulture);

            Assert.AreEqual(doublesCopy, doubles);
The reason is that in the Parse method of the Float class, the splitted array is looped, but not correctly passed to the internal Parse method:
public static Number[] Parse(string value, NumberStyles numberStyle, IFormatProvider formatProvider)
{
    var strings = value.Split(whitespace, StringSplitOptions.RemoveEmptyEntries);
    var numbers = new Number[strings.Length];

    for (var i = 0; i < strings.Length; i++)
    {
        numbers[i] = Integer.Parse(value, numberStyle, formatProvider);
    }

    return numbers;
}
should be
public static Number[] Parse(string value, NumberStyles numberStyle, IFormatProvider formatProvider)
{
    var strings = value.Split(whitespace, StringSplitOptions.RemoveEmptyEntries);
    var numbers = new Number[strings.Length];

    for (var i = 0; i < strings.Length; i++)
    {
        numbers[i] = Integer.Parse(strings[i], numberStyle, formatProvider);
    }

    return numbers;
}
And maybe use double.Parse instead of Integer.Parse

comments