How to use the standard fuctions with InfInt?

Mar 3, 2016 at 10:26am
Hello to everybody. When i started programming, i always found an article that suited my needs. In this case, i can't find anything, so i thought i 'll write my question and hopefully someone with experience will be able to answer it. I thank you in advance.
Me program is about finding if a number is a prime or not. I have installed the InfInt library of Dev C++ and the problem is that i can't use the stardand fuctions of C++, because they only work with double or long, but not with InfInt. I have writen more complex programs about primes, but with long long int, so you can't put as input a really big integer. Here is my code:

#include <iostream>
#include <cmath>
#include <ctime>
#include <stdlib.h>
#include <windows.h>
#include "InfInt.h"
//#pragma GCC system_header
#include <bits/c++config.h>
#include <bits/cpp_type_traits.h>
#include <ext/type_traits.h>
#include <math.h>
#include <assert.h>
#include <ios>
#include <bits/ostream_insert.h>
#include <iosfwd>
#include <exception> // For ios_base::failure
#include <bits/char_traits.h> // For char_traits, streamoff, streamsize, fpos
#include <bits/localefwd.h> // For class locale
#include <bits/ios_base.h> // For ios_base declarations.
#include <streambuf>
#include <bits/basic_ios.h>

using namespace std;


int countDigitsInInteger(InfInt number)
{
int count =0;
while(number>0)
{
count++;
number=number/10;
}
return count;
}

int main ()

{
InfInt n;

bool once;

do
{

cout << "Doste ena poly megalo akeraio: ";
once = true;
if (!(cin >> n)||(n<2))
{
cout << "Parakalw mhn dinete xaraktires i arithmous mikroterous tou 3!"<<endl;
cin.clear();
cin.ignore(100000, '\n');
once = false;


}

} while (once == false);



if (n % 2 == 0)
{
cout << "O arithmos einai zygos!"<<endl;
}
else if (n % 2 == 1)
{
cout << "O arithmos einai monos!"<< endl;
}



//cout << "log("<<n<<")="<<log((double)n)<<endl;



bool prime;


cout << "The given number has: "<< countDigitsInInteger(n) << " digits"<<endl;
for (InfInt i=2; i<sqrt(n); i++)
{

if (n % i == 0)
{
prime = false;
break;
}
else if (n % i != 0)
{
prime = true;
}


}


if (prime)
{
cout << "O arithmos "<< n <<" einai prwtos!"<<endl;
}
else if (prime == false)
{
cout << "O arithmos "<< n <<" den einai prwtos!"<< endl;
}




cin.get();
cout << "Patiste opoiodipote plktro gia synexeia!"<<endl;
cin.ignore(100000, '\n');






















return 0;
}
The compiler error status report:

D:\Ôá ðñïãñÜììáôÜ ìïõ dev c++\PrimesInf.cpp In function 'int main()':
82 27 D:\Ôá ðñïãñÜììáôÜ ìïõ dev c++\PrimesInf.cpp [Error] no matching function for call to 'sqrt(InfInt&)'
82 27 D:\Ôá ðñïãñÜììáôÜ ìïõ dev c++\PrimesInf.cpp [Note] candidates are:
46 0 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\cmath In file included from c:\program files (x86)\dev-cpp\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.1/include/c++/cmath
2 D:\Ôá ðñïãñÜììáôÜ ìïõ dev c++\PrimesInf.cpp from D:\Ôá ðñïãñÜììáôÜ ìïõ dev c++\PrimesInf.cpp
152 18 c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\include\math.h [Note] double sqrt(double)
152 18 c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\include\math.h [Note] no known conversion for argument 1 from 'InfInt' to 'double'
2 0 D:\Ôá ðñïãñÜììáôÜ ìïõ dev c++\PrimesInf.cpp In file included from D:\Ôá ðñïãñÜììáôÜ ìïõ dev c++\PrimesInf.cpp
497 5 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\cmath [Note] template<class _Tp> typename __gnu_cxx::__enable_if<std::__is_integer<_Tp>::__value, double>::__type std::sqrt(_Tp)
497 5 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\cmath [Note] template argument deduction/substitution failed:
c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\cmath In substitution of 'template<class _Tp> typename __gnu_cxx::__enable_if<std::__is_integer<_Tp>::__value, double>::__type std::sqrt(_Tp) [with _Tp = InfInt]':
82 27 D:\Ôá ðñïãñÜììáôÜ ìïõ dev c++\PrimesInf.cpp required from here
497 5 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\cmath [Error] no type named '__type' in 'struct __gnu_cxx::__enable_if<false, double>'
489 3 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\cmath [Note] long double std::sqrt(long double)
489 3 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\cmath [Note] no known conversion for argument 1 from 'InfInt' to 'long double'
485 3 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\cmath [Note] float std::sqrt(float)
485 3 c:\program files (x86)\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.7.1\include\c++\cmath [Note] no known conversion for argument 1 from 'InfInt' to 'float'
Mar 3, 2016 at 11:03am
The InfInt class has a member function intSqrt() that returns the integer square root.
Last edited on Mar 3, 2016 at 11:04am
Mar 3, 2016 at 11:11am
Thank you Peter87 for your answer. I have already tried and the compiler doesn't aknwoledge the existence of the fuction.I read that in the readme file, but it didn't apply.
Mar 3, 2016 at 11:23am
Did you call it correctly? Note that it's a member function.

 
cout << "The integer square root of " << n << " is " << n.intSqrt() << ".\n";
Last edited on Mar 3, 2016 at 11:23am
Mar 3, 2016 at 11:42am
Problem Solved! Just one more thing for the same problem. I want to put really big integers for input and i can't possibly write all the digits. Is there a way to write the input number in a scientific form (ex 5*10^2345+1). I tried to input that but it reads an other number with fewer digits.
Mar 3, 2016 at 1:00pm
The normal way you write scientific notation in C++ would be 5e2345, but it doesn't look like InfInt support this format. There appear to be no way of working with exponents at all.

One possible way is to construct a string that starts with '1' followed by 2345 '0', pass it to the InfInt constructor and then multiply by 5 and add 1, like this:
 
5 * InfInt("1" + string(2345, '0')) + 1
Not sure how that would perform though. If you do it this way you should probably create a function (or a user-defined literal) so that creating a InfInt object using scientific notation looks a bit nicer.
Mar 3, 2016 at 2:49pm
Thanks for the extra answer Peter87! Although i am not sure how to construct the fuction. It needs extra thinking!
Topic archived. No new replies allowed.