Why am I receiving a "call is ambigious" error when trying to user Math.Floor with an integer?

I've just made a small program that uses the Floor method:

public static void Main(string[] args)
    {

        int i;

        decimal[] t = new decimal[30];

        for(i=0; i<t.Length; i++)
        {
            t[i]=Math.Floor(i/6);
        }

        for(i=0; i<t.Length; i++)
        {
            Console.WriteLine(""+t[i]);
        }

        Console.ReadKey(true);
    }

But it doesn't work. SharpDevelop returns this error:

The call is ambiguous between the following methods or properties: 'System.Math.Floor(decimal)' and 'System.Math.Floor(double)' (CS0121)

which is related to this line:

t[i] = Math.Floor(i/6);

Could someone explain me what's wrong?

Also: at first I tried writing int[] t = new int[30]; since my program only returns integers. But it returned a second error, saying that I should use the type decimal instead of the type int. I also tried to use the type float and the type double but it didn't work either. It seems only the type decimal works. I would also like to know why?

Jon Skeet
people
quotationmark

Could someone explain me what's wrong?

The compiler is telling you exactly what's wrong - the call is ambiguous. The fact that you're trying to use the result of the call to assign into a decimal array is irrelevant to the compiler - it only looks at the arguments to the method to try to work out what method you want to call.

The type of the expression i/6 is int, and that is convertible to both double and decimal, with neither conversion being "better" than the other, so the call is ambiguous. In fact, you don't really need the call at all - you're performing integer arithmetic anyway with the expression i / 6, which truncates towards zero - which is the equivalent to Floor for non-negative numbers. And you're only wanting to store integers anyway... All you need is:

public static void Main(string[] args)
{
    // While "values" isn't a particularly descriptive name, it's better
    // than "t"
    int[] values = new int[30];

    // Limit the scope of i - declare it in the loop header
    for (int i = 0; i < values.Length; i++)
    {
        values[i] = i / 6;
    }

    // You don't need the index here, so use a foreach loop. Generally prefer
    // foreach over for
    foreach (int value in values)
    {
        // You don't need any string concatenation here...
        Console.WriteLine(value);
    }

    Console.ReadKey(true);
}

people

See more on this question at Stackoverflow