# rounding error .n() and N()

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

rounding error .n() and N()

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

add a comment

1

`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
```

1

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

Asked: **
2013-04-08 04:53:32 -0500
**

Seen: **148 times**

Last updated: **Apr 08 '13**

Difference between RealField and numerical_approx

Smallest positive numerical solution of an equation in one variable

Homomorphisms lifted from base ring in PowerSeriesRing do not preserve precision

Changing number of decimal digits displayed in output

'object is not callable' error in sagetex (that doesn't occur in a worksheet)

pi and e not evaluated when i use my own classes

Arbitrary Precision Physics Calculation

Set the precision of imported methods

How to print the numeric part of a symbolic expression with arbitrary precision?

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.