I'm experiencing a small glitch on the label of a slider. When I change the value of another slider, the time slider may display its value at higher precision (5 digits after the decimal point), while it normally shows only two. I want to keep it at 2 digits after the decimal point. How can I do that?

Here's a minimal working example to show the problem :

  If[T > Sqrt[a], T = Sqrt[a]];
  Plot[Sin[2 Pi a (t - T)], {t, -10, 10}, 
    Frame -> True, PlotRange -> {{-5, 5}, {-1, 1}}],
  {{a, 1, Style["Frequency", 10]}, 0, 5, 0.01, Appearance -> {"Labeled"}},
  {{T, 0, Style["Time translation", 10]}, -10, Dynamic[Sqrt[a]], 0.01, 
      Appearance -> {"Labeled"}}]

Just set the two sliders to their max value, then lower the first parameter ("Frequency"). You'll see the second label with too many digits (five digits after the decimal point). I want that label to always shows 2 digits only.

So how can I constrain the label output to 2 digits only, after the decimal point, in all circumstances?

  • $\begingroup$ Does NumberForm not work? $\endgroup$ – Michael E2 Dec 22 '16 at 18:34
  • $\begingroup$ @MichaelE2, apparently not. The problem is the updated value of the "Time" label, which has too much digits. I don't see where to change it. Is this a limitation of the manipulate implementation of Mathematica ? $\endgroup$ – Cham Dec 22 '16 at 18:41

The problem arises when your code adjusts the value of T to keep it below Sqrt[a]. When that happens and the T slider is also at its maximum, the value of T shown to right of time translation control must be updated immediately. This update seems to bypass the normal mechanism used update the control value. And once the value gets corrupted, it stays corrupted until the time translation control is manipulated by the user.

This problem can be fixed coercing T to only take on values that have the right granularity. You use the granularity .01, but I have changed it to .1 in this answer, because the lower value makes it easier to see what is going on.

  If[T > Sqrt[a], T = Floor[Sqrt[a], .1]];
  Plot[Sin[2 Pi a (t - T)], {t, -10, 10},
    Frame -> True, 
    PlotRange -> {{-5, 5}, {-1, 1}}], 
    {{a, 1, "Frequency"}, 0, 5, .1, Appearance -> "Labeled"}, 
    {{T, 0, "Time translation"}, -10, Dynamic[Floor[Sqrt[a], .1]], .1, 
      Appearance -> "Labeled"}]
  • $\begingroup$ It works, but it's not obvious to me how to adapt this to my full code. $\endgroup$ – Cham Dec 23 '16 at 2:16
  • $\begingroup$ When I use your trick in my full code, changing some parameters may make MMA to complain : Input value {... } lies outside the range of data in the interpolating function. Extrapolation will be used. $\endgroup$ – Cham Dec 23 '16 at 2:22
  • 1
    $\begingroup$ @Cham. I can only work with the code you post. $\endgroup$ – m_goldberg Dec 23 '16 at 2:22
  • $\begingroup$ I understand, but I can't post my full code ! It's too long and complicated just for this precision problem displayed in the label. $\endgroup$ – Cham Dec 23 '16 at 2:23
  • $\begingroup$ @Cham You should bound the range of the slider so that the input into the interpolating function stays within its domain. If there's a complicated relationship between the parameters and the input, then it might be a hard problem to solve. You could always use FindRoot inside Dynamic to set the limit on T. OR, use Quiet, if the extrapolation error is negligible. $\endgroup$ – Michael E2 Dec 24 '16 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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