8 Replies - 1283 Views - Last Post: 17 September 2011 - 10:04 AM Rate Topic: -----

#1 Hadean Fall  Icon User is offline

  • D.I.C Head


Reputation: -5
  • View blog
  • Posts: 241
  • Joined: 05-September 11

largest variable in C++

Posted 15 September 2011 - 05:46 PM

hi everyone, i just had a quick question, what is the largest variable in C++, it needs to be big enough to compute e = mc squared, i can't find one online that will hole like 20 digits, and i am using Xcode to compile, thanks in advance :bigsmile:
Is This A Good Question/Topic? 0
  • +

Replies To: largest variable in C++

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,332
  • Joined: 08-August 08

Re: largest variable in C++

Posted 15 September 2011 - 05:57 PM

Numbers can be any size if you use an arbitrary precision library. Do a search on:
c++ arbitrary precision library
Was This Post Helpful? 1
  • +
  • -

#3 Hadean Fall  Icon User is offline

  • D.I.C Head


Reputation: -5
  • View blog
  • Posts: 241
  • Joined: 05-September 11

Re: largest variable in C++

Posted 15 September 2011 - 06:01 PM

thanks appreciate it
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: largest variable in C++

Posted 16 September 2011 - 06:18 AM

The largest intrinsic C++ type is unsigned long long. It can hold numbers up to 263-1, or roughly 9.2x1018.
Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: largest variable in C++

Posted 16 September 2011 - 06:37 AM

An unsigned long long must be at least 264-1 (18446744073709551615). The signed long long must be at least 263-1.


Jim
Was This Post Helpful? 2
  • +
  • -

#6 Ricky65  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 115
  • Joined: 03-June 10

Re: largest variable in C++

Posted 16 September 2011 - 12:09 PM

In C++11, discounting an arbitrary precision arithmetic library, the largest integer available for your architecture is std::uintmax_t defined in header <cstdint>. This is usually unsigned long long but doesn't necessarily have to be.
Was This Post Helpful? 1
  • +
  • -

#7 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Re: largest variable in C++

Posted 16 September 2011 - 06:16 PM

string::size_type could work too, right?
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: largest variable in C++

Posted 16 September 2011 - 06:30 PM

Probably not. The std::string::size_type is on many systems defined the same as an unsigned int (4,294,967,295 on my system).

If you are using C++ and you want to know how big or small a number a certain variable can hold you should check out some of the functions defined in the limits header.
#include <iostream>
#include <string>
#include <limits>

using namespace std;

int main()
{
   cout << "Minimum value for unsigned int          : " << numeric_limits<unsigned int>::min() << endl;
   cout << "Maximum value for unsigned int          : " << numeric_limits<unsigned int>::max() << endl;
   cout << "Minimum value for std::string::size_type: " << numeric_limits<std::string::size_type>::min() << endl;
   cout << "Maximum value for std::string::size_type: " << numeric_limits<std::string::size_type>::max() << endl;
   cout << "Minimum value for unsigned long long    : " << numeric_limits<unsigned long long>::min() << endl;
   cout << "Maximum value for unsigned long long    : " << numeric_limits<unsigned long long>::max() << endl;
   cout << "Minimum value for signed long long      : " << numeric_limits<long long>::min() << endl;
   cout << "Maximum value for signed long long      : " << numeric_limits<long long>::max() << endl;

   return 0;
}



Output on my machine:

Quote

Minimum value for unsigned int : 0
Maximum value for unsigned int : 4294967295
Minimum value for std::string::size_type: 0
Maximum value for std::string::size_type: 4294967295
Minimum value for unsigned long long : 0
Maximum value for unsigned long long : 18446744073709551615
Minimum value for signed long long : -9223372036854775808
Maximum value for signed long long : 9223372036854775807

For C you should check out the limits.h header file.

Jim

This post has been edited by jimblumberg: 16 September 2011 - 06:34 PM

Was This Post Helpful? 2
  • +
  • -

#9 Hadean Fall  Icon User is offline

  • D.I.C Head


Reputation: -5
  • View blog
  • Posts: 241
  • Joined: 05-September 11

Re: largest variable in C++

Posted 17 September 2011 - 10:04 AM

thanks guys, i solved the problem, turns out i needed to take the commas ot of the number i was assainging to the variable, oops, my bad
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1