# Underflow occurring in computation

I've got an "Underflow occurred in computation" problem. Here's a simplified version of the code:

toplot =
Table[
Exp[-((x - 1.21*10^15)^4/(2*10^25))],
{x, 1.20*10^15, 1.22*10^15, 10^12}, {y, 1.20*10^15, 1.22*10^15, 10^12}];

ListDensityPlot[toplot, PlotRange -> All]


and the error is

General::unfl: Underflow occurred in computation. >>
...
General::stop: Further output of General::unfl will be suppressed during this calculation. >>

When I replace the power 4 with the power 2 in the code as follows:

toplot =
Table[
Exp[-((x - 1.21*10^15)^2/(2*10^25))],
{x, 1.20*10^15, 1.22*10^15, 10^12}, {y, 1.20*10^15, 1.22*10^15, 10^12}];


the code works without any error and I get

ListDensityPlot[toplot, PlotRange -> All]


Unfortunately that's not the function I need :(

I found some other topics about the underflow problem, but I wasn't still able to fix my problem by just reading them.

Any help/suggestions to avoid/bypass the problem are welcome.

• I suppose it's about numerical calculation error.
– Wjx
Commented Jul 19, 2016 at 11:30
• \$MinNumber=6.229688249675322*10^-1355718576299610 It's quite small, that's why I'm surprised that the computation has an underflow, but I'm pretty far from being an expert... Commented Jul 19, 2016 at 13:12
• You're getting underflow, but you're still getting a valid plot right? Commented Jul 19, 2016 at 14:23
• Ponder this Min@Table[-((x - 1.21*10^15)^4/(2*10^25)), {x, 1.20*10^15, 1.22*10^15,10^12}, {y, 1.20*10^15, 1.22*10^15, 10^12}]/Log[10], the exponent base 10 of your smallest number. Commented Jul 19, 2016 at 15:08
• Why couldn't you rescale your units and avoid all this trouble? Commented Jul 19, 2016 at 15:12

The difficulty of doing numerical calculation with the 4th power as opposed to the square is immense. This easily demonstrated by doing a couple of exact computations.

With[{x = (12/10)*10^15}, Exp[-((x - (121/100)*10^15)^2/(2*10^25))]]


1/E^5

With[{x = (12/10)*10^15}, Exp[-((x - (121/100)*10^15)^4/(2*10^25))]]


1/E^500000000000000000000000000

It should now be evident to you why you are getting underflow with the 4th power, but not with the square.

• Honestly, the 10^25 and 10^15 just look like a bad choice of units on the OP's part... Commented Jul 19, 2016 at 16:08
• @J.M. Absolutely agree. Scaling is the solution. But that was already covered in the comments. I post this because I want the OP to see the immense numeric effect of the 4th power, and how easy it is to use Mathematica to illuminate the issue. Commented Jul 19, 2016 at 16:14
• @m_goldberg thank you for your help! Unfortunately it wasn't easy to scale the problem because that was just a code that I had to add and match to another old program, and scaling all that code would have been too long! Just for the sake of completeness, the correct value for the denominator was ~(2*10^48): by using it the program works perfectly! Commented Jul 19, 2016 at 20:25