Consider the function
f[a_] := NIntegrate[Sqrt[a + Log[x]], {x, 1, 10}, WorkingPrecision -> 30]
.
With this definition, I cannot call f[0.1]
(for example) without Mathematica throwing an error, since "0.1" is interpreted automatically as a machine number with MachinePrecision
(about 16), whereas the integration requires 30 digits of precision.
But when I enter 0.1
, of course I really mean 1/10
. One solution is to enter f[1/10]
, but this is very inconvenient to do every time, especially because my real functions involve many parameters with long strings of decimal numbers. What's the best way around this issue? Ideally, I'd like Mathematica to interpret 0.1
as 1/10
automatically, as mentioned in the title.
EDIT
To clarify, I would like any decimal number input be treated as the exact mathematical number with the same name. For example, 0.###
should become ###/1000
. As pointed out in the comments, Rationalize
converts 0.33333333
to 1/3
, which I would consider a problem for my case. I would like Mathematica to treat decimal number input as exact numbers without rounding to any finite precision, nor assuming that I intended a "close" but different rational number.
Another option would be to change the function definition:
f[a_] := NIntegrate[Sqrt[Rationalize[a] + Log[x]], {x, 1, 10}, WorkingPrecision -> 30]
or
f[a_] := NIntegrate[Sqrt[SetPrecision[a, \[Infinity]] + Log[x]], {x, 1, 10}, WorkingPrecision -> 30]
But this too would be quite inconvenient to do for every function I use. I also worry about the fact that SetPrecision[0.1, \[Infinity]]
does not give 1/10
. So all this makes me wonder what the "right" method is.
$PreRead
to replace"0.1"
withRowBox[{"1", "/", "10"}]
, I guess. For my money, I would personally go with entering1/10
. Your comment about it suggests you might not just be worried about0.1
but other decimal numbers. It's unclear to me which ones I should worry about, though. Perhaps instead of$PreRead$
, you could use$Pre
to replace real numbers by their rationalizations. $\endgroup$0.1
as an example, but in reality there are many arbitrary decimal values I may want to enter. $\endgroup$0.###
would be converted to###/1000
. $\endgroup$0.3333333333333
should not become1/3
as happens when you feed it toRationalize
? (Maybe this should be clarified in the question, and not just left in the comments.) $\endgroup$