int() on real numbers broken?

23 views
Skip to first unread message

Zeda Thomas

unread,
Mar 24, 2014, 7:28:27 AM3/24/14
to sage-s...@googlegroups.com
I am working on a Z80 project and I needed 72 bits of precision for 64 elements of the form log2(1+2^-i) (so log2(3/2), log2(5/4),...). I needed to convert these to hexadecimal, and it worked until I tried the following for i=56:
int(256*log(1+2^-56,2))

This returns 1, when in fact it should be 0. Actually, instead of multiplying by 256, multiplying by 65536, or 600, or many other numbers would also return the integer part as 1.

As a note, I used RealField(80) as my precision.

Peter Bruin

unread,
Mar 24, 2014, 10:49:18 AM3/24/14
to sage-s...@googlegroups.com, Zeda Thomas
Hello,
Typing log(1 + 2^-56, 2) gives the result as a symbolic expression, not
as an element of a real field. Applying int() to this internally uses a
RealIntervalField with 53 bits of precision, which is not enough in this
case. Here is a way to get the desired precision:

sage: x = RealField(80)(256*log(1+2^-56,2))
sage: x
5.1254824061038682620123e-15
sage: int(x)
0

Peter

Zeda Thomas

unread,
Mar 25, 2014, 7:49:52 AM3/25/14
to sage-s...@googlegroups.com, Zeda Thomas, p.b...@warwick.ac.uk
Thanks much, that helps and I am pretty sure I understand what is going on! 
Reply all
Reply to author
Forward
0 new messages