Good day all.
I am getting back into C# after a few years, and am little stuck on an error I'm receiving. I wrote a practice program to determine whether a word is a palindrome (same backwards and forwards). I am however getting an error that confuses me.
I try to call Math.Ceiling(word.Length / 2) to get the middle of a word, but it gives me the following error: "The call is ambiguous between the following methods or properties:'Math.Ceiling(decimal) and Math.Ceiling(double)"
Although I get that this is the compiler worrying about identifying the correct overloaded method, but am unsure how to indicate which I am using. I also don't get why should this matter?
Here is my full program:
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string word = "Deleveled";
word = word.ToUpper();
bool isPalindrome = true;
for (var i = 0; i <= Math.Ceiling(word.Length / 2); i++)
{
char tmp = word[word.Length - i - 1];
if (Char.ToUpper(word[i]) != Char.ToUpper(tmp))
{
isPalindrome = false;
break;
}
}
Console.WriteLine(isPalindrome);
Console.ReadLine();
}
}
}
I would greatly appreciate it f anyone could help me understand what the issue is here?
My thanks in advance
Integer division always results in an integer; so: word.Length / 2
returns int
(it rounds down).
When you call Math.Ceiling
on this, you are passing an integer, but there is not Math.Ceiling(int)
. It has two choices: Math.Ceiling(double)
and Math.Ceiling(decimal)
, but: it could use either, and neither of those is better from the compiler's perspective.
Frankly, it might be simpler to use the general purpose "page count" formula:
int pages = (items + pageSize - 1) / pageSize;
which in this case becomes simply:
int upperLimit = (word.Length + 1) / 2;
(note that the general purpose page count formula can also be written int pages = ((items - 1) / pageSize) + 1;
, although in this case it would be harder to substitute your fixed page size)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With