0
$\begingroup$

I would like to tabulate a list of numbers but Mathematica slightly changes these numbers?!

Here is the list

Hdrz = {-1.19293392628635630 + 
0.333274064763606160 I, -0.89380004035801760 - 
0.36959523498813230 I, -0.77633233787938280 - 
0.08159597927539744 I, -0.75080684622772420 - 
0.62498786768731270 I, -0.68142831803002260 + 
0.72467158753569390 I, -0.67023673404361500 + 
0.23777408481240340 I, -0.49219298410279894 - 
0.87189348958859570 I, -0.43301960306453147 + 
1.16666923455551100 I, -0.09073698351093003 + 
5.81549234664283400 I, -0.05400852949041188 + 
0.89514817303035410 I, 
0.02586935813180266 - 1.00949126186999520 I, 
0.12708577172546760 - 1.15163844397230460 I, 
0.34121878330887984 + 1.15526153099421380 I, 
0.54897412279415590 + 0.52784091826586540 I, 
0.56339918267451470 - 0.21257825928341670 I, 
0.61400656995488830 - 0.77163398642220390 I, 
0.74873548937020160 + 0.81131323943560500 I, 
0.89105134103005410 + 0.21903754849588017 I, 
0.99621116079027920 - 0.29716265051942590 I, 
1.17894452321354700 - 0.49590555492518335 I};

Grid[Table[{NumberForm[Hdrz[[i]], {18, 18}]}, {i, 20}], Frame -> All]

some of the wrong numbers are highlighted

enter image description here

$\endgroup$
  • 2
    $\begingroup$ By default, Mathematica uses 16 decimals of precision. What you're seeing is merely rounding of the numbers. If you need these extra digits, you can do 0.49590555492518335`18 or similar $\endgroup$ – march Jul 6 at 15:51
  • $\begingroup$ Is possible to apply this directly to the whole list? or I should do it for each element by hand? $\endgroup$ – HD2006 Jul 6 at 15:58
  • $\begingroup$ I believe you can use SetPrecision. $\endgroup$ – march Jul 6 at 16:09
  • $\begingroup$ I tried it with SetAccuracy but the same problem is maintained $\endgroup$ – HD2006 Jul 6 at 18:40
  • $\begingroup$ Your problem exemplifies that it's a bad idea to transport real numbers from one application to another by printing them in decimal form and then re-parsing them on the other side. Much better to use a format that stays in IEEE 754, for example HDF5, or even a simple binary dump of the numbers to a file. $\endgroup$ – Roman Jul 7 at 6:16
1
$\begingroup$

According to the answers here SetPrecision and setting precision with a backtick SetPrecision[] and single back-tick are not the same, nor are SetAccuracy[] and double back-ticks after a number.

Similarly, you can't use Rationalize or most other functions I've tried because the numbers are interpreted as machine precision unless they have back-ticks after them or are obviously exact numbers.

It may not be the most elegant, but I think you can get around this by interpreting them as strings, removing the decimal place, dividing them back down to their actual values in exact arithmetic, and then setting the accuracy or precision there. The exact details may vary depending on whether all of your numbers look like the ones provided above.

hdrzString = "-1.19293392628635630+0.333274064763606160 \
I,-0.89380004035801760-0.36959523498813230 \
I,-0.77633233787938280-0.08159597927539744 \
I,-0.75080684622772420-0.62498786768731270 \
I,-0.68142831803002260+0.72467158753569390 \
I,-0.67023673404361500+0.23777408481240340 \
I,-0.49219298410279894-0.87189348958859570 \
I,-0.43301960306453147+1.16666923455551100 \
I,-0.09073698351093003+5.81549234664283400 \
I,-0.05400852949041188+0.89514817303035410 \
I,0.02586935813180266-1.00949126186999520 \
I,0.12708577172546760-1.15163844397230460 \
I,0.34121878330887984+1.15526153099421380 \
I,0.54897412279415590+0.52784091826586540 \
I,0.56339918267451470-0.21257825928341670 \
I,0.61400656995488830-0.77163398642220390 \
I,0.74873548937020160+0.81131323943560500 \
I,0.89105134103005410+0.21903754849588017 \
I,0.99621116079027920-0.29716265051942590 \
I,1.17894452321354700-0.49590555492518335 I"

hdrzExact = 
  SetAccuracy[
    ToExpression[StringSplit[StringReplace[hdrzString, "." -> ""], ","]]/
      10^17, 20]
Column[hdrzExact, Frame -> All]

Table of numbers with added precision/accuracy.

Of course, if you'd prefer, you can change the accuracy to any value you like or you can remove SetAccuracy[..., 20] and work with them as exact numbers.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.