2
$\begingroup$

When I enter

N[Gamma[2, -40]]

into my Mathematica notebook, I get

-9.18003*10^18 + 1124.23 i

However, Wolfram Alpha will give me a real expression. Why do these two numbers disagree - and which one is correct?

$\endgroup$
  • 1
    $\begingroup$ It is a precision issue. Use arbitrary precision rather than machine precision: N[Gamma[2, -40], 20] $\endgroup$ – Bob Hanlon Jun 26 '17 at 13:41
  • $\begingroup$ @BobHanlon Apparently I also have to wrap N[x, 20] around Gamma whenever I use Gamma in longer equations, if I want to kill those imaginary terms in the result? Sounds somewhat suspicious to me. $\endgroup$ – FooBar Jun 26 '17 at 13:48
  • 1
    $\begingroup$ Using arbitrary precision causes Mma to track and control the precision. Look at N[Gamma[2, -40], 6]. You could also force the Gamma function to evaluate before asking for the numerical approximation: Evaluate[Gamma[2, -40]] // N $\endgroup$ – Bob Hanlon Jun 26 '17 at 14:00
  • $\begingroup$ @BobHanlon the Evaluate, // chain also yields an imaginary component $\endgroup$ – FooBar Jun 26 '17 at 14:02
  • $\begingroup$ Must be a version difference. There is no imaginary component with version 11.1.1 for Mac OS X x86 (64-bit) (April 18, 2017) $\endgroup$ – Bob Hanlon Jun 26 '17 at 14:04
3
$\begingroup$

This is a precision issue. If you use arbitrary precision rather than machine precision, Mathematica will track and control the precision.

$Version

(*  "10.4.1 for Mac OS X x86 (64-bit) (April 11, 2016)"  *)

N[Gamma[2, -40]]

(*  -9.18003*10^18 + 1124.23 I  *)

N[Gamma[2, -40], 30] // Chop

(*  -9.18002540664377943090809651921*10^18  *)

This Gamma function can be evaluated exactly using FunctionExpand

Gamma[2, -40] // FunctionExpand

(*  -39 E^40  *)

Gamma[2, -40] // FunctionExpand // N

(*  -9.18003*10^18  *)

A later version does not have these problems

$Version

(*  "11.1.1 for Mac OS X x86 (64-bit) (April 18, 2017)"  *)

Gamma[2, -40]

(*  -39 E^40  *)

Gamma[2, -40] // N

(*  -9.18003*10^18  *)
$\endgroup$

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.