0

I'm trying to code a function using C++ and Xcode as compiler that will test if a is palindrome or not. The code works well when the argument is a "C++ born" type (such as int, long, double etc.) but I want to use the function for larger values. So I used an argument of type BigInteger. But the compiler gives an error on the line

 BigInteger q = x - floor(x.toLong()/10)*10

saying that Conversion from 'double' to 'const BigInteger' is ambiguous. Here is the whole code :

#include <iostream>
#include "BigInteger.hh"

using namespace std;
bool isPalindrom(BigInteger x){
long ch = ceil(log10(x.toUnsignedLong())), n[ch];
//    cout << floor(log10(x)) + 1 << endl;
for (int i = 0; i <= ch; i++){
    BigInteger q = x - floor(x.toLong()/10)*10;
    n[i] = q.toInt();
    //        cout << n[i] << endl;
    x /= 10;
}

for (long i = 0; i <= ceil(ch); i++){
    if (n[i] != n[ch - i]){
        return false;
    }
}
return true;
}

How can I solve this problem?

moray95
  • 937
  • 2
  • 9
  • 12
  • It is not the whole code, you did not show your `BigInteger.hh` header file, which might be faulty.... – Basile Starynkevitch Feb 12 '13 at 19:10
  • FYI "C++ born" types are called PODs (plain old datatypes). –  Feb 12 '13 at 19:11
  • 1
    I'm going to assume that BigInteger doesn't have a built in cast type here, but has a conversion function instead? Haven't used that one yet... – Michael Dorgan Feb 12 '13 at 19:11
  • 1
    Call me names, but I'd actually render that number to string and then compare with loop from both ends. – Bartek Banachewicz Feb 12 '13 at 19:13
  • What conversions does BigInteger support? – Bill Feb 12 '13 at 19:16
  • 3
    You don't get any benefit from `class BigInteger` if you're just going to access it using `toLong()` or `toUnsignedLong()`. And yes, there is no conversion from `double` to `BigInteger`, for good reasons. – aschepler Feb 12 '13 at 19:17
  • The `BigInteger.hh` file is in the link. It's a downloadable library. And there is no way that I can do this operation? – moray95 Feb 12 '13 at 19:17
  • 1
    @H2CO3 Not quite. A class can be a POD (and the term is most commonly associated with classes). See for example http://stackoverflow.com/questions/146452/what-are-pod-types-in-c . "Fundamental type" is probably the term you're looking for. – JBentley Feb 12 '13 at 19:30
  • @JonBentley Isn't `class` a C++-specific kind of type? Correct me if I'm wrong, but I haven't yet encountered a flavour of C that had classes (except Objective-C of course). –  Feb 12 '13 at 19:31
  • @H2C03 Are you referring to the actual URL itself? The page is about C++, even though the URL seems to imply that it's C. – JBentley Feb 12 '13 at 19:33

2 Answers2

1

There's little point in using BigInteger if you're going to convert to longs all the time.

You can write that thing using only BigInteger operations, in exactly the same way as you would with primitive integers:

bool isPalindrome(BigInteger x){
   std::vector<int> digits;
   while (x > 0)
   {
      digits.push_back((x % 10).toInt());
      x /= 10;
   }

   size_t sz = digits.size();
   for (size_t i = 0; i < sz; i++){
      if (digits[i] != digits[sz - i - 1]){
         return false;
      }
   }
   return true;
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

perhaps

  BigInteger q (static_cast<long>(x - floor(x.toLong()/10)*10));

might make the compiler happier. Look inside BigInteger.hh for the public constructors. Notice that floor gives a double, hence the substraction gives also a double, and BigInteger has no constructor for that.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547