Ask Your Question
0

is it a bug?

asked 2013-05-09 07:54:14 +0200

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

the output of the following lines

n=log(64.0)/log(2.0)
print n, range(n)

n=log(N(64))/log(N(2))
print n, range(n)

n=N(log(64)/log(2))
print n, range(n)

is

6.00000000000000 [0, 1, 2, 3, 4, 5]
6.00000000000000 [0, 1, 2, 3, 4, 5]
6.00000000000000 [0, 1, 2, 3, 4]

shouldn't they be all the same?

('Sage Version 5.9, Release Date: 2013-04-30', osx 10.6.8)

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2013-05-09 08:23:51 +0200

tmonteil gravatar image

updated 2013-05-09 08:37:41 +0200

First, when you read 6.00000000000000, you should understand that this not the value of the number (which is a floating point number having 53 bits of precision), but a string representation of it, as a decimal expansion (not binary). In your third case, the value of N(log(64)/log(2)) is less that 6 (though very close, hence the decimal representation), as you can check by typing:

sage: a = N(log(64)/log(2))
sage: a < 6 
True
sage: a.exact_rational()
6755399441055743/1125899906842624
sage: a.sign_mantissa_exponent()
(1, 6755399441055743, -50)

In the first two cases, you can check that the first two values are equal to 6.

Now, where could this difference come from ? The first two values are the same since N(64) is just the conversion of the integer 64 into an element of RealField(53), which is the same as 64.0. What about the third ? log(64)/log(2) is not a floating point number but an element of the Symbolic Ring, as you can check by typing:

sage: (log(64)/log(2)).parent()     
Symbolic Ring

Now, when you type N(log(64)/log(2)), you ask Sage to convert this element of the Symbolic Ring to an element of RealField(53). Hence, it seems that the rounding from the Symbolic Ring to RealField(53) is not done to the nearest floating point number, but toward zero:

sage: N(log(64)/log(2)) < 6 
True
sage: N(-log(64)/log(2)) > -6
True

It looks like to be a similar problem that the one that appeared for the conversion from the Rational Field to the Real Double Field, see trac ticket 14416 and ask question 2444.

What is weird is that the Symbolic Ring is able to understand that $64 = 2^6$ and find the right result by itself:

sage: a = log(64)/log(2)
sage: a.full_simplify()
6
sage: N((log(64)/log(2)).full_simplify()) == 6.0
True

The main advice i could do is to avoid the use of Symbolic Ring as much as possible (see for example this problem with powers of complex numbers)

edit flag offensive delete link more

Comments

I do not understand your last comment: If it is a problem with the conversion to the real field, then why is it wired that the symbolic ring works well?

abc gravatar imageabc ( 2013-05-09 09:18:10 +0200 )edit

When you write N(log(64)/log(2)), actually you call the method `.n()` of the object `log(64)/log(2)`, which is an element of the `Symbolic Ring`. sage: N?? sage: a = log(64)/log(2) sage: a.n() == N(a) True Hence the conversion problem should be attributed to the class `sage.symbolic.expression.Expression`. However, this class also has a method `.full_simplify()`, which does some formal simplifications (here it understands that `64=2^6` and `log(a^b) = b.log(a)`, and, when it is called before `.n()`, it alows to find the correct rounding: sage: (log(64)/log(2)).n() == 6 False sage: (log(64)/log(2)).full_simplify().n() == 6 True which is a kind of inconsistency.

tmonteil gravatar imagetmonteil ( 2013-05-09 09:47:06 +0200 )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: 2013-05-09 07:54:14 +0200

Seen: 701 times

Last updated: May 09 '13