Advertisement
Guest User

rational approximation generator.html

a guest
Oct 18th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>rational approximation generator</title>
  4. <script>
  5.  
  6. function go_(){
  7.  
  8. var rf = (inpt.value<1)?1:0;
  9. var num = (rf)?1/inpt.value:inpt.value;
  10. var num0 = 0;
  11. var den0 = 0;
  12. var num1 = 1;
  13. var den1 = 0;
  14. var num2 = Math.floor(num);
  15. var den2 = 1;
  16. var str = "";
  17.  
  18. function frac_str(){return((rf)?den2:num2) + " / " + ((rf)?num2:den2) + "<br>";}
  19.  
  20. str = frac_str();
  21. for (var i=1; i<no_of.value; i++){
  22. num0 = num1; den0 = den1; num1 = num2; den1 = den2;
  23. num = 1 / (num - Math.floor(num));
  24. num2 = num1 * Math.floor(num) + num0; den2 = den1 * Math.floor(num) + den0;
  25. str += frac_str();
  26. }
  27. out.innerHTML = str
  28. }
  29. </script>
  30.  
  31. </head>
  32.  
  33. <body>
  34.  
  35. Javascript to generate rational approximations<br>
  36. There's no attempt to sanitize the input or make sure
  37. the output makes sense.<br>
  38. If you put in garbage you get garbage out.<br>
  39. If you put in a rational number you will likely get some garbage,<br>
  40. sooner rather than later in the case of eg integers.<br>
  41. And since you can only input rational numbers ...<br><br>
  42.  
  43. <input id="inpt" type="text" size="16" value="3.14159265359" onchange="go_()"><button type="button" onclick="go_()">go</button>&nbsp;&nbsp;
  44.  
  45. <input type="number" id="no_of" size="4" value = "8" onchange="go_()" min="1" max="99"><br>
  46.  
  47. <div id="out"></div>
  48.  
  49. </body>
  50. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement