Ask Your Question

Is n() the same as N() if not why not

asked 2012-01-17 11:29:55 -0600

russ_hensel gravatar image

updated 2012-01-17 12:41:33 -0600

I am trying to use Sage as a simple numerical calculator but sometimes have to fight with more advanced features, things I think are number are not. I starting using n() to fix this up, but in some cases it did not work. Bug big N N() did. Is this the way it is supposed to be ( if so why ). Identical commands ( symbols..... ) except for capitilization seems like a bad idea. Here is the code:

# Is n() the same as N() if not why not

xll = 1. / ( pi * ( 2. ^ .5 )   ) 

xll = 22. * 2

print "xll is of type", type( xll )
print ""

print "Big N next" 
print N( ll )

print "" 
print "Little n next" 
print n( xll )

produced output:

xll is of type <type 'sage.rings.real_mpfr.RealNumber'>

Big N next 

Little N next 
Traceback (click to the left of this block for traceback) ...       
TypeError: 'sage.rings.real_mpfr.RealLiteral' object is not callable
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-01-17 11:50:24 -0600

DSM gravatar image

updated 2012-01-17 14:30:05 -0600

n() is N() -- they're different names for exactly the same function, so they'll give exactly the same answers.. at least if n and N are still what they started as.

Could you cut and paste the output that shows they're different? Usually when something like this happens, either (1) a function has been replaced, say because someone typed "n = something_or_other", or (2) there's a simple typo.

(BTW, your code confuses me a little-- you assign to xll twice but print N(ll), where ll is never defined.)

UPDATE: Thanks for posting the traceback. [Although it's not the output from the code you posted -- the capitalization in 'Little n next' is different between the two, so you must've changed something.]

The traceback makes it clear that my guess #1 was right: you've accidentally set n, little n, to a real number. Type "print n" to see what the number is. The error message is saying that a RealLiteral can't be called (which makes sense, it's not really a function.)

For example:

sage: n(pi)
sage: n = 3.45 
sage: n(pi)
TypeError                                 Traceback (most recent call last)
/Applications/sage/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in <module>()
TypeError: 'sage.rings.real_mpfr.RealLiteral' object is not callable
edit flag offensive delete link more


two versions of xll to see if some detail of calc was problem

russ_hensel gravatar imageruss_hensel ( 2012-01-17 11:57:47 -0600 )edit

My formatting is messed up, is there a code tag -- I will look

russ_hensel gravatar imageruss_hensel ( 2012-01-17 12:29:44 -0600 )edit

This was exactly right, redefinition of n() --- I will look out for this sort of thing in the future, thanks

russ_hensel gravatar imageruss_hensel ( 2012-01-20 07:58:45 -0600 )edit

@russ_hensel: Great! You can accept the answer by clicking the checkmark (on the top left hand side of this answer.)

DSM gravatar imageDSM ( 2012-01-20 08:21:52 -0600 )edit

answered 2012-01-17 19:11:57 -0600

As far as the style is concerned, when you want to print the values of some variables for debug purposes, instead of

print "Value of a next"
print a

you can write

print "Value of a: ",a

It's easier to read (my opinion).

edit flag offensive delete link more


Or: print "Value of a: %s" % a

John Palmieri gravatar imageJohn Palmieri ( 2012-01-18 04:01:21 -0600 )edit

Right, is there a similar thing you can do with show()?

russ_hensel gravatar imageruss_hensel ( 2012-02-13 07:36:28 -0600 )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


Asked: 2012-01-17 11:29:55 -0600

Seen: 433 times

Last updated: Jan 17 '12