3
$\begingroup$

I have an Interpretation Button that displays its label correctly on evaluation. However, when I copy and paste it as input the label loses its formatting.

Interpretation[{x = RandomReal[]}, Button[Dynamic[N[x, 6]], x = RandomReal[]], N[x, 6]]

Mathematica graphics

When this is copy and pasted into an input cell it changes to:

Mathematica graphics

How can I get it to retain its label formatting which should be the 6 decimal place number?

Also, I though N was cutting the number down to 6 significant figures. This is obviously not the case since the machine precision number is showing up in the label. What have I missed here, as well?

$\endgroup$
1
  • $\begingroup$ It's because an input cell displays the number input form, which is what you are seeing. $\endgroup$
    – m_goldberg
    Commented Dec 2, 2016 at 1:52

2 Answers 2

3
$\begingroup$

It happens because "Input" cell style has NumberMarks -> True while "Output" inherits default False.

You can set BaseStyle -> NumberMarks -> False for the Button:

Interpretation[
 {x = RandomReal[]}, 
 Button[Dynamic[N[x, 6]], x = RandomReal[], BaseStyle -> NumberMarks -> False], 
 N[x, 6]
]
$\endgroup$
1
$\begingroup$

As an option

Interpretation[{x = RandomReal[]}, Button[Dynamic@ToString@SetPrecision[x, 6], 
               x = RandomReal[]], N[x, 6]]

Or

Interpretation[{x = RandomReal[]}, Button[Dynamic@N@Round[x, 1*^-6], 
               x = RandomReal[]], N@Round[x, 1*^-6]]

As far as I understand, N really doesn't change anything about a machine number, not even its precision, it does most of the work on exact inputs. So for machine numbers you need to use SetPrecision at the very least.

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