Home > graphics > Dealing with numbers too large for machine precision in Graphics

# Dealing with numbers too large for machine precision in Graphics

March 14Hits:2

Graphics only supports machine precision numbers (i.e. number that can be converted to machine precision). Take for example

Graphics[Point[{0, Exp[1000.]}]] 


which produces an error because Exp[1000.] is too large to be representable as a double.

What is the simplest way to deal with situations like this? Replacing all too large numbers with just some arbitrary very large machine precision number is fine, as they'll be outside the plot range anyway. I'm sure there must be some built-in function that can fix this easily.

I wonder whether I have understood your question correctly because I know you'll be aware of Clip

data =
Clip[#, {-$MaxMachineNumber,$MaxMachineNumber}] & /@ {0, Exp[1000.]}

(*
==> {0, 1.797693135*10^308}
*)

Precision /@ data

(*
==> {\[Infinity], MachinePrecision}
*)

data = RandomReal[10, {10, 2}]~Join~{{0, Exp[1000.]}};

Graphics[Point[data], PlotRange -> {0, 10}]



data = Map[Clip[#, {-$MaxMachineNumber,$MaxMachineNumber}] &, data, 2]

(*
==> {{1.712790207, 2.900090032}, {2.659619591,
7.829120544}, {1.961467042, 3.28800444}, {8.391594058,
6.895205615}, {7.272335729, 5.320941734}, {2.663140973,
0.988927991}, {3.408201238, 2.47708199}, {7.951584505,
7.102838229}, {6.826916007, 5.639933047}, {5.307337319,
1.629710693}, {0, 1.797693135*10^308}}
*)

Graphics[Point[data], PlotRange -> {0, 10}]



Using @Sjoerd idea of Clipping, maybe you could use too Rescale. Something simple could be a wrapper to rescale every point inside a Graphics:

rescale[things_] :=
Module[{points =
Cases[things, {_?NumericQ, _?NumericQ}, \[Infinity]], minmax,
rescaled},
minmax = Transpose[{Min /@ #, Max /@ #} &@Transpose[points]];
rescaled = Clip[minmax, {-$MaxMachineNumber,$MaxMachineNumber}];
things /.
{x_?NumericQ, y_?NumericQ} :>
{Rescale[x, minmax[[1]], rescaled[[1]]],
Rescale[y, minmax[[2]], rescaled[[2]]]}
]



This is just a "draft" since points should be taken only from graphics directives with coordinates, and then filter also the Scaled or Offset coordinates.

Graphics[rescale[{... Points[data], ..., Polygon[poly], ...}]]



Anyway, you will have to adjust the aspect ratio after doing the rescale (the rescale is different for each axis).

## Related Articles

• ### Dealing with numbers too large for machine precision in GraphicsMarch 14

Graphics only supports machine precision numbers (i.e. number that can be converted to machine precision). Take for example Graphics[Point[{0, Exp[1000.]}]] which produces an error because Exp[1000.] is too large to be representable as a double. What

• ### dealing with a very large state machine in a classSeptember 10

I am currently implementing a class for a network protocol. The protocol uses a quite complex and extensive finite state machine. A pair of an event plus a state is mapped to a function, which does some work and sets the new state. As there are about

• ### Floor inconsistent with Less for machine-precision approximate numbersJuly 9

For machine-precision numbers, Mathematica uses a tolerance for comparisons, so that 1.-$MachineEpsilon==1. However, Floor does not take into account this tolerance, leading to an inconsistency: {Floor[#], # < 1., # >= 1.} &[1. -$MachineEpsilon

• ### Can I force Mathematica to use machine precision? May 8

I'm working on some pretty intense computation in Mathematica; when my code started running slowly, I tracked the source of the problem to Exp[]. I need to exponentiate every element of a 50x500x500 array; performing the operation on a 500x500 array

• ### Machine precision near zero: not fulfilled?September 19

I am puzzled by the behavior of Mathematica machine precision with numbers approaching zero. This manifests itself, e.g., with FixedPoint and the like. In the examples below I will use the following "service" function to take only a few values o

• ### How to remove duplicates from set of machine precision 2D points?March 25

I have a set of 2D points with machine precision coordinates. I need to remove all duplicates. Performance is important. This is the most obvious fast solution: Union[points] Unfortunately it turns out that unlike Equal and SameQ, Union has no tolera

• ### Machine precision infinityApril 14

Is it possible to obtain the machine precision (double) version of Infinity? This is useful when using LibraryLink and C functions that accept doubles: passing Infinity raises a type error, because Infinity is a symbol. --------------Solutions-------

• ### How does Plus work on machine precision Real arguments?January 10

I thought Kahan's summation method would make a nice example for students to use to think about round-off error [W. Kahan, Pracniques: Further Remarks on Reducing Truncation Errors, Commun. ACM 8 (1965), 40]. The method is available (I surmise) throu

• ### Converting to machine precisionJune 28

There are multiple ways to convert an expression to machine precision, for example: In[1]:= a = Sqrt[2] Out[1]= Sqrt[2] In[2]:= {1.a, 1`a, [email protected], SetPrecision[a,MachinePrecision]} Out[2]= {1.41421,1.41421,1.41421,1.41421} In[3]:= Precision /@ % Out[3]=

• ### N, machine precision and expression evaluation September 23

I should state that I am a Mathematica beginner but I checked the questions about machine vs arbitrary precision (such as: this one ) and I continue to have the following issue while trying to evaluate an expression: After various calculations which

• ### Handling a matrix with components greater than machine precisionDecember 11

I have four quantities stemming from a 4th order differential equation. I can represent these as a vector which is a product of a 4X4 matrix  M=\left\{v,\frac{\partial v}{\partial x},\frac{\partial ^2v}{\partial x\, \partial x},-\frac{\partial ^3v}

• ### Dynamic Slider and InputField: some numbers with machine precision display badly April 24

Consider the following InputField InputField[Dynamic[h2, If[# === Null, h2 = h2, h2 = Round[#, 0.001]] &], Number] The purpose of the second argument of Dynamic is to avoid someone entering a value with more than 3 decimal places (or emptying the fie

• ### Dealing with 6 VERY large equations. Am I doing this correctly, and can I expect a solution?July 24

I'm new to Mathematica, but so far, everything seems straight forward and intuitive, which is really nice. However, I've come to use this software since I have to show a rather nasty 3x3 matrix is unitary. Initially, I set up a system to solve Conjug

• ### Best online backup for large virtual machine July 29

I do all of my development in a virtualbox virtual machine. Copying it over and over again to an external for back-up gets tedious and I simply do not do it as often as I should. Ideally I would like to simply be able to shut down my VM, click 'backu

• ### LFSR for small numbers with large periodsJanuary 16

I want to generate a few random numbers using an LFSR. However, the LFSR output depends on the number of taps, so for a large period I use large (relative) number of bits. This causes the numbers to be as large as the period. For example, I would lik

• ### Perl module to deal with numbered nested objectsAugust 24

I wrote a small perl module that dealing with PHP-ish array structures, and I'm going to release it to CPAN. I want it to be reviewed before the release about the module naming and the code itself. Could you please give me improvements? Any suggestio

• ### JSON export truncates numbers to 6 digits of precisionJune 12

It seems that in version 10.0.2 and 10.1.0, floating point numbers are truncated to only 6 digits of precision when exporting to JSON. Example: ExportString[RandomReal[1, 5], "JSON"] (* "[ 0.832049, 0.355306, 0.615313, 0.332704, 0.186309 ]&

• ### Large Time Machine Backups 10.9.4. Excluded Dropbox and VirtualBoxSeptember 11

Issue: Time Machine wants to take 60Gb, 40GB, etc. when that is not the amount of data change. I have excluded Dropbox and my VirtualBox VMs from the backup via the GUI. I tried this previously via the command line (tmutil) with no positive results (

• ### How a large virtual machine disk affects SSD life timeSeptember 14

I've an interesting doubt. Since virtual machines create a file that represents a hard disk - the so-called virtual disk -, and it usually takes 20-40GB (and even more), how does this affect a solid-state disk (SSD) life time? For example, in my case

• ### How should I deal with numbering of claims?February 8

I've defined my claim in the following way: \newtheorem{claim}{Claim} The numbering works perfectly fine, but what should I do in cases when I want to skip some numbering. That is: Claim 1: .... Claim 2: .... Claim: //just claim without numbering! Cl