Ask Your Question
0

Converting string into number does not work properly

asked 2012-06-06 01:02:20 -0500

azerbajdzan gravatar image

updated 2015-01-13 14:25:55 -0500

FrédéricC gravatar image

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 flag offensive close merge delete

3 answers

Sort by » oldest newest most voted
6

answered 2012-06-06 03:20:55 -0500

fidbc gravatar image

updated 2012-06-06 04:32:44 -0500

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.

edit flag offensive delete link more

Comments

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).

kcrisman gravatar imagekcrisman ( 2012-06-06 03:33:12 -0500 )edit
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.

kcrisman gravatar imagekcrisman ( 2012-06-06 03:37:03 -0500 )edit
2

answered 2012-06-06 03:33:32 -0500

Jason Grout gravatar image

updated 2012-06-06 03:41:48 -0500

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.

edit flag offensive delete link more
0

answered 2012-06-06 10:28:51 -0500

azerbajdzan gravatar image

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.

edit flag offensive delete link more

Comments

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.

DSM gravatar imageDSM ( 2012-06-06 10:57:42 -0500 )edit
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.

Eviatar Bach gravatar imageEviatar Bach ( 2012-06-06 12:44:02 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-06-06 01:02:20 -0500

Seen: 2,397 times

Last updated: Jun 06 '12