• API
• FAQ
• Tools
• Archive
Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

# rational approximation generator.html

a guest Oct 18th, 2013 131 Never
ENDING IN00days00hours00mins00secs

1. <html>
3. <title>rational approximation generator</title>
4. <script>
5.
6.  function go_(){
7.
8.  var rf = 0;
9.  var num = 0;
10.  var num0 = 0;
11.  var den0 = 0;
12.  var num1 = 0;
13.  var den1 = 0;
14.  var num2 = 0;
15.  var den2 = 0;
16.  var str = ""
17.
18.
19.  num = inpt.value
20.  if (num < 1){num = 1 / num; rf = 1;} else {rf = 0;}
21.  num1 = 1; den1 = 0; num2 = Math.floor(num); den2 = 1;
22.  str = ((rf)?den2:num2) + " / " + ((rf)?num2:den2) + "<br>"
23.  for (var i=0; i<no_of.value-1; i++){
24.  num0 = num1; den0 = den1; num1 = num2; den1 = den2;
25.  num = 1 / (num - Math.floor(num));
26.  num2 = num1 * Math.floor(num) + num0; den2 = den1 * Math.floor(num) + den0;
27.  str += ((rf)?den2:num2) + " / " + ((rf)?num2:den2) + "<br>"
28.  }
29.  out.innerHTML = str
30.  }
31. </script>
32.
34.
35. <body>
36.
37. Javascript to generate rational approximations<br>
38. There's no attempt to sanitize the input or make sure
39. the output makes sense.<br>
40. If you put in garbage you get garbage out.<br>
41. If you put in a rational number you will likely get some garbage,<br>
42. sooner rather than later in the case of eg integers.<br>
43. And since you can only input rational numbers ...<br><br>
44. <input id="inpt" type="text" size="16" value="3.14159265359" onchange="go_()"><button type="button" onclick="go_()">go</button>&nbsp;&nbsp;<input type="number" id="no_of" size="4" value = "8" onchange="go_()" min="1" max="99"><br>
45. <div id="out"></div>
46.
47. </body>
48. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top