When I enter
f + 0 where
f is an undefined symbol, I get
f. So far so good. When I enter
f + 0., I get
f + 0.. How can I get Mathematica to simplify 0. just like it does for 0? I tried using
Simplify, I got the same result.
Another reason why
0. is not removed is that it is an inexact zero. In other words, it represents something that is zero to a certain precision, but unknown beyond that.
The usual way to convert very small inexact numbers to an exact zero is
Chop. An exact zero will be removed immediately.
Chop[f + 0.] (* f *)
I do not recommend using a replacement rule to remove it. Why? It's because replacement rules use patterns, which will match only on structurally identical expressions (not mathematically equal ones). There are several structurally different expressions which look like zero and are effectively zero. Yet a simple pattern will only match one of them. Try e.g.
f + 0.``20, or a "no precision left" zero arising from catastrophic cancellation.
From the doc :
Chop[expr,delta] replaces numbers smaller in absolute magnitude than delta by 0.
Chop uses a default tolerance of 10^-10.
Chop[f + 1. 10^-20]
You might want very small values not to canceled. In that case you can use the two arguments syntax of
Chop[f + 1. 10^-20, 10^-30]
1.*10^-20 + f
But if you have no idea of the magnitude of what is negligible or not, it seems that you can use
Rationalize[..., 0] :
Rationalize[f + 0., 0]
Rationalize[f + 1. 10^-20, 0]
1/100000000000000005484 + f
and then return to classic machine-precision number :
N[Rationalize[f + 1. 10^-20, 0]]
1.*10^-20 + f