Truncation error

Meghna Karkera mkarkera98 at gmail.com
Sun Oct 11 08:17:14 EDT 2020


May I request you to let me know the steps python follows in order to
compute covariance matrix using the inbuilt
 syntax.

np.cov(cov_mat) .
>>cov_mat = np.stack((x, y), axis = 0)
>>np.cov(cov_mat)

On Sun, Oct 11, 2020 at 9:14 AM Grant Edwards <grant.b.edwards at gmail.com>
wrote:

> On 2020-10-10, Peter J. Holzer <hjp-python at hjp.at> wrote:
> > On 2020-10-07 07:53:55 +0200, Marco Sulla wrote:
> >> If you want to avoid float problems, you can use Decimal:
> >
> > Decimal doesn't avoid floating point problems, because it is a floating
> > point format. For example:
> >     [...]
>
> >     >>> from decimal import *
> >     >>> a = Decimal(3)
> >     >>> a
> >     Decimal('3')
> >     >>> b = Decimal(1E50)
> >     >>> b
> >     Decimal('100000000000000007629769841091887003294964970946560')
> >     [...]
>
> There are two problems with your code:
>
>  1. You meant Decimal('1e50').  What you typed creates a Decimal value
>     from the IEEE 64-bit floating point value closest to 1e50.
>
>  2. You need to increase the context precision.  It defaults to 28,
>     and you're example needs it to be at least 51:
>
> >>> getcontext().prec = 100
> >>> a = Decimal(3)
> >>> b = Decimal('1e50')
> >>> c = Decimal(2)
> >>> a + b - c - b
> Decimal('1')
> >>> b - b + a - c
> Decimal('1')
> >>> a + (b - b) - c
> Decimal('1')
> >>> a + b - b - c
> Decimal('1')
>
> Like other floating point systems, you still need to know what you're
> doing if you want to get the "right" results.
>
> --
> Grant
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list