开发者

What programming language will enable me to enter a very long number without converting it to floating point?

What would be the best way to do the following.

Enter a very long number, lets say 500,000 digits long without it going into scientific notation; and then am able to do math with it, like +2 etc.?

Thank 开发者_如何学Pythonyou in advance.

EDIT: It is a 500,000 digit, positive integer.


Python and Java have native support, libraries exist for C++, C, .NET, ...


I know that Erlang has support for unlimited size int arithmetics.


Python does this out of the box with no special library. So does 'bc' (which is a full programming language masquerading as a calculator) for Unix systems.


Python is pretty good on its own, but better with gmpy (which bridges it to the GMP library others have mentioned, or alternately to the MPIR kinda-work-alike one [[work in progress;-)]]). Consider:

$ python -mtimeit -s'x=int("1"*9999); y=int("2"*9999)' 'x*y'
100 loops, best of 3: 6.46 msec per loop

i.e., in pure Python, multiply two 10K-digits ints takes 6.5 milliseconds or so. And...:

$ python -mtimeit -s'from gmpy import mpz; x=mpz("1"*9999); y=mpz("2"*9999)' 'x*y'
1000 loops, best of 3: 326 usec per loop

...with gmpy at hand, the operation will be about 20 times faster. If you have hundreds rather than thousands of digits, it's even more extreme:

$ python -mtimeit -s'x=int("1"*199999); y=int("2"*199999)' 'x*y'
10 loops, best of 3: 675 msec per loop

vs

$ python -mtimeit -s'from gmpy import mpz; x=mpz("1"*199999); y=mpz("2"*199999)' 'x*y'
100 loops, best of 3: 17.8 msec per loop

so, with 200k digits instead of just 10k, gmpy's speed advantage is 38 times or so.

If you routinely need to handle integers of this magnitude, Python + gmpy is really a workable solution (of course I'm biased, since I did author and care for gmpy over the last few years exactly because I ♥ Python (hey, my license plate is P♥thon!-) and in one of my hobby (combinatorial arithmetic) I do have to deal with such numbers pretty often;-).


Mathematica allows you to do such math and you can write complete programs in it.

Otherwise, what you seek is a "library" to extend the built-in functionality of another programming language, such as Python or Java.

In the case of Python, the decimal module enables you to specify a precision in which math operations will be peformed.


Common Lisp has built-in support for arbitrary large numbers as well...


Haskell (when using GHC) also has builtin support for arbitrarily long integers. Here's a snippet showing the length of a number converted to a string.

Prelude> length $ show $ 10
2
Prelude> length $ show $ 1 + 2^2000000
602060
Prelude> let x = 2^200000
Prelude> let y = 2^200000 + 5
Prelude> y - x
5

Or you could just type 2^200000 at the interactive console and wait a couple minutes for it to print out all 600k+ characters. I figured this way was a little simpler to demonstrate.


Perl, Python, Ruby, and Java can all do that. External libraries exist for everything else.

I rather like Ruby and Python because they automatically switch from Fixnum to Bignum. (Python: int to long.)


Perl has a bignum module to do that sort of thing, and Python supports it natively.


What you're looking for isn't necessarily a language, but an arbitrary-precision library.

GMP would be a fast implementation in C/C++, and scripting languages that handles big integers would probably use something like that.


In C or C++, you can use GMP (Gnu Multi-Precision library).

In Perl, you can use the bignum module.


MIT/GNU Scheme has support for arbitrarily large numbers.


Many functional languages natively support arbitrary-precision numbers. Some have already been mentioned here, but I'll repeat them for completeness:

  • Most versions of Haskell.
  • Miranda, a predecessor to Haskell.
  • Some implementations of Scheme. In particular, PLT Scheme.
  • Erlang.


I find Python quite good for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜