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

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

Sort by » oldest newest most voted

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

more

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

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

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

( 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

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

( 2012-01-20 15:21:52 +0100 )edit

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

more

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

( 2012-01-18 11:01:21 +0100 )edit

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

( 2012-02-13 14:36:28 +0100 )edit