Strange result in modulo calculation [duplicate] - javascript

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
-224 / -37.333333333333336 = 6
-224 % -37.333333333333336 = -37.33333333333332
(-224 / -37.333333333333336) % 1 = 0
Why doesn't % return 0 if the first result is 6?
And why isn't the result a float in the third calculation?

All numbers in JavaScript are 64-bit floating point numbers. Just because it doesn't display the number as 0.0 doesn't mean it's not a double.
According to WolframAlpha:
-224 / -37.333333333333336 = 5.9999999999999995714285...
Due to accuracy limitations / rounding in floating point division, the division results in 6 in JavaScript.
The modulo operation, however, correctly sees that -37.3… doesn't quite fit into -224 six times. That means that the other 99.99…% of -37.33… is a leftover after the division, which corresponds to result of the modulo operation.
Hence, you get almost, but not quite, the divisor back.

Related

Javascipt decimal multiplication and division output [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Why in the output, the length of decimals differ in these two scenarios?
33 * .1 = 3.3000000000000003
where as
33 * .01 = 0.33
Any idea why its like that?
NB: 33 in the above calculation can be any integer
When you convert .1 or 1/10 to base 2 (binary) you get a repeating
pattern after the decimal point, just like trying to represent 1/3 in
base 10. The value is not exact, and therefore you can't do exact math
with it using normal floating point methods.
This is the reason why you end up answer like above.

Javascript - Limit the number to no decimals [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I'm using following Javascript code in one of my calculations:
var bx1 = parseFloat(tx9) * ( 70 / 100 );
The output of bx1 is showing in decimals. However, I want it to be no decimals. Looking for some help!
Eg. bx1 = 700*70/100 = 489.999. I want it either 489 or 490, anything is fine.
Use Math.round().
var = Math.round(parseFloat(tx9) * ( 70 / 100 ));
You can use either Math.round(), Math.floor() or Math.ceil():
var bx1 = 489.999;
console.log(Math.floor(bx1));
console.log(Math.round(bx1));
console.log(Math.ceil(bx1));
floor() will round down your number.
round() will round your number (based on decimal value).
ceil() will round up your number.

Problems with parseInt method [duplicate]

This question already has answers here:
parseInt rounds incorrectly
(2 answers)
Closed 7 years ago.
The parseInt function made my number loose precision: the last two digits changed from 18 to 20:
console.log(parseInt('76561198236425518', 10));
76561198236425520
Why did that happen and how to fix it?
Numbers are stored as floating point with a 53-bit manttissa. This limits the precision you can have to less than what you have in that number of yours, hence it has to round to the nearest floating point number it can represent.
The actual number of bits needed to represent a number N can be calculated as about log2N or, if you're working on a calculator that can't calculate logarithms to base two, logxN/logx2.
The value of log276561198236425518, roughly 56.1, shows that it requires about 56 bits, which is why it's not exact near the end.

javascript math result lesser than 0 gets 0.0000000000 [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
Why does javascript returns so many zeros and not just 0.24
And how can i disable javascript to do this. Because when im using a calculator i never get the result 0.24000000000002
var sum = (0.0001 * 2400);
result 0.2400000000002
The reason for this is that your sum is a float which are known to not be very precise. This is a limitation of float values.
To fix this you need to round the decimals by either Math.round or .toFixed.
javascript always do that but you can make it show only 2 digits after the dot.
var sum = (0.0001 * 2400);
alert(sum.toFixed(2));

strange Javascript Float division result [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
var coeff='0.1';
var amount='12.2';
var res = Math.floor(parseFloat(amount) / parseFloat(coeff));
console.log(res);
Why the result of this is 121 (I was expecting 122)?
EDIT: my question was ambiguous: no trouble with the floor function. I was just wondering why 12.2 / 0.1 is not equal to 122.
the result is 121.99999999999999
from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
The Math.floor() function returns the largest integer less than or equal to a given number.
so it cuts off the .99999. you might want to use
Math.round(parseFloat(amount) / parseFloat(coeff));
instead

Categories

Resources