# 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.

• 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). – MarcoB Apr 8 at 12:42
• @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. – Qbyte Apr 8 at 12:48
• 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"}] – Bob Hanlon Apr 8 at 13:31
• @BobHanlon -- You should add this as an answer. Simple and elegant, as we have all come to expect from you;-) – Jagra Apr 8 at 15:04

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"}]


• 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? – Qbyte Apr 8 at 23:31
• @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. – Bob Hanlon Apr 8 at 23:58
Manipulate[If[b < Pi, b, Pi], {b, {0, Pi}, ControlType -> Slider}]