Jump to content

Help in formula on javascript


Recommended Posts

I managed to understand some things, but when i try to multiply 9.999 * 9.999 it gives 99.98000100000002,

when it should be 99.980001, this case and any number should be precise.

What's wrong in the program?

 

Here is the code:

 

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="ohmslaw.css">
<meta charset="utf-8" />
<title>Ohm's law</title>
</head>

<body>

<h1>Ohm's Law Calculator</h1>

<form action="" id="volt">
<fieldset>
<legend>V=R*I</legend>
<p>
<label for="ohm">Ohm ®</label>
<input type="text" name="ohm"/>
<label for="ampere">Ampere (I)</label>
<input type="text" name="ampere"/>
</p>
<p>
<input type="submit" value="Calculate"/>
or
<input type="reset" value="Reset"/>
</p>
<p>
<label for="volt">Volt (V)</label>
<input type="text" name="volt"/>
</p>
</fieldset>
</form>

<script>
(function () {
function calculateVolt(ohm,ampere) {
ohm = parseFloat(ohm);
ampere = parseFloat(ampere);
return (ohm*ampere);


}

var volt = document.getElementById("volt");
if (volt) {
volt.onsubmit = function () {
this.volt.value = calculateVolt(this.ohm.value, this.ampere.value);
return false;
};
}
}());
</script>

Link to post
Share on other sites

Specify the decimal number using toFixed():

 

var dec= 9.999 * 9.999;

var specdec = dec.toFixed(6)
console.log( specdec ); //99.980001

 

but the value will be a string.

Edited by musicman
Link to post
Share on other sites

That's just how it is with computers, they aren't perfect with floating-point math. Computers are binary, not base-10, so there are going to be precision errors with floating-point numbers when things get converted. There's an article about it here, which includes some links to libraries that are specifically for handling floating-point math.

 

http://modernweb.com/2014/02/17/what-every-javascript-developer-should-know-about-floating-points/

Link to post
Share on other sites

You are mixing id referencing, with name attribute dot notation referencing, and I suspect with some browsers they will make correct assumption what you want, but some will not.

 

'for' attribute value is supposed to refer to id ref of input directly following it.

 

Example of how it should be

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="ohmslaw.css">
<meta charset="utf-8" />
<title>Ohm's law</title>
</head>

<body>

<h1>Ohm's Law Calculator</h1>

<form action="" id="voltform">
<fieldset>
    <legend>V=R*I</legend>
    <p>
        <label for="ohm">Ohm ®</label>
        <input type="text" name="ohm" id="ohm"/>
        <label for="ampere">Ampere (I)</label>
        <input type="text" name="ampere" id="ampere"/>
    </p>
    <p>
        <input type="submit" value="Calculate"/>
        or
        <input type="reset" value="Reset"/>
    </p>
    <p>
        <label for="volt">Volt (V)</label>
        <input type="text" id="volt" name="volt"/>
    </p>
</fieldset>
</form>

<script>
(function () {
    function calculateVolt(ohm,ampere) {
        ohm = parseFloat(ohm);
        ampere = parseFloat(ampere);
        return (ohm*ampere);
        
    
    }

    var voltform = document.getElementById("voltform");
var volt = document.getElementById("volt");

var ohm = document.getElementById("ohm");

var ampere = document.getElementById("ampere");

    if (volt) {
        voltform.onsubmit = function () {
            volt.value = calculateVolt(ohm.value,ampere.value).toFixed(6);
            return false;
        };
    }
}());
</script>
</body>
</html>
Edited by dsonesuk
Link to post
Share on other sites

In this program it happens that when i calculate volt (V) it works fine,

 

But when i want to calculate ohm ® in the equation bellow nothing happens.

 

I also want another equation for calculate ampere (I) in the same file.

 

Hope you could help me

 

here is the code:

 

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="ohmslaw.css">
<meta charset="utf-8" />
<title>Ohm's law</title>
</head>

<body>

<h1>Ohm's Law Calculator</h1>

<form action="" id="voltform">
<fieldset>
<legend>V=R*I</legend>
<p>
<label for="ohm">Ohm ®</label>
<input type="text" name="ohm" id="ohm"/>
<label for="ampere">Ampere (I)</label>
<input type="text" name="ampere" id="ampere"/>
</p>
<p>
<input type="submit" value="Calculate"/>
or
<input type="reset" value="Reset"/>
</p>
<p>
<label for="volt">Volt (V)</label>
<input type="text" id="volt" name="volt"/>
</p>
</fieldset>
</form>

<script>
(function () {
function calculateVolt(ohm,ampere) {
ohm = parseFloat(ohm);
ampere = parseFloat(ampere);
return (ohm*ampere);


}

var voltform = document.getElementById("voltform");
var volt = document.getElementById("volt");

var ohm = document.getElementById("ohm");

var ampere = document.getElementById("ampere");

if (volt) {
voltform.onsubmit = function () {
volt.value = calculateVolt(ohm.value,ampere.value).toFixed(6);
return false;
};
}
}());
</script>

<form action="" id="ohmform">
<fieldset>
<legend>R=V/I</legend>
<p>
<label for="volt">Volt (V)</label>
<input type="text" name="volt" id="volt"/>
<label for="ampere">Ampere (I)</label>
<input type="text" name="ampere" id="ampere"/>
</p>
<p>
<input type="submit" value="Calculate"/>
or
<input type="reset" value="Reset"/>
</p>
<p>
<label for="ohm">Ohm ®</label>
<input type="text" id="ohm" name="ohm"/>
</p>
</fieldset>
</form>

<script>
(function () {
function calculateOhm(volt,ampere) {
volt = parseFloat(volt);
ampere = parseFloat(ampere);
return (volt/ampere);


}

var ohmform = document.getElementById("ohmform");
var ohm = document.getElementById("ohm");

var volt = document.getElementById("volt");

var ampere = document.getElementById("ampere");

if (ohm) {
ohmform.onsubmit = function () {
ohm.value = calculateOhm(volt.value,ampere.value).toFixed(6);
return false;
};
}
}());
</script>

</body>
</html>
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...