0
$\begingroup$

This question was from the End-Semester question paper of the previous year of my Course on Wolfram Mathematica - Apparently when I try to code it, something is going wrong with respect to Precision - this is the question :

Numerically integrate the function f(x) [mentioned below in my code] over the limits 0.6 to twenty two different upper limits. The twenty two upper limits are to be generated from the number 0.7687079701073738466984: The sequence should be 0.7, 0.76, 0.768, 0.7687, … ,0.7687079701073738466984.

And this is my code :

ClearAll["Global`*"]
SetPrecision[$MachinePrecision, 200];
f[x_] := x*Sin[1/x^3];
lower = 0.6;
num = 0.7687079701073738466984;
lst = {};
For[i = 1, i <= 22, i++, AppendTo[lst, N[10^(-i)*Floor[10^i*num]]];]
lst

and this was the output I got :

{0.7, 0.76, 0.768, 0.7687, 0.7687, 0.768707, 0.768708, 0.768708, 
0.768708, 0.768708, 0.768708, 0.768708, 0.768708, 0.768708, 0.768708, 
0.768708, 0.768708, 0.768708, 0.768708, 0.768708, 0.768708, 0.768708}

Can someone please help me figure out where I'm going wrong?

$\endgroup$
3
  • $\begingroup$ Try using the second argument to N. For example N[10^(-i)*Floor[10^i*num], 25] $\endgroup$
    – MelaGo
    Commented Oct 30, 2023 at 17:40
  • $\begingroup$ @MelaGo it doesn't work $\endgroup$ Commented Oct 31, 2023 at 2:45
  • $\begingroup$ I get this: {0.7000000000000000000000000, 0.7600000000000000000000000, \ 0.7680000000000000000000000, 0.7687000000000000000000000, \ 0.7687000000000000000000000, 0.7687070000000000000000000, \ 0.7687079000000000000000000, 0.7687079700000000000000000, \ 0.7687079700000000000000000, 0.7687079701000000000000000, \ 0.7687079701000000000000000, 0.7687079701070000000000000, \ 0.7687079701073000000000000, 0.7687079701073700000000000, \ 0.7687079701073730000000000, 0.7687079701073738000000000, \ 0.7687079701073738400000000, 0.7687079701073738460000000, \ 0.7687079701073738466000000, ...} $\endgroup$
    – MelaGo
    Commented Oct 31, 2023 at 18:20

2 Answers 2

0
$\begingroup$

Note that the following does nothing:

SetPrecision[$MachinePrecision, 200];

First $MachinePrecision is evaluated to some number (e.g.:15.9546 ), then the precision of this number is set to 200 and the result is discarded because it is not assigned to anything.

To make a working code, you may calculate the digits of "num" and then take successively more digits, before changing the digits back to a number:

res = Table[
  t = RealDigits[num];
  t[[1]] = t[[1, 1 ;; i]];
  FromDigits[t]
  , {i, 22}]

This results to rational numbers:

enter image description here

You may convert these to decimal numbers with the corresponding number of digits by:

N[res, 22]

enter image description here

$\endgroup$
2
$\begingroup$

Couple of things.

If you're expecting to set a sort of global precision by doing SetPrecision[$MachinePrecision, 200], then you need to understand that's not what is happening. You can check this by evaluating $MachinePrecision before and after the SetPrecision.

Next, while the last several values in your lst look identical, that's only because of how they're displayed. If you look at FullForm[lst], you'll see all of the digits. However, that still won't be what you want because their precision is less than you need (they'll still be machine precision, because of the previous comment).

You'll probably need to do something along these lines:

(* First get the digits as a list *)
allDigits = First[RealDigits[0.7687079701073738466984]]
(* {{7, 6, 8, 7, 0, 7, 9, 7, 0, 1, 0, 7, 3, 7, 3, 8, 4, 6, 6, 9, 8, 4}, 0} *)

(* Next, take increasing numbers of digits which we can then apply FromDigits to. *)
digitsSequence = Rest@FoldList[Append, {}, allDigits]
(* Using Rest because we don't need the initial empty list *)
(* {{7},{7,6},<<19>>,{7,6,8,7,0,7,9,7,0,1,0,7,3,7,3,8,4,6,6,9,8,4}} *)

(* Now we can get your values to infinite precision. *)
limitValues = FromDigits[{#, 0}] & /@ digitsSequence
(* {7/10, 19/25, 96/125, <<18>>, 960884962634217308373/1250000000000000000000} *)

Now, if you need finite precision versions of these limit values, you can apply N, but you'll need to be careful, because the finite precision representations might introduce subtle deviations from what you expect.

$\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.