Dzień dobry jestem początkująca.
Zrobiłam kalkulator, który przelicza stopnie alfa na sinus alfa, cosinus alfa, tangens alfa. Przy wprowadzeniu liczbę 90 stopni przez użytkownika wynik tangensa powinien pokazać ∞ lub infity lub w miejsce wyniku nic nie powinno być (wynik nie istnieje). Natomiast mi I sposobie wynik pokazuje błędny 16331239353195370 = 16331239353195370.0000 tangens alfa, a w drugim sposobie -1.995200412208242 = -1.9952. Jeśli użytkownik wprowadzi liczbę 45 stopni to wynik pokazuje poprawny 1 = 1.0000 .
Co zrobić aby po wprowadzeniu liczby 90 stopni pokazał się wynik infinity?
Proszę o pomoc i wskazanie gdzie jest błąd.
HTML
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="stopien_sinus.js"></script>
</head>
<body>
<form id="oblicz" action="" method="post" autocomplete="off">
<div>
<h3>Przelicznik Stopnia Alfa na Sinus Alfa</h3>
Stopień Alfa <input type="number" id="SA"><br>
Sinus: <span id="SIN">= </span><br><br>
Cosinus: <span id="KOS">= </span><br><br>
Tangens: <span id="TAN">= </span><br><br>
<input type="button" value="Oblicz" onclick="myFunction()">
<input type="reset" value="RESETUJ" onclick="resetuj()">
</div>
</form>
</body>
</html>
I sposób
function myFunction() {
const SA = parseInt(document.getElementById("SA").value);
const SIN = Math.sin(parseInt(SA) * Math.PI / 180);
const KOS = Math.round(Math.cos(parseInt(SA) * Math.PI / 180));
const TAN = Math.round(Math.tan(parseInt(SA) * Math.PI / 180));
document.getElementById("SIN").innerHTML = Math.sin(SA * Math.PI / 180) + ' = ' + SIN.toFixed(4);
document.getElementById("KOS").innerHTML = Math.round(Math.cos(SA * Math.PI / 180)) + ' = ' + KOS.toFixed(4);
document.getElementById("TAN").innerHTML = Math.round(Math.tan(SA * Math.PI / 180)) + ' = ' + TAN.toFixed(4);
return false;
}
function resetuj () {
document.getElementById("SIN").innerHTML = '';
document.getElementById("KOS").innerHTML = '';
document.getElementById("TAN").innerHTML = '';
}
II sposób
function myFunction() {
const SA = parseInt(document.getElementById("SA").value);
const SIN = Math.sin(parseInt(SA) * Math.PI / 180);
const KOS = Math.cos(parseInt(SA) * Math.PI / 180);
const TAN = Math.sin(parseInt(SA)) / Math.cos(parseInt(SA));
document.getElementById("SIN").innerHTML = Math.sin(SA * Math.PI / 180) + ' = ' + SIN.toFixed(4);
document.getElementById("KOS").innerHTML = Math.cos(SA * Math.PI / 180) + ' = ' + KOS.toFixed(4);
document.getElementById("TAN").innerHTML = Math.sin(SA ) / Math.cos(SA ) + ' = ' + TAN.toFixed(4);
return false;
}
function resetuj () {
document.getElementById("SIN").innerHTML = '';
document.getElementById("KOS").innerHTML = '';
document.getElementById("TAN").innerHTML = '';
}