Running on Widows 7 with MSVS 2010
I am following this tutorial to understand how to use MPIR library for adding two big integers
I understand this library should help me in adding very big numbers as shown in the program below:
#include < stdio.h>
#include < stdlib.h>
#include < gmpxx.h>
#include < iostream>
using namespace std;
void main(int argc, char *argv[])
{
mpz_class answer_a = 111111111111111111111111111111111111111111111111;
mpz_class answer_b = 111111111111111111111111111111111111111111111111;
mpz_class answer_c;
answer_c= answer_b + answer_a ;
cout << answer_c<<"\n";
}
But still I get error C2177: constant too big.
Did I misunderstand MPIR ?
Such constant is (very likely) too big for standard integer types. You should use char * constructor instead:
void mpz_class::mpz_class (const char *s)
For example:
mpz_class answer_a("111111111111111111111111111111111111111111111111");
to make this work you need to include suitable MPIR C++ interface header (notice that <gmpxx.h> is from C++ interface of GNU MP library):
#include <mpirxx.h>
See 12.2 C++ Interface Integers chapter in MPIR documentation for more details.
Related
I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}
Look my code seems to be correct, according to all the documentation I can find online. My IDE is MS Visual Studio Xpress 4 Windows Desktop 2012, and it's compiler is throwing up the error:
Error 1 error C3861: 'setenv': identifier not found e:\users\owner\documents\visual studio 2012\projects\project1\project1\source1.cpp 18 1 Project1.
Help me!!!
#include <windows.h>
#include <sstream>
#include <ostream>
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
using namespace std;
int howManyInClass = 0;
int main(){
long checklength = sizeof(getenv("classSize"))/sizeof(*getenv("classSize"));
if (checklength==0){
cout<<"Please enter the ammount of students in your class";
cin>> howManyInClass;
cin.ignore();
setenv("classSize", howManyInClass, 1);}
};
Microsoft's runtime library doesn't support the standard setenv() function. You could use their replacement _putenv() or, for portable code, I prefer to use a simple wrapper.
Here's my wrapper with the standard interface:
int setenv(const char *name, const char *value, int overwrite)
{
int errcode = 0;
if(!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, name);
if(errcode || envsize) return errcode;
}
return _putenv_s(name, value);
}
You can either use _putenv() which takes a string parameter as the string classSize=7;
ostringstream classSize;
classSize << "classSize=" << howManyInClass;
_putenv(classSize.str().c_str());
...or (preferably) the security enhanced _putenv_s() that takes the key and the value as separate (const char*) parameters;
ostringstream classSize;
classSize << howManyInClass;
_putenv_s("classSize", classSize.str().c_str());
Try _putenv instead of setenv.
msdn _putenv
the reason you encountered the linkage error is that, if you take a look at the content of the library of stdlib.h, you will find that, setenv() is not declared there. At the first glance, it is a C standard API, but looks like Windows do not follow all of the standard. Or, you might be able to configure your VS to use CRT instead of Windows runtime, in that case, I think setenv will be identified.
Hi I am new to C++ and Code::Block
I am trying to make a simple code to test it, using strings.
when I compile the code there is no problem, but when I try to debug it, Code::Block gives me the following warning:
Cannot open file:
File:../../../../../src/gcc-4.9.2/libgcc/unwind-sjlj.c
Info: "Multiple information windows with the same message have been
supressed."
Image of the error FYI:
Part of the code that gives me an error.
inside main function
#include <iostream>
#include <string>
int main ()
{
std::mystring("What's wrong with strings");
return 0;
}
I realise that this error only occurs when I try to debug a string or a file containing a string.
Any help would be appreciated.
some other information that might help:
Code::Block 16.01
Compiler MinGW gcc4.9.2
Windows 7 Professional 32 bits SP1
First of all, to use strings you must include the file header string. And the name of the type string is..std::string, not std::mystring.
#include <string>
int main(int argc, char** argv)
{
std::string mystring("Nothing's wrong with strings");
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
string mystring = "Whats wrong with my string";
return 0;
}
If you write it in the following way, it should work.
It's safer to define strings like I showed it. It will be also easier for you if you add using namespace std in the beginning of every program if you are new to C++.
I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}
Is element wise multiplication (%) speed in armadillo depends whether LAPACK/BLAS is installed? Im currently running armadillo without them installed and speed is awful.
Ok here is the simplest code, which takes eternity to calculate
#include <iostream>
#include "conio.h"
#include "armadillo"
using namespace arma;
using namespace std;
int main(int argc, char** argv)
{
int n=250;
mat X=ones(n,n);
mat quan;
for (int xi=1;xi<=256;xi++)
{
quan = exp(X)%exp(X);
}
getch();
return 0;
}
Make sure you have optimisation flags enabled in your compiler settings (eg. in GCC or Clang, use -O2 or -O3). Armadillo makes use of template metaprogramming, and like any C++ template library, this absolutely requires optimisation enabled within the compiler to be effective. For example, this also applies to C++ template libraries such as Boost.
Why are you calculating exp(X) twice? You're not benchmarking elementwise multiplication; you're apparently benchmarking exp(). Also, why are you not using expmat() or expmat_sym()?