Suppose we have the following function:

NN = 150;
W[n1_] := NN!/(n1 ! (NN - n1!)) (1/2)^n1 (1/2)^(NN - n1)

Then we can Plot or Listplot it:

ListPlot[Table[{n1, W[n1]}, {n1, 1, 10, 0.1}], Joined -> True, 
 Frame -> True, Axes -> None, BaseStyle -> {FontSize -> 16}, 
 PlotMarkers -> {"\[Bullet]", 10}, PlotStyle -> Red]

enter image description here

If we change NN to 200 or more, then we get the following error:

ListPlot::prng: Value of option PlotRange -> {{0,10.},{-1.341145999799935*10^312,2.466246209857526*10^312}} is not All, Full, Automatic, a positive machine number, or an appropriate list of range specifications. >>

How can we deal with these large numbers in plot?

  • 4
    $\begingroup$ Divide the function by some large exponent, e.g. 10^200, then generate your own tick labels that reverse the scaling. $\endgroup$ – ciao Feb 11 '14 at 9:05
  • $\begingroup$ The workaround to use is what rasher said. One more thing to mention is that while in general Mathematica can handle arbitrary precision numbers, Plot (and Graphics) will only work with machine precision numbers, i.e. numbers smaller than $MaxMachineNumber. $\endgroup$ – Szabolcs Feb 11 '14 at 14:39
  • $\begingroup$ rasher and Szabolcs, thanks a lot for your answers. $\endgroup$ – Soodeh Z. Feb 11 '14 at 19:42
  • 1
    $\begingroup$ I just met the same problem. Shouldn't this be considered a bug? Because the error message that mma gives is completely irrelevant. $\endgroup$ – Yi Wang Mar 5 '14 at 10:06
  • $\begingroup$ This problem has annoyed me for several years -I use the scaling solution but it is a nuisance. Maybe it is worth writing to Wolfram to develop a specialized function for this purpose that does this scaling automatically so the front end plot functions can be used seamlessly. $\endgroup$ – user20060 Sep 27 '14 at 3:10

Plot and more generally Graphics can only handle coordinates that are machine numbers, i.e. in the range $MinMachineNumber to $MaxMachineNumber. On my machine these are of the order $10^{308}$.

This limitation is probably present because graphics are rendered by the front end (not the kernel which supports arbitrary precision arithmetic).

The simplest solution would be to linearly rescale everything, i.e. divide the numbers by a large value, before plotting. Then you can manually set the Ticks to reflect the unscaled values.

  • $\begingroup$ I think Plot uses Compile to plot faster, so it uses only machine number.. ? $\endgroup$ – Nam Nguyen Feb 12 '14 at 16:32
  • 1
    $\begingroup$ @DaoTRINH Good point. I don't know if it auto-compiles or not. But it also has the WorkingPrecision option, so it can work with arbitrary precision arithmetic, even if it doesn't by default ... This comes useful as a simple brute force solution when there's some cancellation in the formula that eats up precision. $\endgroup$ – Szabolcs Feb 12 '14 at 18:01

This is a limitation of Graphics itself. If the PlotRange problem is bypassed we still get an error:

NN = 200;
W[n1_] := NN!/(n1! (NN - n1!)) (1/2)^n1 (1/2)^(NN - n1)
dat = Table[{n1, W[n1]}, {n1, 1, 10, 0.1}];

Graphics[Point @ dat]

enter image description here

This at least indicates that the problem is with the coordinates, but again the error message is less than accurate such obviously that is "a pair of numbers."

Briefly searching I could not find it directly stated in the documentation that Graphics only works with machine size floats. I wonder if such an omission (if true) is sufficient to consider this at least a "documentation bug."


A workaround I found while struggling with the same problem. It seems that Graphics can interpret \[Infinity] and -\[Infinity]. So the idea is simply to filter the data before plotting it:

Which[# < 0 && Abs[#] >= $MaxMachineNumber, -\[Infinity], # > 0 && 
Abs[#] >= $MaxMachineNumber, +\[Infinity], True, #] & /@ data

The advantage is to not having to change the scale and the ticks of the plot.


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.