Extremely large numbers in c++

T

Todd.Branchflower

Hey everyone,

I am new to c++ and the group and would appreciate your help.

I was doing a problem on projecteuler.net in which you had to find the
largest factor of an extremely large number. Unfortunately, I didn't
know how to accommodate a number of this size in c++. After
developing my algorithm and writing it in c++, I wrote the program in
ruby (a bit easier for large numbers) and found the answer. Looking
in the forum at other people's code, I was able to change mine to
work:

#include <iostream>
#include <cmath>
using namespace std;

int main() {

long long N=317584931803LL;

for (long long int i=2; i<=sqrt(N); i++) if (!(N%i)) N/=i;

cout << N;

string wait;

cin >> wait;

return 0;
}


My question is, what is the significance of the LL convention
following the number?? Why won't the code work without it? Also,
some people didn't need the LL when they declared N as __int64. This
doesn't work on my compiler (Dev-C++), as the compiler says the number
is too large for the __int64 data type. Why? Final question, how can
I compile programs at the command line with Dev-C++?

Thanks in advance for your help,
Todd
 
V

Victor Bazarov

I am new to c++ and the group and would appreciate your help.

I was doing a problem on projecteuler.net in which you had to find the
largest factor of an extremely large number. Unfortunately, I didn't
know how to accommodate a number of this size in c++. After
developing my algorithm and writing it in c++, I wrote the program in
ruby (a bit easier for large numbers) and found the answer. Looking
in the forum at other people's code, I was able to change mine to
work:

#include <iostream>
#include <cmath>
using namespace std;

int main() {

long long N=317584931803LL;

This is not yet a feature of the language. 'long long' has not yet
been standardised and probably exists as an extension to the language
offered by your compiler.
for (long long int i=2; i<=sqrt(N); i++) if (!(N%i)) N/=i;

cout << N;

string wait;

cin >> wait;

return 0;
}


My question is, what is the significance of the LL convention
following the number??

That's to make the literal to have the type 'long long', most likely.
Why won't the code work without it?

Since 'long long' is not a standard type yet, nothing can be said
about the well-formedness of your code with or without it. It's
an extension and probably up to the compiler. It might work without
it or it might not work.
Also,
some people didn't need the LL when they declared N as __int64. This
doesn't work on my compiler (Dev-C++), as the compiler says the number
is too large for the __int64 data type.

'__int64' type is also non-standard, compiler support for it is not
required, and you can only find the explanation in your compiler's
manual.

Your compiler documentation or technical support should be able to
answer this.
Final question, how can
I compile programs at the command line with Dev-C++?

That's not a language question. Please refer to the documentation
for your compiler and/or the OS.

V
 
J

James Kanze

This is not yet a feature of the language. 'long long' has
not yet been standardised and probably exists as an extension
to the language offered by your compiler.

That's not really an accurate portrayal of the situation. "long
long" has been standardized, for a couple of years now. The
version of the C++ standard containing this feature is still
undergoing work, however, in other sections, and has not been
officialized. This feature is directly derived from C (where it
is official), and thus is quite stable and almost certainly will
not change between now and when the standard is officialized; as
a result, it would be unthinkable that a recent compiler didn't
implement it. It's far more portable than, e.g. export (which
is standard).

It might cause problems with older compilers, however.
That's to make the literal to have the type 'long long', most
likely.

Not "most likely". It's according to the C standard, and the
next C++ standard.
Since 'long long' is not a standard type yet, nothing can be
said about the well-formedness of your code with or without
it. It's an extension and probably up to the compiler. It
might work without it or it might not work.
This is not yet a feature of the language. 'long long' has
not yet been standardised and probably exists as an extension
to the language offered by your compiler.

That's not really an accurate portrayal of the situation. "long
long" has been standardized, for a couple of years now. The
version of the C++ standard containing this feature is still
undergoing work, however, in other sections, and has not been
officialized. This feature is directly derived from C (where it
is official), and thus is quite stable and almost certainly will
not change between now and when the standard is officialized; as
a result, it would be unthinkable that a recent compiler didn't
implement it. It's far more portable than, e.g. export (which
is standard).

It might cause problems with older compilers, however.


That is simply false. The behavior of the LL suffix and of
long long is well defined, both by the C standard and the
current draft C++ standard. An older compiler might not
implement it, but any compiler which implements it will (or
should) do so according to the standard. It's not an arbitrary
compiler extension, it's C99 and C++0x.
'__int64' type is also non-standard, compiler support for it
is not required, and you can only find the explanation in your
compiler's manual.

"__int64" is non-standard. It was one of the logical extensions
for supporting 64 bit integers before "long long".

I don't understand the error message from Dev-C++, however.
There are serveral issues:

-- If the compiler does support __int64, the only logical
meanings for it would be a 64 bit or a 64 byte integral
type. 317584931803 will easily fit in either, so no message
concerning its appropriateness for an __int64 data type
makes any sense.

-- If the numeric constant is entered without the LL suffix,
and the compiler doesn't support long long, then if long is
only 32 bits, the constant itself is illegal, the program is
ill formed, and the compiler must issue a diagnostic. (If
the compiler supports long long, then there is no problem.)

-- If the numeric constant is entered with the LL suffix, and
the compiler doesn't support long long, then the token is
illegal, the program is ill formed, and the compiler must
issue a diagnostic. (Again, if the compiler does support
long long, there should be no problem.)
Your compiler documentation or technical support should be able to
answer this.

No need to go so far. Just a little knowledge of C++, C and the
recent history of the language should suffice. Use of 64 bit
integers can still cause some portability problems today,
although if you can restrict your targets to recent versions of
the compilers you use, long long is portable.
That's not a language question. Please refer to the
documentation for your compiler and/or the OS.

Finally, a correct answer. How to invoke the compiler so that
it supports long long is implementation dependent. (Note, for
example, that g++ doesn't support strict standards compliance
for anything later than C++98, and if you use the -std=c++98
-pedantic options, it very logicallyl turns off support for long
long. IIRC, dropping the -pedantic is sufficient.)
 
J

jacob navia

(e-mail address removed) wrote:

Hi Todd.

If you do not mind using plain C (with some C++ like operator
overloading) you should use lcc-win, a free compiler for windows.

It features 352 bits floating point, 128 bit integers, true
long double (80 bits) and other goodies. It has too an
extensive math library.

Download it at the URL below
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top