0
$\begingroup$

I have an expression with lots of numbers such as

1.000000002, 2.0000000000001, ...

and I would like to automatically set them to

1, 2, ...

how can I do that?

$\endgroup$
7
  • 2
    $\begingroup$ Round them to the nearest integer $\endgroup$
    – Roman
    Commented Feb 28, 2019 at 10:19
  • 4
    $\begingroup$ or something likeChop[1.000000002, 10^(-5)] $\endgroup$
    – user59583
    Commented Feb 28, 2019 at 10:55
  • 1
    $\begingroup$ If all your numbers in that form, then use Floor[]? $\endgroup$ Commented Feb 28, 2019 at 13:32
  • 1
    $\begingroup$ All of these commands like Round, Floor, etc. have an optional argument that lets you specify their "granularity". For example, Round[x, 1/1000] rounds to the nearest thousandth. Your own solution using Rationalize has this parameter too and is even more flexible. $\endgroup$
    – Roman
    Commented Feb 28, 2019 at 16:06
  • 1
    $\begingroup$ I don't understand why the suggestion of @Buddha_the_Scientist has 3 upvotes. -- It does nothing; the output is 1.000000002 (if you look at the FullForm). $\endgroup$
    – Michael E2
    Commented Mar 1, 2019 at 0:50

1 Answer 1

1
$\begingroup$

A friend of mine gave me a nice answer and I will post it here for completeness.

Use "Rationalize".

$\endgroup$
2
  • 5
    $\begingroup$ Simple Rationalize /@ {1.000000002, 2.0000000000001} doesn't work, you'll have to specify a "catching range": something like Rationalize[#, 10^-8] & /@ {1.000000002, 2.0000000000001}. $\endgroup$
    – Roman
    Commented Feb 28, 2019 at 16:04
  • 1
    $\begingroup$ This would have been a good answer if you had included an example of how Rationalize[] solves your problem. $\endgroup$ Commented Mar 1, 2019 at 9:18

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