2
$\begingroup$

Using the ScientificNotationThreshold option inside NumberForm causes erroneous behavior and generates a mystifying error message when used inside Dynamic.

For example, running

With[{x = N[-\[Pi] 10^4]},
 Print[{
   NumberForm[x],
   NumberForm[x, ScientificNotationThreshold -> {-3, 3}]
   }];
 Grid[{{
    Dynamic[NumberForm[x], (x = #) &],
    Dynamic[
     NumberForm[x, 
      ScientificNotationThreshold -> {-3, 3}], (x = #) &]},
   {InputField[
     Dynamic[Style[NumberForm[x], 
       ShowStringCharacters -> False], (x = #) &], FieldSize -> 5, 
     Alignment -> Right],
    InputField[
     Dynamic[Style[
       NumberForm[x, ScientificNotationThreshold -> {-3, 3}], 
       ShowStringCharacters -> False], (x = #) &], FieldSize -> 5, 
     Alignment -> Right]}
   }]
 ]

produces this output

enter image description here

accompanied by the twice repeated error message

NumberForm::expons: The value ScientificNotationThreshold of the option {-3,3} should be a list of two values. Each can be a machine integer or +/- Infinity.

In the example, NumberForm is invoked three times

  1. Not inside Dynamic
  2. Inside Dynamic, and
  3. Inside an InputField that contains a Dynamic argument

and each one is done two different ways, once WITHOUT and once WITH the ScientificNotationThreshold option explicitly set.

The error occurs only when I try to explicitly set the ScientificNotationThreshold option inside Dynamic.

Even the statement of the error seems slightly backward.

What's going on here?

Seems like a bug in Mathematica to me.

Is there a simple way to make this work the way it's supposed to?

Note: I included the two cases that use InputField merely to show the type of code I actually intend to use and why Dynamic is necessary for my purposes.

$\endgroup$
1
  • 1
    $\begingroup$ Pink boxes no longer appear in 13.1. $\endgroup$
    – QuantumDot
    Commented Oct 1, 2022 at 19:10

1 Answer 1

1
$\begingroup$

This bug still persists in Mathematica 12.3.

Workaround

Define

ToRawBoxesQuiet[value_] := Quiet[RawBoxes[ToBoxes[value]]]

and use the function before calling Dynamic:

Dynamic[ToRawBoxesQuiet[...]]

or Manipulate

Manipulate[
  ToRawBoxesQuiet[...],
  ...
]

How I found the workaround

I used

Dynamic[NumberForm[1, ScientificNotationThreshold -> {-4, 4}]]

as an example.

In the stack trace of the error there is the line

MakeBoxes[NumberForm[1, ScientificNotationThreshold -> {-4, 4}], StandardForm]

so it seems like a bug of MakeBoxes and NumberForm. So I tried to manually create the boxes (using ToBoxes) and use Quiet to suppress the error message.

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