Javascript: gamma approximation


 
Thread Tools Search this Thread
Top Forums Programming Javascript: gamma approximation
# 1  
Old 11-25-2013
Javascript: gamma approximation

Here is some javascript code that implements the Lanczos approximation to the gamma function (http://en.wikipedia.org/wiki/Lanczos_approximation):

Code:
<script type="text/javascript">
// Gamma approximation; coefficients used by the GNU Scientific Library
function gamma(z) {
  var p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7];
  if z < 0.5 {
     return Math.PI / (Math.sin(Math.PI*z) * gamma(1-z));
  } else {
    z -= 1;
    x = p[0];
    for (var i = 0; i < p.length; i++) x += p[i]/(z+i);
    t = z + p.length - 1.5;
    return Math.sqrt(2*Math.PI) * Math.pow(t, z+0.5) * Math.exp(-t) * x;
  }
}

for (var x = 0.1; x < 3; x += 0.1) document.write("x: " + x + " gamma(x): " + gamma(x) + "<br/>");
</script>

This returns an empty page, which usually means there is a syntactical error in the code. In the absence of a proper debugger, can anyone point out where this error might be?

Thank you in advance.
# 2  
Old 11-25-2013
If you have Firefox, ctrl-shift-j to bring up an error console. Clear it, reload the page, and you should see the error.

In this case it's complaining about missing brackets:

Code:
if z < 0.5 {

Code:
if(z < 0.5) {

# 3  
Old 11-26-2013
Thank you that worked. Ctrl-Shift-J also works in Chrome.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

Need help with Javascript

Hi guys, Ok first, let me explain what I want to do. I'm making a theme for the iphone, and I found a nice wallpaper slideshow script. Here is how it knows which wallpapers to use: <script type="text/javascript"> // SLIDE ROTATION FREQUENCY (in minutes) var slideRotation = 0.4;... (3 Replies)
Discussion started by: kicker75
3 Replies

2. Programming

Help in javascript

Hi , I wanted to know if its possible to execute a javascript function like this E:- function js1(){ alert ("this is js1"); } function js2(){ alert ("this is js2"); } function js3(){ .... execthisscript(js1); execthisscript(js2); } ... (1 Reply)
Discussion started by: daptal
1 Replies

3. Web Development

Javascript Problems in Opera 10

Ref earlier post: https://www.unix.com/web-programming-web-2-0-mashups/118283-opera-10-0-released-looks-nice.html I have noticed that Google Docs, Speadsheets does not work properly in Opera 10. Anyone else seen the same thing? (1 Reply)
Discussion started by: Neo
1 Replies

4. Web Development

Sortable Tables in Javascript

Hi, I am writing a web application using Perl-CgI mostly. I wanted to integrate a table in which I was capable of sorting columns and I don't think this is possible with static HTML code. Can someone help me integrating some javascript code into what I have to acheive the above? Regards,... (6 Replies)
Discussion started by: garric
6 Replies

5. UNIX and Linux Applications

stopclock coding in javascript.

i have got a problem in this coding.i have got start/pause in the same button.wen i open that page the stopclock runs automatically.wen i click pause it get paused in the frontend textbox bt running in the backend.wen i again click that start itz starting from the time without considering that... (2 Replies)
Discussion started by: VIJI
2 Replies

6. Shell Programming and Scripting

javascript injection

Please advise a script to get rid of the following code which is infected in a large number of files ( in particular php and html files ) <div id="testws35fdgh"></div> <script language="JavaScript"> var0 = "\x69\x3c\x33\x27\x34\x38\x30\x75\x3b\x34"; var1 =... (20 Replies)
Discussion started by: fed.linuxgossip
20 Replies

7. UNIX for Dummies Questions & Answers

javascript onClick help

I have two radio buttons with corresponding text boxes for input, when one is chosen I am disabling the text box for the other, is there a way to "gray out" the disabled text box using background-color or something? Any help is greatly appreciated, right now my onClick looks like this: ... (1 Reply)
Discussion started by: k@ssidy
1 Replies
Login or Register to Ask a Question