First time here? Check out the FAQ!

Ask Your Question
1

rounding error .n() and N()

asked 11 years ago

kjhudson gravatar image

updated 11 years ago

Why does 1.414 - sqrt(2).n(digits=4) not evaluate to zero?

sage: sqrt(2).n(digits=4)

1.414

sage: 1.414 - sqrt(2).n(digits=4)

-0.0002136

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 11 years ago

vdelecroix gravatar image

Hi,

This is because the two numbers do not live in the same ground field as you can see

sage: 1.414.parent()
Real Field with 53 bits of precision
sage: sqrt(2).n(digits=4).parent()
Real Field with 17 bits of precision

1.414 is not exactly represented in memory because it is not a diadic number. Two different diadic approximations with different precisions are different.

Vincent

Preview: (hide)
link
1

answered 11 years ago

sqrt(2).n(digits=4) returns a number which agrees with sort(2) to 4 digits of precision, but it need not be obtained by truncating sqrt(2) after 4 digits. I'm guessing that it's obtained by truncating sqrt(2) after the necessary number of binary digits:

sage: a = sqrt(2).n(digits=4)
sage: a.n(digits=30)
1.41421508789062500000000000000

sage: b = sqrt(2).n(16)
sage: b.n(digits=30)
1.41421508789062500000000000000
sage: a == b
True
Preview: (hide)
link

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: 11 years ago

Seen: 535 times

Last updated: Apr 08 '13