Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>rational approximation generator</title>
- <script>
- function go_(){
- var rf = (inpt.value<1)?1:0;
- var num = (rf)?1/inpt.value:inpt.value;
- var num0 = 0;
- var den0 = 0;
- var num1 = 1;
- var den1 = 0;
- var num2 = Math.floor(num);
- var den2 = 1;
- var str = "";
- function frac_str(){return((rf)?den2:num2) + " / " + ((rf)?num2:den2) + "<br>";}
- str = frac_str();
- for (var i=1; i<no_of.value; i++){
- num0 = num1; den0 = den1; num1 = num2; den1 = den2;
- num = 1 / (num - Math.floor(num));
- num2 = num1 * Math.floor(num) + num0; den2 = den1 * Math.floor(num) + den0;
- str += frac_str();
- }
- out.innerHTML = str
- }
- </script>
- </head>
- <body>
- Javascript to generate rational approximations<br>
- There's no attempt to sanitize the input or make sure
- the output makes sense.<br>
- If you put in garbage you get garbage out.<br>
- If you put in a rational number you will likely get some garbage,<br>
- sooner rather than later in the case of eg integers.<br>
- And since you can only input rational numbers ...<br><br>
- <input id="inpt" type="text" size="16" value="3.14159265359" onchange="go_()"><button type="button" onclick="go_()">go</button>
- <input type="number" id="no_of" size="4" value = "8" onchange="go_()" min="1" max="99"><br>
- <div id="out"></div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement