ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 06 Jun 2012 12:44:02 -0500Converting string into number does not work properlyhttp://ask.sagemath.org/question/9034/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?Wed, 06 Jun 2012 01:02:20 -0500http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/Answer by fidbc for <p>Here is an example with huge integer number "n".</p>
<p>n=2^7000000;</p>
<p>Sage can convert huge integer number into string very quickly:</p>
<p>s=str(n);</p>
<p>But the other way, i.e. the same huge string converting back into integer number is very slow:</p>
<p>m=int(s);</p>
<p>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.</p>
<p>Mathematica can do all the above operation much more quickly than Sage.</p>
<p>Do you know, where the problem is?
Is there some other function besides "int", "eval" and "sage_eval" that can do it faster?</p>
http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?answer=13657#post-id-13657In 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.Wed, 06 Jun 2012 03:20:55 -0500http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?answer=13657#post-id-13657Comment by kcrisman for <p>In your example, if you check the type of <code>n</code>, you can see it is of type <code>Integer</code>.
So maybe <code>Integer(s)</code> can do the job faster.</p>
<p>Edit: Changed from <code>Integer(s,10)</code>. Thanks to <a href="/users/41/kcrisman/">@kcrisman</a> for pointing this out.</p>
http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?comment=19681#post-id-19681Also, `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.Wed, 06 Jun 2012 03:37:03 -0500http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?comment=19681#post-id-19681Comment by kcrisman for <p>In your example, if you check the type of <code>n</code>, you can see it is of type <code>Integer</code>.
So maybe <code>Integer(s)</code> can do the job faster.</p>
<p>Edit: Changed from <code>Integer(s,10)</code>. Thanks to <a href="/users/41/kcrisman/">@kcrisman</a> for pointing this out.</p>
http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?comment=19682#post-id-19682Definitely 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).Wed, 06 Jun 2012 03:33:12 -0500http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?comment=19682#post-id-19682Answer by azerbajdzan for <p>Here is an example with huge integer number "n".</p>
<p>n=2^7000000;</p>
<p>Sage can convert huge integer number into string very quickly:</p>
<p>s=str(n);</p>
<p>But the other way, i.e. the same huge string converting back into integer number is very slow:</p>
<p>m=int(s);</p>
<p>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.</p>
<p>Mathematica can do all the above operation much more quickly than Sage.</p>
<p>Do you know, where the problem is?
Is there some other function besides "int", "eval" and "sage_eval" that can do it faster?</p>
http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?answer=13660#post-id-13660Thank 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.Wed, 06 Jun 2012 10:28:51 -0500http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?answer=13660#post-id-13660Comment by DSM for <p>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".</p>
<p>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.</p>
http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?comment=19680#post-id-19680Sage 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.Wed, 06 Jun 2012 10:57:42 -0500http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?comment=19680#post-id-19680Comment by Eviatar Bach for <p>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".</p>
<p>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.</p>
http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?comment=19679#post-id-19679When 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.Wed, 06 Jun 2012 12:44:02 -0500http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?comment=19679#post-id-19679Answer by Jason Grout for <p>Here is an example with huge integer number "n".</p>
<p>n=2^7000000;</p>
<p>Sage can convert huge integer number into string very quickly:</p>
<p>s=str(n);</p>
<p>But the other way, i.e. the same huge string converting back into integer number is very slow:</p>
<p>m=int(s);</p>
<p>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.</p>
<p>Mathematica can do all the above operation much more quickly than Sage.</p>
<p>Do you know, where the problem is?
Is there some other function besides "int", "eval" and "sage_eval" that can do it faster?</p>
http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?answer=13658#post-id-13658I'm seeing `int(s)` take a long time, but `Integer(s)` is relatively quick: http://aleph.sagemath.org/?q=3ddbbfe6-0640-4700-bef4-1ead8214737c
On my computer, Sage's `Integer(s)` talks about half the time that Mathematica's `ToExpression[s]` takes.Wed, 06 Jun 2012 03:33:32 -0500http://ask.sagemath.org/question/9034/converting-string-into-number-does-not-work-properly/?answer=13658#post-id-13658