caret interpreted as bitwise operator not power

50 views
Skip to first unread message

robin hankin

unread,
May 26, 2014, 6:29:29 PM5/26/14
to sage-s...@googlegroups.com
Hi, debian linux, Sage-6.2.

 It took me some time to track down a problem in my code that boiled down to the interpretation of a caret ("^") in a file which was read using load() in a sage session.

Specifically, I have a file called "f.py" which contains a single line, "o = 10^6".  I load() this file, expecting o to be 1000000, but instead it takes the value 12.  This is , I think, the result of a bitwise xor operation.  But it is not what I wanted. 


sage: !cat f.py
o = 10^6
sage: load("f.py")
sage: o
12
sage:


Note that in interactive mode, the caret is interpreted as expected:

sage: o = 10^6
sage: o
1000000
sage:



Is this a bug?  Can anyone advise?  I am very very very reluctant to adopt the ghastly "**" notation.

best wishes

Robin





--
Robin Hankin
Neutral theorist
hankin...@gmail.com

leif

unread,
May 26, 2014, 7:39:32 PM5/26/14
to sage-s...@googlegroups.com
robin hankin wrote:
> Hi, debian linux, Sage-6.2.
>
> It took me some time to track down a problem in my code that boiled
> down to the interpretation of a caret ("^") in a file which was read
> using load() in a sage session.
>
> Specifically, I have a file called "f.py" which contains a single line,
> "o = 10^6". I load() this file, expecting o to be 1000000, but instead
> it takes the value 12. This is , I think, the result of a bitwise xor
> operation. But it is not what I wanted.
>
>
> sage: !cat f.py
> o = 10^6
> sage: load("f.py")
> sage: o
> 12
> sage:
>
>
> Note that in interactive mode, the caret is interpreted as expected:
>
> sage: o = 10^6
> sage: o
> 1000000
> sage:
>
>
>
> Is this a bug?

No. '^' means bitwise exclusive-or in Python, where exponentiation is
(just) '**'.


> Can anyone advise? I am very very very reluctant to
> adopt the ghastly "**" notation.

If you want your file to get preparsed (such that the Sage preparser
replaces '^' by '**' "internally" when you load or run the file with
'sage'), simply rename your file to "f.sage".

Similar holds for '.pyx' vs. '.spyx'; the latter first gets preparsed.


-leif

--
() The ASCII Ribbon Campaign
/\ Help Cure HTML E-Mail

robin

unread,
May 26, 2014, 7:47:23 PM5/26/14
to sage-s...@googlegroups.com

> Can anyone advise?  I am very very very reluctant to
> adopt the ghastly "**" notation.

If you want your file to get preparsed (such that the Sage preparser
replaces '^' by '**' "internally" when you load or run the file with
'sage'), simply rename your file to "f.sage".

Similar holds for '.pyx' vs. '.spyx'; the latter first gets preparsed.



thanks for this Leif.  Where do I look for documentation of this issue?  The help page for load() discusses file extensions but does not mention  preparsing.


best wishes

Robin

leif

unread,
May 26, 2014, 8:41:23 PM5/26/14
to sage-s...@googlegroups.com
See for example

http://sagemath.org/doc/tutorial/programming.html#loading-and-attaching-sage-files

or type 'preparse?<RETURN>' at the 'sage:' prompt.

The documentation for 'load' (type 'load?') also refers to 'attach',
whose documentation is more explicit regarding filename extensions.

robin

unread,
May 26, 2014, 9:04:04 PM5/26/14
to sage-s...@googlegroups.com
brilliant, thanks Leif.

It'd be nice to have an explicit "Note:  " on the documentation for load() which states this extension-dependent behaviour for newbies like me.  In my world (mostly R) the file extension is immaterial, and I'm sure this issue is a gotcha for many people like me.

best wishes [marked 'resolved']

Robin

John H Palmieri

unread,
May 26, 2014, 9:49:03 PM5/26/14
to sage-s...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages