Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
trizen
perl-scripts
Repository navigation
Code
Issues
Pull requests
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
perl-scripts
/
Math
/
lanczos_approximation.pl
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
57 lines (45 loc) · 1.57 KB
master
Breadcrumbs
perl-scripts
/
Math
/
lanczos_approximation.pl
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
57 lines (45 loc) · 1.57 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl
# Algorithm from Wikipedia:
# https://en.wikipedia.org/wiki/Lanczos_approximation#Simple_implementation
use 5.020;
use strict;
use warnings;
use Math::AnyNum qw(:overload pi real imag);
use experimental qw(signatures lexical_subs);
sub gamma($z) {
my $epsilon = 0.0000001;
my sub withinepsilon($x) {
abs($x - abs($x)) <= $epsilon;
}
state $p = [
676.5203681218851, -1259.1392167224028,
771.32342877765313, -176.61502916214059,
12.507343278686905, -0.13857109526572012,
9.9843695780195716e-6, 1.5056327351493116e-7,
];
my $result;
if (real($z) < 0.5) {
$result = (pi / (sin(pi * $z) * gamma(1 - $z)));
}
else {
$z -= 1;
my $x = 0.99999999999980993;
while (my ($i, $pval) = each @$p) {
$x += $pval / ($z + $i + 1);
}
my $t = ($z + @$p - 0.5);
$result = (sqrt(pi * 2) * $t**($z + 0.5) * exp(-$t) * $x);
}
withinepsilon(imag($result)) ? real($result) : $result;
}
foreach my $i (0.5, 4, 5, 6, 30, 40, 50) {
printf("gamma(%3s) =~ %s\n", $i, gamma($i));
}
__END__
gamma(0.5) =~ 1.77245385090551659496855986697771284175944211142
gamma( 4) =~ 6.00000000000000628999184513591742545418327380194
gamma( 5) =~ 24.0000000000000308599507225303222574058679398028
gamma( 6) =~ 120.000000000000178632999163000072600390777175518
gamma( 30) =~ 8841761993739669928012342097034.15093049782426111
gamma( 40) =~ 20397882081197200259694400837033107505429486392
gamma( 50) =~ 6.08281864034254395430563164837656389765153447987e62
You can’t perform that action at this time.