4
$\begingroup$

Problem

Consider the following code:

DynamicModule[{a = {0, Pi}, b = 4},
  {Slider[Dynamic@b, {a}], Dynamic@b}
]

I want a Slider which sets b to the exact values in a (0 and Pi). However b is set to 3.14159 instead of Pi.

The same problem exists also for Manipulator and Manipulate:

Manipulate[b,
  {b, {0, Pi}, ControlType -> Slider}
]

Is there a better way to do this than the following workaround?

Workaround

My current workaround is to use a third variable c and Nearest:

DynamicModule[{a = {0, Pi}, b, c},
  {Slider[Dynamic@b, {a}], Dynamic@b, Dynamic[c = First@Nearest[a, b]]}
]

this sets c correctly to 0 or Pi but it is a bit cumbersome.

$\endgroup$
4
  • $\begingroup$ I cannot reproduce your results with Manipulate in MMA 12.2. The variable does get set to Pi. As an aside, using a slider to choose between only two values is very counterintuitive, since a slider naturally suggests a continuum of values. Use another control that explicitly indicates only two values are available (just like the default Manipulate would do with two values given). $\endgroup$
    – MarcoB
    Commented Apr 8, 2021 at 12:42
  • $\begingroup$ @MarcoB Ok, then I have to test again with Manipulate. My actual usage includes many multiples of Pi. This question is only a minimal example. $\endgroup$
    – Qbyte
    Commented Apr 8, 2021 at 12:48
  • 2
    $\begingroup$ If you want the control variable to only take exact values you need to specify step values that are exact values, e.g., Manipulate[var, {{var, Pi}, 0, Pi, Pi/16, Appearance -> "Labeled"}] $\endgroup$
    – Bob Hanlon
    Commented Apr 8, 2021 at 13:31
  • $\begingroup$ @BobHanlon -- You should add this as an answer. Simple and elegant, as we have all come to expect from you;-) $\endgroup$
    – Jagra
    Commented Apr 8, 2021 at 15:04

2 Answers 2

5
$\begingroup$

If you want the control variable to only take exact values you need to specify all values including the step value as exact values, e.g.,

Manipulate[var, {{var, Pi}, 0, Pi, Pi/16, Appearance -> "Labeled"}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you for this great answer. However my full problem is that I have a list of numbers which may be also {0, Pi, 4.5, 7 Pi} with no repeatable patterns. Do you also have an easier solution for this case? $\endgroup$
    – Qbyte
    Commented Apr 8, 2021 at 23:31
  • $\begingroup$ @Qbyte - For a slider you need to specify the lower bound, upper bound, and step size. You have not said how the list {0, Pi, 4.5, 7 Pi} relates to a slider or what the list is supposed to represent. If you are just trying to select from a list, a SetterBar or RadioButtonBar or PopupMenu may be more appropriate. Provide a complete description of what you are trying to accomplish. $\endgroup$
    – Bob Hanlon
    Commented Apr 8, 2021 at 23:58
2
$\begingroup$
Manipulate[If[b < Pi, b, Pi], {b, {0, Pi}, ControlType -> Slider}]

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.