Das Heronverfahren
Berechnen der Quadratwurzel von a
i | ki | ki2 |
// Das Heronverfahren (c)2007 dh
var i = 0;
var j = 0;
function Add0(zahlstr) {
/* sorgt für eine formatierte Ausgabe der Zahlenwerte */
var input = zahlstr;
var dot = false;
var pos = 0;
for (j = 0; j <= input.length; j++) {
if (input.charCodeAt(j) == 46) {dot = true; pos = j}
}
var st = "";
if (dot == true) {
var n0 = 11 - input.length + pos;
for (j = 1; j <= n0; j++) {st = st + "0"}
}
else {
input = input + ".";
for (j = 0; j <= input.length; j++) {
if (input.charCodeAt(j) == 46) pos = j
}
st = "";
var n0 = 11 - input.length + pos;
for (j = 1; j <= n0; j++) {st = st + "0"}
}
return input + st;
}
function CheckInput(inp) {
/* findet fehlerhafte Eingaben */
var input = inp;
var key;
var value = "";
var noValue = false;
var isDot = false;
for (j = 0; j < input.length; j++) {
key = input.charCodeAt(j);
if ((key < 48)||(key > 57)) {
if ((key == 44)||(key == 46)) {
value = value + ".";
if (!isDot) {isDot = true} else {noValue = true}
}
else {noValue = true}
}
else {value = value + String.fromCharCode(key)}
}
if ((noValue)||(value == ".")||(value == "0")) {value = ""}
return value;
}
function Heron() {
/* berechnet näherungsweise die Quadratwurzel aus a */
var inp_k0 = CheckInput(document.f_startwert.e_k0.value);
var inp_a = CheckInput(document.f_startwert.e_a.value);
if ((inp_a == "")||(inp_k0 == "")) {
document.f_startwert.e_k0.value = "";
document.f_startwert.e_a.value = "";
}
else {
document.f_startwert.e_k0.value = inp_k0;
document.f_startwert.e_a.value = inp_a;
var kOld = 1*inp_k0;
var a = 1*inp_a;
var d = 1;
var h = 0;
var kNew = 0;
j = i;
while (j > 0) {
document.getElementById("Tabelle").deleteRow(j);
j--;
}
i = 0;
styleValue = "text−align:right; border:1px solid #04027B; height:16px";
while ((d > 0.0000000001)&&(i < 20)) {
var row = document.getElementById("Tabelle").insertRow(i+1);
h = a/kOld;
kNew = 0.5*(kOld+h);
d = Math.abs(kNew - kOld);
var td1 = document.createElement("td");
td1.setAttribute ("style", styleValue);
var td1Text = i;
var td1Node = document.createTextNode(td1Text);
td1.appendChild(td1Node);
var td2 = document.createElement("td");
td2.setAttribute ("style", styleValue);
var pow = 10000000000;
var td2Text = Add0("" + Math.round(kOld*pow)/pow);
var td2Node = document.createTextNode(td2Text);
td2.appendChild(td2Node);
var td3 = document.createElement("td");
td3.setAttribute ("style", styleValue);
var td3Text = Add0("" + Math.round(kOld*kOld*pow)/pow);
var td3Node = document.createTextNode(td3Text);
td3.appendChild(td3Node);
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
i += 1;
kOld = kNew;
}
}
}
var i = 0;
var j = 0;
function Add0(zahlstr) {
/* sorgt für eine formatierte Ausgabe der Zahlenwerte */
var input = zahlstr;
var dot = false;
var pos = 0;
for (j = 0; j <= input.length; j++) {
if (input.charCodeAt(j) == 46) {dot = true; pos = j}
}
var st = "";
if (dot == true) {
var n0 = 11 - input.length + pos;
for (j = 1; j <= n0; j++) {st = st + "0"}
}
else {
input = input + ".";
for (j = 0; j <= input.length; j++) {
if (input.charCodeAt(j) == 46) pos = j
}
st = "";
var n0 = 11 - input.length + pos;
for (j = 1; j <= n0; j++) {st = st + "0"}
}
return input + st;
}
function CheckInput(inp) {
/* findet fehlerhafte Eingaben */
var input = inp;
var key;
var value = "";
var noValue = false;
var isDot = false;
for (j = 0; j < input.length; j++) {
key = input.charCodeAt(j);
if ((key < 48)||(key > 57)) {
if ((key == 44)||(key == 46)) {
value = value + ".";
if (!isDot) {isDot = true} else {noValue = true}
}
else {noValue = true}
}
else {value = value + String.fromCharCode(key)}
}
if ((noValue)||(value == ".")||(value == "0")) {value = ""}
return value;
}
function Heron() {
/* berechnet näherungsweise die Quadratwurzel aus a */
var inp_k0 = CheckInput(document.f_startwert.e_k0.value);
var inp_a = CheckInput(document.f_startwert.e_a.value);
if ((inp_a == "")||(inp_k0 == "")) {
document.f_startwert.e_k0.value = "";
document.f_startwert.e_a.value = "";
}
else {
document.f_startwert.e_k0.value = inp_k0;
document.f_startwert.e_a.value = inp_a;
var kOld = 1*inp_k0;
var a = 1*inp_a;
var d = 1;
var h = 0;
var kNew = 0;
j = i;
while (j > 0) {
document.getElementById("Tabelle").deleteRow(j);
j--;
}
i = 0;
styleValue = "text−align:right; border:1px solid #04027B; height:16px";
while ((d > 0.0000000001)&&(i < 20)) {
var row = document.getElementById("Tabelle").insertRow(i+1);
h = a/kOld;
kNew = 0.5*(kOld+h);
d = Math.abs(kNew - kOld);
var td1 = document.createElement("td");
td1.setAttribute ("style", styleValue);
var td1Text = i;
var td1Node = document.createTextNode(td1Text);
td1.appendChild(td1Node);
var td2 = document.createElement("td");
td2.setAttribute ("style", styleValue);
var pow = 10000000000;
var td2Text = Add0("" + Math.round(kOld*pow)/pow);
var td2Node = document.createTextNode(td2Text);
td2.appendChild(td2Node);
var td3 = document.createElement("td");
td3.setAttribute ("style", styleValue);
var td3Text = Add0("" + Math.round(kOld*kOld*pow)/pow);
var td3Node = document.createTextNode(td3Text);
td3.appendChild(td3Node);
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
i += 1;
kOld = kNew;
}
}
}