Upon converting SymPy expressions to NumPy longdoubles (code snippet below), I noticed that the last three digits in the NumPy results are typically rubbish. I don't understand why this happens, as I'm explicitly indicating that the NumPy results should be of dtype=np.longdouble. What am I doing wrong, and is there a (straightforward) way to fix it?
There doesn't seem to be a single mention of SymPy in NumPy's documentation, so I'm not sure what exactly NumPy's array constructor does when given SymPy input.
from sympy import *
import numpy as np
print(np.finfo(np.longdouble))
symMat = Matrix([[sqrt(5), sqrt(7)]])
print(symMat)
print(N(symMat, 19))
np.set_printoptions(precision = 18)
numMat = np.array(symMat, dtype=np.longdouble)
print("", repr(numMat))
numMat = np.array(N(symMat, 19), dtype=np.longdouble)
print("", repr(numMat))
This produces the following output:
Machine parameters for float128
---------------------------------------------------------------
precision = 18 resolution = 1e-18
machep = -63 eps = 1.084202172485504434e-19
negep = -64 epsneg = 5.42101086242752217e-20
minexp = -16382 tiny = 3.3621031431120935063e-4932
maxexp = 16384 max = 1.189731495357231765e+4932
nexp = 15 min = -max
---------------------------------------------------------------
Matrix([[sqrt(5), sqrt(7)]])
Matrix([[2.236067977499789696, 2.645751311064590591]])
array([[2.236067977499789805, 2.645751311064590716]], dtype=float128)
array([[2.236067977499789805, 2.645751311064590716]], dtype=float128)