alert(5.30/0.1);
This gives 52.99999999999999
but should be 53
. Can anybody tell how and why?
I want to find that a number is divisible by a given number. Note that one of the number may be a float.
alert(5.30/0.1);
This gives 52.99999999999999
but should be 53
. Can anybody tell how and why?
I want to find that a number is divisible by a given number. Note that one of the number may be a float.
For the same reason that
0.1 * 0.2 //0.020000000000000004
Some decimal numbers can't be represented in IEEE 754, the mathematical representation used by JavaScript. If you want to perform arithmetic with these numbers in your question, it would be better to multiply them until they are whole numbers first, and then divide them.
Scale the numbers to become whole. Then modulus the result.
alert((5.30*10) % (0.1*10));
You can get it by following: var num = (5.30/0.1); alert(num.toFixed(2));
this will give you 53.00.
Now that you have read the article i commented, you should know the root of your problem. You can partially work around that by scaling you floats...
Then just write a function which:
function isDivisable(n, d) {
var ndI = 1 + "".indexOf.call(n, "."); //Index of the Number's Dot
var ddI = 1 + "".indexOf.call(d, "."); // Index of the Divisors Dot
if (ndI || ddI) { // IF its a float
var l = Math.max(("" + n).length - ndI, ("" + d).length - ddI); //Longest Decimal Part
var tmpN = (n * Math.pow(10, l)); //scale the float
var tmpD = (d * Math.pow(10, l));
return !~((tmpN % tmpD) - 1); //Substract one of the modulo result, apply a bitwise NOT and cast a boolean.
}
return !~((n % d) - 1); // If it isnt a decimal, return the result
}
console.log(isDivisable(5.30, 0.1));//true
Heres a JSBin
However...
As Integers are stored with 64bit precision, the maximum precision lies about (2^53), and you will soon exceed the maximum precision when scaling larger numbers.
So it might be a good idea to get some sort of BigInteger Library for javascript if you want to test floats for divisibility
To find if a number x
is divisible by a number y
you have to do x % y
(modulo). If the result is 0, it is perfectly divisible, any other isn't.