Author Topic: graph of the gamma function (Euler-Mascheroni)  (Read 1579 times)

0 Members and 1 Guest are viewing this topic.

Offline BSpinoza

  • Newbie
  • Posts: 80
Re: graph of the gamma function (Euler-Mascheroni)
« Reply #15 on: August 24, 2021, 02:04:23 PM »
I've attached my screenshot (value with full 16 digits) made with my 64bit Linux (Q4OS).
I have the same result with Windows 10, 64 bit.
Could it be that you have an 32 bit system  and this is the reason for this different results?
 
gamma1.png
"Simplicity is the result of maturity."  Friedrich Schiller

Offline bplus

  • Forum Resident
  • Posts: 7446
  • b = b + ...
Re: graph of the gamma function (Euler-Mascheroni)
« Reply #16 on: August 24, 2021, 02:50:57 PM »
Quote
Could it be that you have an 32 bit system  and this is the reason for this different results?

No.

Could it be your line #4 is different than your line #4 from before?

But I don't really think that explains our difference. No my theory rests upon the different voltage they use in  Europe ;-))
« Last Edit: August 24, 2021, 02:53:50 PM by bplus »

Offline bplus

  • Forum Resident
  • Posts: 7446
  • b = b + ...
Re: graph of the gamma function (Euler-Mascheroni)
« Reply #17 on: August 24, 2021, 03:00:37 PM »
Dang my screen shot cut off the QB64 x 64 icon at top left corner, sorry.

I thought I was doing good showing the version number in bottom right corner, wait that should be assoc with x64 too.
« Last Edit: August 24, 2021, 03:02:10 PM by bplus »

Offline BSpinoza

  • Newbie
  • Posts: 80
Re: graph of the gamma function (Euler-Mascheroni)
« Reply #18 on: August 25, 2021, 01:38:27 AM »
@bplus:

Quote
Could it be your line #4 is different than your line #4 from before?
Yes, I reduced the number of iterations a little bit and 500 is enough for a 16 digit precision.

I can not explain our difference in the results (The voltage is a good theory ! ;-) )

As conclusion: My result is the better one and give me what I expect.  So this all remains as your problem ...   ;-)
(Change the voltage!)

Perhaps somebody else has an idea what the reason is for this different program results!

jack has written:

Quote
_Pi will only give Pi to double precision, if you want Pi to _Float precision then don't use _Pi(1)

This could be right, but this is not an explanation for our difference, because we both use _PI(1).


"Simplicity is the result of maturity."  Friedrich Schiller

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3657
    • Steve’s QB64 Archive Forum
Re: graph of the gamma function (Euler-Mascheroni)
« Reply #19 on: August 25, 2021, 04:39:47 AM »
The difference in precision might be between the stable build and the development build.  If you guys remember, Luke pushed some changes into the source a while back which alters 64-bit precision with the development builds.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline BSpinoza

  • Newbie
  • Posts: 80
Re: graph of the gamma function (Euler-Mascheroni)
« Reply #20 on: August 25, 2021, 06:37:13 AM »
SMcNeill has written:

Quote
The difference in precision might be between the stable build and the development build.

I'm not sure:
I have written this code for the Gamma function in 2017 and I remember that I had the same precision.
My selfmade UPN calculator , which implements this gamma function (I had an old exe from 2018) delivers the same good precision:
 
upn calculator 2018.png
"Simplicity is the result of maturity."  Friedrich Schiller

Offline bplus

  • Forum Resident
  • Posts: 7446
  • b = b + ...
Re: graph of the gamma function (Euler-Mascheroni)
« Reply #21 on: August 25, 2021, 08:38:30 AM »
Thanks BSpinoza, just for the record I tried older stable versions of QB64 and get same.

All this makes me wonder what others are getting? What is the voltage in Australia or Brazil or India? ;-))

Offline _vince

  • Seasoned Forum Regular
  • Posts: 348
Re: graph of the gamma function (Euler-Mascheroni)
« Reply #22 on: August 26, 2021, 07:03:55 AM »
I did a little experiment by numerically integrating

\int_0^1 log(1/t)^(z - 1) dt for Re(z) > 0

then using the reflection formula Gamma(z)Gamma(-z) = -pi / (z*sin(pi*z)) to get Re(z) < 0

and drawing a real plot and a domain colouring plot (ie https://en.wikipedia.org/wiki/Gamma_function#/media/File:Gamma1.png)


You can see that the real plot looks okay visually but the complex plot shows the integration errors around x=0

cgamma.png

Offline BSpinoza

  • Newbie
  • Posts: 80
Re: graph of the gamma function (Euler-Mascheroni)
« Reply #23 on: August 27, 2021, 07:14:20 AM »
jack sended me a pm with his construction of a gamma approximation
 ("' it's a blend of the Spouge and Lanczos method")   ->    @jack: Many thanks!!!
and I've made a comparison:

Code: (qb64) [Select]
' -------------- gamma function --------------------------
' The (complete) gamma function â(n) is defined to be an extension
' of the factorial to complex and real number arguments.
' It is related to the factorial by
' â(n)=(n-1)!,
'
' a slightly unfortunate notation due to Legendre which is now universally
' used instead of Gauss's simpler Pi(n)=n! (Gauss 1812; Edwards 2001, p. 8).
' ( from: ttps://mathworld.wolfram.com/GammaFunction.html)
' ---------------------------------------------------------
' jack constructed this gamma approximation,
' it's a blend of the Spouge and Lanczos method
' ---------------------------------------------------------

DIM AS _FLOAT y, z

'DIM I AS LONG, n AS LONG

PRINT "  Keisan (CASIO online calculator):"
PRINT "        â(1.25) = 0.90640247705547707798267128896691800074879192072  "
PRINT
PRINT "  Approximations:"
PRINT USING "  Stirling       ##.###################^^^^^"; gamma_stirling##(1.25)
PRINT USING "  Spouge/Lanczos ##.###################^^^^^"; gamma_spouge##(1.25)
PRINT
PRINT "--------------------------------------------------------------------------"
PRINT
PRINT "  Keisan (CASIO online calculator):"
PRINT "         â(100) = 9.332621544394415268169923885626670049071596826E+155"
PRINT
PRINT "  Approximations:"
PRINT USING "  Stirling       ##.###################^^^^^"; gamma_stirling##(100)
PRINT USING "  Spouge/Lanczos ##.###################^^^^^"; gamma_spouge##(100)



FUNCTION gamma_stirling## (x##)
    DIM AS LONG I, n
    DIM AS _FLOAT a, b, c
    PI## = 3.141592653589793238462643383F0
    n = 500
    a = (x## + n)
    b = x##
    FOR I = 1 TO (n - 1)
        b = b * (x## + I)
    NEXT I
    gamma_stirling## = SQR((2 * PI##) / a) * a ^ a * EXP(a * (-1) + (1 / (12 * a)) - (1 / (360 * a ^ 3))) / b
END FUNCTION


FUNCTION gamma_spouge## (z AS _FLOAT) ' blend of the Spouge and Lanczos approximation method by jack
    DIM AS _FLOAT a, y, y2
    z = z - 1
    a = 11##
    y = (275.9379958989626453381F0 + 2.506628274631000413006F0 * z)
    y = (13669.27934220190127409F0 + y * z)
    y = (4.012693332808590111572F5 + y * z)
    y = (7.730287584695298154390F6 + y * z)
    y = (1.021171143779219783165F8 + y * z)
    y = (9.367828308832242704191F8 + y * z)
    y = (5.892810811988612011167F9 + y * z)
    y = (2.432626162101472730328F10 + y * z)
    y = (5.950924515204440739993F10 + y * z)
    y = 6.550975741601077694099F10 + y * z

    y2 = (55## + z)
    y2 = (1320## + y2 * z)
    y2 = (18150## + y2 * z)
    y2 = (157773## + y2 * z)
    y2 = (902055## + y2 * z)
    y2 = (3416930## + y2 * z)
    y2 = (8409500## + y2 * z)
    y2 = (12753576## + y2 * z)
    y2 = (10628640## + y2 * z)
    y2 = (3628800## + y2 * z)
    y = y / y2

    y = y * (z + a) ^ (z + .5##) * EXP(-z - a)
    gamma_spouge## = y
END FUNCTION
At the End of this post you see my result.

@ bplus: Perhaps you could check this version. It would be interesting if you have a lower precision too.
 
jacks gamma function.png

"Simplicity is the result of maturity."  Friedrich Schiller

Offline bplus

  • Forum Resident
  • Posts: 7446
  • b = b + ...
Re: graph of the gamma function (Euler-Mascheroni)
« Reply #24 on: August 27, 2021, 10:52:03 AM »
Quote
@ bplus: Perhaps you could check this version. It would be interesting if you have a lower precision too.

Hi @BSpinoza

Jack has already advised me to get latest dev build because of this (Luke's fix of precision):
https://www.qb64.org/forum/index.php?topic=3987.msg133338#msg133338

Plus what Fellippe has been doing with debugging, yep!, definitely time for update.

For me, I already solved the problem of running code with extended precision with my not-so-basic interpreter.
(A little plug): https://www.qb64.org/forum/index.php?topic=3723.msg130797#msg130797
It does that calculation that gets close to 500 terms in the Fibonacci sequence which as you know takes thousands of digits to get right. :)
https://www.qb64.org/forum/index.php?topic=2921.msg133318#msg133318

And thanks again to jack for helping me get it debugged.
« Last Edit: August 27, 2021, 11:00:48 AM by bplus »

Offline jack

  • Seasoned Forum Regular
  • Posts: 367
Re: graph of the gamma function (Euler-Mascheroni)
« Reply #25 on: August 27, 2021, 11:35:43 AM »
@BSpinoza if you are interested in how I derived the coefficients for the Spouge/Lanczos gamma function let me know