4
$\begingroup$

Suppose I have a number $0.26630939883535015$ and want to transfer it into a symbolic fraction by truncating it first. The way I do it is first Round it and then Rationalize it as the following code shows

Rationalize@Round[0.26630939883535015, 0.00001]

with output $26631/100000$. So far so good. Since I want higher precision, so I execute the following code

Rationalize@Round[0.26630939883535015, 0.000001]

Notice that I change $0.00001$ into $0.000001$ inside Round function. The result now becomes $0.266309$. It does not work. Then I tried to use the following code

Rationalize[Round[0.26630939883535015, 0.000001], 0]

However, the output is $7085634719/26606816589$ but I expect the output to be $266309/1000000$.

Is there some other method that I can achieve the goal?

$\endgroup$
4
  • 1
    $\begingroup$ want to transfer it into a symbolic fraction by truncating it first. Why truncate? You lose accuracy. The whole point of symbolic computation is that you can work with exact values as is all the time, unlink Fortran or Matlab. $\endgroup$
    – Nasser
    Commented Dec 23, 2023 at 15:13
  • 2
    $\begingroup$ I would do SetAccuracy[0.26630939883535015, Infinity] which gives 4797403637440575/18014398509481984 $\endgroup$
    – Nasser
    Commented Dec 23, 2023 at 15:22
  • $\begingroup$ @Nasser Thank you for your comment. For the specific problem I considered, losing some precision is harmless to me but a nicer representation of the number(such as 266309/1000000 instead of 4797403637440575/18014398509481984) is more important. But anyway, I don't know the method you mentioned and it might be very useful someday for me :) $\endgroup$
    – narip
    Commented Dec 23, 2023 at 23:41
  • $\begingroup$ @Nasser Furthermore, you mentioned that you don't want to lose accuracy and provided a method SetAccuracy[0.26630939883535015, Infinity] which implies your method does not lose accuracy. However, I think your method also loses accuracy since the result of 26630939883535015/100000000000000000 - 4797403637440575/18014398509481984 is not zero! $\endgroup$
    – narip
    Commented Dec 23, 2023 at 23:47

2 Answers 2

4
$\begingroup$

Use exact numbers in the second argument of Round

expr = 0.26630939883535015;

Round[expr, 10^-#] & /@ Range[6]

(* {3/10, 27/100, 133/500, 2663/10000, 26631/100000, 266309/1000000} *)
$\endgroup$
2
  • $\begingroup$ Oh, thank you very much but I didn't find it first in the documentation. It seems that if you use a decimal as the second argument of Round, it will return a decimal; if you use a fraction as the second argument of Round, then it will return a fraction. $\endgroup$
    – narip
    Commented Dec 24, 2023 at 0:00
  • 2
    $\begingroup$ From the documentation, "If a is not a real number, Round[x, a] is given by the formula Round[x, a] == a Round[x/a]" $\endgroup$
    – Bob Hanlon
    Commented Dec 24, 2023 at 0:30
9
$\begingroup$

You don't need the rounding, because Rationalize has a 2nd parameter:

Table[Rationalize[0.26630939883535015, 1/10^n], {n, 10}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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