axkr / symja_android_library

:coffee: Symja - computer algebra language & symbolic math library. A collection of popular algorithms implemented in pure Java.
https://matheclipse.org/
GNU General Public License v3.0
384 stars 86 forks source link

`Rationalize(878159.58,1*10^-12) - Rationalize(431874.32,1*10^-12) // N` returns wrong result #1065

Open tranleduy2000 opened 2 months ago

tranleduy2000 commented 2 months ago

There is the input:

Rationalize(878159.58,1*10^-12) - Rationalize(431874.32,1*10^-12) // N

That gives wrong result:

𝟦𝟦𝟨𝟤𝟪𝟧.𝟥
image image

Expected result:

446285.25999999995
tranleduy2000 commented 2 months ago

There is too much precision loss for the following input:

m = {{-2,-2,4},
{-1,-3,7},
{2,4,6}};

s= Transpose[Eigenvectors[m]];

j = {{1+Sqrt[61],0,0},{0,1-Sqrt[61],0},{0,0,-1}};

s1 = Inverse[s];

s.j.s1 // N
image

The result should be

{{-2.0,-2.0,4.0},
{-1.0,-3.0,7.0},
{2.0,4.0,6.0}}
axkr commented 2 months ago

That gives wrong result:

𝟦𝟦𝟨𝟤𝟪𝟧.𝟥

It's only the "output format" if you get the result as "input format"

Rationalize(878159.58,1*10^-12) - Rationalize(431874.32,1*10^-12) // N //InputForm

it returns 446285.26

axkr commented 2 months ago

s.j.s1 // N

You can get a better result, if you increase the precision:

N[s.j.s1 ,30]

A simplification before the numeric calculation may also get a better result.

s.j.s1 // FullSimplify // N
axkr commented 1 month ago

The result should be

{{-2.0,-2.0,4.0},
{-1.0,-3.0,7.0},
{2.0,4.0,6.0}}

See test case testNIssue1065()

with Config.USE_EXTENDED_PRECISION_IN_N = true; you can enable using apfloat for calculating in better precision.

axkr commented 1 month ago

with Config.USE_EXTENDED_PRECISION_IN_N = true; you can enable using apfloat for calculating in better precision.

Note: Because of using Simplify for evaluating the Dot(...) function:

it's not necessary anymore to use Config.USE_EXTENDED_PRECISION_IN_N = true at least for this Eigenvectors example.