Ask Your Question
0

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

asked 2012-01-17 18:29:55 +0100

russ_hensel gravatar image

updated 2012-01-17 19:41:33 +0100

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 
0.0000120042175487614

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
1

answered 2012-01-17 18:50:24 +0100

DSM gravatar image

updated 2012-01-17 21:30:05 +0100

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

Comments

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

russ_hensel gravatar imageruss_hensel ( 2012-01-17 18:57:47 +0100 )edit

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

russ_hensel gravatar imageruss_hensel ( 2012-01-17 19:29:44 +0100 )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 14:58:45 +0100 )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 15:21:52 +0100 )edit
0

answered 2012-01-18 02:11:57 +0100

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

Comments

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

John Palmieri gravatar imageJohn Palmieri ( 2012-01-18 11:01:21 +0100 )edit

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

russ_hensel gravatar imageruss_hensel ( 2012-02-13 14:36:28 +0100 )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-01-17 18:29:55 +0100

Seen: 1,624 times

Last updated: Jan 18 '12