6 Replies - 1822 Views - Last Post: 11 July 2011 - 08:15 AM Rate Topic: -----

#1 Marrodriguez  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 27
  • Joined: 15-April 11

C++ Integer constant is too large for "long" type

Posted 11 July 2011 - 07:52 AM

Hey.. I'm trying to compile this very simple program, however I keep getting the error message : Integer constant is too large for "long" type.... Here is my code:

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
int x;

x = (-1138071060)- (-17768902608) + (11625212826) -( -32060064060) + (-53825741973) - (-1088579250);
cout<< x;
cin.get();
}



How can I fix this?

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Integer constant is too large for "long" type

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: C++ Integer constant is too large for "long" type

Posted 11 July 2011 - 07:55 AM

Change int x; to long long x;. Your result is too big to fit in a standard int.

This post has been edited by PlasticineGuy: 11 July 2011 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 Marrodriguez  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 27
  • Joined: 15-April 11

Re: C++ Integer constant is too large for "long" type

Posted 11 July 2011 - 08:09 AM

View PostPlasticineGuy, on 11 July 2011 - 07:55 AM, said:

Change int x; to long long x;. Your result is too big to fit in a standard int.


I just did but it continues to present the same error :s :

#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
long long x;

x = (-1138071060)- (-17768902608) + (11625212826) -( -32060064060) + (-53825741973) - (-1088579250);
cout<< x;
cin.get();

}



any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: C++ Integer constant is too large for "long" type

Posted 11 July 2011 - 08:10 AM

Why do you have such a large number anyway? You'll need to find and download a big int library to work with these numbers.
Was This Post Helpful? 0
  • +
  • -

#5 Marrodriguez  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 27
  • Joined: 15-April 11

Re: C++ Integer constant is too large for "long" type

Posted 11 July 2011 - 08:13 AM

View PostPlasticineGuy, on 11 July 2011 - 08:10 AM, said:

Why do you have such a large number anyway? You'll need to find and download a big int library to work with these numbers.


I need numbers that big because in the end the program needs to find the determinant of a 6x6, 7x7, 8x8, and 9x9 matrix.

Any suggestions on a library that can handle this?
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: C++ Integer constant is too large for "long" type

Posted 11 July 2011 - 08:14 AM

There are various methods to deal with very large integers. You could use bigint libraries:
http://www.google.co...=UTF-8&oe=UTF-8
https://mattmccutchen.net/bigint/
or you could roll your own as I did in this Fibonacci snippet:
http://www.dreaminco...snippet6206.htm
I used C++ vectors, but with some extra effort you could do it with arrays in C.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,751
  • Joined: 25-December 09

Re: C++ Integer constant is too large for "long" type

Posted 11 July 2011 - 08:15 AM

You should tell the compiler that your constant is larger than an int (-1138071060L) for a long, (-1138071060LL) for a long long. Also note that the long long is only guaranteed to be able to hold a 32 bit number, the same as a long. You can find out the limits of your system by using the functions provided in the limits include file like: numeric_limits<long>::max().

Also you can look at the climits include file to see the limits for your system.


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1