Is there a simple way to enter decimal numbers such as 0.123 so that Mathematica interprets it as an exact rational number 123/1000?
Asked
Active
Viewed 1,050 times
8
2 Answers
6
No doubt this needs work to make robust:
fract[s_String] := (ToExpression[StringReplace[s, "." -> ""]]/
10^(StringLength[s] - First@First@StringPosition[s, "."]));
$PreRead = If[Head[#] =!= Real, (
# /. s_String :> StringReplace[s,
a : ("." ~~ DigitCharacter ...) ~~ "*^" :> a <> " 10^"] /.
s_String :> StringReplace[s, a : (
(DigitCharacter .. ~~ "." ~~ DigitCharacter ...) |
(DigitCharacter ... ~~ "." ~~ DigitCharacter ..)
) :> "fract[\"" <> a <> "\"]"] ), #, #] &
(1.23 + .1*^4 x) /Sin[ .5 a ]
(123/100 + 1000 x) Csc[a/2]
Two known issues: this breaks if you use explicit precision backtic notation, or if your input contains floats within strings.
george2079
- 38,913
- 1
- 43
- 110
-
How about
$Pre = # /. r_Real :> RuleCondition[Round[r, 10^-Ceiling@Precision@r]] &– Simon Woods Apr 15 '14 at 16:06 -
The point is to work with the actual text entered to avoid some of the issues raised in the comments arising from conversion to/from machine precision. (As a practical matter those issues may not be important, and using
$Prelike that is clearly cleaner and more robust ) – george2079 Apr 15 '14 at 16:57 -
1May be a good idea to append
/; StringMatchQ[s, RegularExpression["[+-]?[0-9]*\\.[0-9]+"]]to yourfract: this will avoid all the possible breakage with unexpected formats of the string, returning unevaluated expression instead. – Ruslan Apr 09 '17 at 09:06 -
@Ruslan: By "append to the end of
fract" do you mean exactly this?:fract[s_String] := (ToExpression[StringReplace[s, "." -> ""]]/ 10^(StringLength[s] - First@First@StringPosition[s, "."]));/; StringMatchQ[s, RegularExpression["[+-]?[0-9]*\\.[0-9]+"]]– theorist Feb 26 '18 at 07:43 -
1@theorist not quite: insert it before the trailing semicolon — to make the pattern conditional – Ruslan Feb 26 '18 at 07:57
-
@Ruslan I encountered a case in which your suggested addition causes george2079's code to break where it otherwise would have been fine: When there is a trailing decimal point (e.g., 100./3). One might encounter these if you've deliberately added trailing decimals to numericize evaluations within a notebook, and then use george's code to conveniently reevaluate the notebook using exact numbers. – theorist Dec 31 '18 at 22:36
1
Given the limitations described in the comments, a possibility is to use Ratiolize[x,0] in combination with SetPrecision or SetAccuracy. E.g.:
Rationalize[SetPrecision[0.33333, 5], 0]
1/3
While
Rationalize[SetAccuracy[0.3333, 5], 0] (* one less 3 *)
3332 / 9997
The point is to set Accuracy or Precision to the number of digits that would be normally entered to represent (conventionally) the desired rational as a floating point number.
user8074
- 1,592
- 8
- 7
-
3I think the OP wants the outputs to be
33333/100000and3333/10000respectively, but I think the simplest way to enter these numbers is33333*^-5and3333*^-4, or as fractions. – Michael E2 Feb 11 '14 at 23:31 -
@MichaelE2 it's not really simplest if you want to be able to append digits (e.g. in a manual binary search): you have to carefully count the digits to make sure your order of magnitude isn't off and that you didn't to forget to change the exponent after you appended a new digit. – Ruslan Apr 09 '17 at 08:41
-
@Ruslan If counting digits is a factor in a particular case, what's simpler than the second option, fractions, in which counting seems unnecessary? – Michael E2 Apr 09 '17 at 11:55
-
@MichaelE2 well, that's better, but inconvenient — it's always best to have to change one thing to get what you need. The simplest way would be to define a function, which would do it all for you, as
fractin george2079's answer. – Ruslan Apr 09 '17 at 13:51 -
@Ruslan I see what you mean. What seems simpler depends on context. E.g., how many conversions/entries do you have to do and how often. My personal, and admittedly parochial, experience is limited to infrequent and few conversions. Mostly nowadays they arise from converting parameters in SE questions to exact values, although
Rationalizeis usually sufficient. It hardly seems simpler to dredge up or write a function, when I can type the fraction in more quickly than I can find and type the function command. But it's different for even a moderate amount of data. – Michael E2 Apr 09 '17 at 14:22
Rationalize[0.123]– Yi Wang Feb 11 '14 at 19:25Rationalizedoes have limitations, though. – Michael E2 Feb 11 '14 at 19:51Rationalize[0.333333],Rationalize[0.3333333333333], and the explanation in the docs: "Rationalize[x] yields x unchanged if there is no rational number close enough to x to satisfy the condition |p/q-x| < c/q^2, with c chosen to be 10^-4." – Michael E2 Feb 11 '14 at 21:51Rationalize[0.333333333, 0]does not do what the OP wants. – Michael E2 Feb 11 '14 at 21:58Rationalize@{5.4 10^10, 5.4 10^11, 5.4`17 10^11}I don't know the answer, just the question.... :P (Oops, just saw the answer below!) – Michael E2 Feb 11 '14 at 23:06