Exception in floor(tanh(91))

54 views
Skip to first unread message

Georgi Guninski

unread,
Feb 9, 2024, 10:48:41 AM2/9/24
to sage-...@googlegroups.com
hi,

floor(tanh(91))

ValueError: cannot compute floor(sinh(91)/cosh(91)) using 256 bits of precision

gp('floor(tanh(91))')
1

dmo...@deductivepress.ca

unread,
Feb 9, 2024, 11:04:47 AM2/9/24
to sage-devel
The ValueError is correct -- the quantity is so close to 1 that numerics cannot tell whether the floor is 0 or 1.  You could report a bug to gp, though, because the correct answer is 0, not 1.

Georgi Guninski

unread,
Feb 9, 2024, 11:16:03 AM2/9/24
to sage-...@googlegroups.com
When I try to extract digits of tanh(91) via floor, I still get error

sage: floor(10^4*tanh(91))
ValueError

sage: gp('floor(10^4*tanh(91))')
10000


gp computes it both ways :)

sage: gp.default('realprecision',10^5)
0
sage: gp('floor(tanh(91))')
0

Vincent Delecroix

unread,
Feb 9, 2024, 1:44:00 PM2/9/24
to sage-...@googlegroups.com
The following is the proper way to extract digits

sage: tanh(91).numerical_approx(digits=10)
1.000000000
sage: tanh(91).numerical_approx(digits=100)
0.9999999999999999999999999999999999999999999999999999999999999999999999999999998182667935304138503930
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/CAGUWgD9_cAc%2B_WCMh%3D2hCuvMy2GO-tgcNnrARh2q7K%3D8CErrfQ%40mail.gmail.com.

Nils Bruin

unread,
Feb 9, 2024, 5:40:09 PM2/9/24
to sage-devel
On Friday 9 February 2024 at 08:16:03 UTC-8 Georgi Guninski wrote:
When I try to extract digits of tanh(91) via floor, I still get error

sage: floor(10^4*tanh(91))
ValueError

The sage code doesn't use gp for this. The error you're encountering happens in

sage.functions.other._eval_floor_ceil

and it happens because the system tries to evaluate the expression using interval arithmetic. It iteratively increases the precision until it find an interval that doesn't straddle an integer or it gives up after a set number of trials. You're hitting that second condition. You don't make it easier by multiplying by 10^4. In fact, you make it harder: you now need more digits to get the interval to not include an integer.

Without more information, this code basically has to give up after a fixed number of tries: it could be evaluating an expression that actually does evaluate to 1. In that case, no amount of increased precision will result in an interval that doesn't contain an integer. It's a well-known problem in numerical approximation: discontinuous functions are basically not computable in that model.
 
gp computes it both ways :)

sage: gp.default('realprecision',10^5)
0
sage: gp('floor(tanh(91))')
0

so does sage if you tell it to use the same approach gp uses:

 sage: tanh(91).n(30).floor()
1
sage: tanh(91).n(300).floor()
0
Reply all
Reply to author
Forward
0 new messages