3
$\begingroup$

I'm practising making Dynamics so I made a simple solution measurement converter but I'm having trouble getting a numerical output at basemL/baseL. It keeps coming out as fractional even though I've probably overdone using N. I've also tried doing this with With putting the entire Grid inside it but it still comes out as fractional.

DynamicModule[
    {baseTsp=1, baseGal=1,basemL=N[Dynamic[baseTsp*157725491/32000000]], baseL=N[Dynamic[baseGal*473176473/125000]]},
    Grid[{
        {InputField[Dynamic@baseTsp, Number,ImageSize->{50,20}, Alignment->Center],
        "Tsp  (Solution) per",
        InputField[Dynamic@baseGal, Number,ImageSize->{50,20}, Alignment->Center],
        "Gallon (Base)"},
        {InputField[basemL, Number,ImageSize->{70,20}, Alignment->Center,Enabled->False],
        "mL (Solution) per",
        InputField[baseL, Number,ImageSize->{70,20}, Alignment->Center,Enabled->False],
        "mL (Base)"},
        {InputField[N[basemL/baseL],Enabled->False],
        "per 1000 mL"}
    }, Alignment->Left]
]

How do I make it output as numerical?
Also if you have suggestions on how I can improve my code or better conventions using Dynamics please feel free to tell me.

$\endgroup$
2
  • $\begingroup$ I tried as you suggested {baseTsp=1, baseGal=1,basemL, baseL} and InputField[basemL=N[Dynamic[baseTsp*157725491/32000000]] for basemL and baseL but it still comes out as fractional output at basemL/baseL. $\endgroup$ May 31 '17 at 0:04
  • $\begingroup$ Inside the definition of baseml within the InputField wrap the right hand side of the equal sign with N. That is InputField[Dynamic[basemL = N[baseTsp*157725491/32000000]] ... $\endgroup$ May 31 '17 at 2:09
3
$\begingroup$

A slightly different formulation than Karsten 7 proposed in a comment but similar in spirit I think:

DynamicModule[

 {baseTsp = 1, baseGal = 1, basemL, baseL},

 Grid[{
   {
    InputField[Dynamic[baseTsp], Number, ImageSize -> {50, 20}, 
     Alignment -> Center], "Tsp  (Solution) per", 
    InputField[Dynamic[baseGal], Number, ImageSize -> {50, 20}, 
     Alignment -> Center], "Gallon (Base)"},
   {
    InputField[Dynamic[basemL = baseTsp*157725491`/32000000`], Number, 
     ImageSize -> {70, 20}, Alignment -> Center, Enabled -> False], 
    "mL (Solution) per", 
    InputField[Dynamic[baseL = baseGal*473176473`/125000`], Number, 
     ImageSize -> {70, 20}, Alignment -> Center, Enabled -> False], "mL (Base)"},
   {
    InputField[Dynamic[basemL/baseL], Enabled -> False], "per 1000 mL"}},
  Alignment -> Left
 ]
]

Note the use of backtick to enter machine precision numbers. See:

$\endgroup$
2
  • $\begingroup$ This works, thanks. Is putting variable definition inside Grid rather than at declaration considered convention or are there computational differences? After playing with the code it seemed that the Dynamic during definition of basemL and baseL was what was causing the output to be fractional. $\endgroup$ May 31 '17 at 0:22
  • $\begingroup$ @Isho I would not say it is a convention, rather it just seems appropriate here. A Dynamic expression must be displayed in the Notebook for it to have effect, so if we are going to use Dynamic in this way displaying it inside the Grid makes sense. An alternative is to make the entire Grid expression Dynamic, i.e. Dynamic[Grid[ . . . ]] but that would cause extra evaluation I believe as the entire Grid would be redrawn when a value is changed, rather than only the InputField. $\endgroup$
    – Mr.Wizard
    May 31 '17 at 7:21

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.