Skip to main content
6 of 6
deleted 4 characters in body
andre314
  • 19k
  • 1
  • 37
  • 73

Copy-paste with annoying trailing 0s

When one copies an output containing machine-precision numbers to a input, many trailing 0s appears.

For example :

Evaluate the following expression :

{{41.`,19.200000000000003`},
 {41.`,39.5`},
 {33.6`,49.300000000000004`},
 {33.2`,59.800000000000004`}, 
 {33.2`,61.800000000000004`}}

{{41., 19.2}, {41., 39.5}, {33.6, 49.3}, {33.2, 59.8}, {33.2, 61.8}}

Then copy-paste this output. The result is a input cell with again all the trailing 0s.

I need to Round the values so that the input form stays short (It is not a problem of number formatting/rendering since the numerical values can be changed a little bit in this operation).

Tests already done, without success :

First, a preamble that shows that this is possible

niceValue=49.3
niceValue //InputForm  

49.3
49.3

Then, a example of a difficult case :

irritatingValue=49.300000000000004`  
irritatingValue //InputForm  

49.3
49.300000000000004

Unsuccessfull attempts :

This seems to work :

Round[irritatingValue,1. 10^-6] //InputForm

49.3

But this does not :

Round[irritatingValue,1. 10^-5] //InputForm    

49.300000000000004

so the "solution" Round[..., 1. 10^-n] is inacceptable. Because the problem is certainly due to the binary representation of numbers that we see ultimately in base 10, I have tried also things like : Round[..., 256 $MachineEpsilon] and Round[..., 2^-10] , without success.

Chop[...] doesn't seem neither to bring a solution.

andre314
  • 19k
  • 1
  • 37
  • 73