-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrational.html
62 lines (52 loc) · 1.51 KB
/
rational.html
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
58
59
60
61
62
<head>
<title>rational approximation generator</title>
<script>
function go_(){
var rf = 0;
var num = 0;
var num0 = 0;
var den0 = 0;
var num1 = 0;
var den1 = 0;
var num2 = 0;
var den2 = 0;
var str = ""
num = inpt.value
if (num < 1) {
num = 1 / num; rf = 1;
}
else {
rf = 0;
}
num1 = 1;
den1 = 0;
num2 = Math.floor(num);
den2 = 1;
str = ((rf)?den2:num2) + " / " + ((rf)?num2:den2) + "<br>"
for (var i=0; i<no_of.value-1; 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 += ((rf)?den2:num2) + " / " + ((rf)?num2:den2) + "<br>"
}
out.innerHTML = str
}
</script>
</head>
<body>
@ http://wilsonminesco.com/16bitMathTables/RationalApprox.html
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>