# Converting string into number does not work properly Here is an example with huge integer number "n".

n=2^7000000;

Sage can convert huge integer number into string very quickly:

s=str(n);

But the other way, i.e. the same huge string converting back into integer number is very slow:

m=int(s);

It should be done in approximately same time as variables "m", "n", "s" are of the same length. I also tried to use eval(s) and sage_eval(s) instead of int(s) and all are very slow.

Mathematica can do all the above operation much more quickly than Sage.

Do you know, where the problem is? Is there some other function besides "int", "eval" and "sage_eval" that can do it faster?

edit retag close merge delete

Sort by » oldest newest most voted In your example, if you check the type of n, you can see it is of type Integer. So maybe Integer(s) can do the job faster.

Edit: Changed from Integer(s,10). Thanks to @kcrisman for pointing this out.

more

1

Definitely do NOT use int; that will give you a Python integer, which has vastly smaller capabilities than a Sage Integer. In addition to being monstrously slow, if even possible for such a large number without causing errors (arithmetic or other).

1

Also, Integer(s,10) would be longhand for Integer(s) in this case, since the string we get from 2^70.. is already in base 10, I think.

I'm seeing int(s) take a long time, but Integer(s)is relatively quick: http://aleph.sagemath.org/?q=3ddbbfe6...

On my computer, Sage's Integer(s) talks about half the time that Mathematica's ToExpression[s] takes.

more

Thank you all for your helping replies. But what is the purpose of function "int" when there is a better function "Integer"? Then the code of "int" should be rewritten according to the code of "Integer".

I had to manipulate extremely large integer numbers read from a file as strings (2^7000000 is tiny compared to those numbers). Then I need to convert them into numbers to be able to do some computations with them. With "int" it was impossible.

more

2

Sage has at its base the Python programming language, which contains its own native types. Removing those would be impractical for many reasons (among them invalidating lots of Python programs and modules). Basically, int and Integer are different kinds of thing: one is a basic Python type and the other is a Sage-specific type with lots of additional functionality which lives in the Sage universe.

1

When you're writing Sage programs, you should never have to use int. The only time you may have to use it is if you're interfacing with a Python program.