Why is the output of this subtraction wrong? [duplicate] - javascript

This question already has answers here:
Is floating point math broken?
(31 answers)
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 4 years ago.
Why is the output of this subtraction wrong?
var a = 1753.10
var b = 87.66
var c = a - b
console.log(c);
The given output is 1665.4399... but the calculator given output is 1665.44

Related

javascript bitwise and confusion [duplicate]

This question already has answers here:
bitwise AND in Javascript with a 64 bit integer
(6 answers)
Javascript Decimal to Binary - 64 bit
(2 answers)
Closed 1 year ago.
Code like this:
let a = 3232237057
let b = 4294967040
let c = a & b
console.log(a.toString(2))
console.log(b.toString(2))
console.log(c.toString(2))
Output is:
11000000101010000000011000000001
11111111111111111111111100000000
-111111010101111111101000000000
I have expected output like:
11000000101010000000011000000001
11111111111111111111111100000000
11000000101010000000011000000000
What is wrong?

Javascript wrong calculations & rounding numbers [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 2 years ago.
My code:
$(document).ready(function() {
var num1 = 1473.735;
var num2 = 1473;
var num3 = num1 - num2;
alert(num3); // 0.7349999999999
}
Javascript seems to round my numbers very strangely.
How is it possible that javascript comes up with a strange decimal notation with such a simple calculation?
1473.735 - 1473 = 0.735, and not 0.7349999999999.
Here my Sandbox example: https://codepen.io/thomasveld/pen/yLebMMo
thanks in advance.

Javascript : Addition of 2 numbers always give = 1 [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 3 years ago.
I'm trying to make an addition of the 2 following numbers:
v1 = 0.005919725632510221
v2 = 0.9940802743674898
v1+v2
Result always = 1
Why ?

How does number at JavaScript works [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 3 years ago.
I have this code in javascript
let a = 1;
let b = 0.8;
let c = a-b
I expect the value of c to be equal to 0.2, but the value I get is 0.19999999999999996
does any one have an explanation

Incorrect results in calculations with decimal values with four digits after dot [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
I have a JavaScript function that returns the result of multiplications using decimal values (four digits after dot).
But, in some conditions, the result is a mess like this:
3.9050 * 9 = 35.144999999999996.
What should I do to normalize those results?
Use .toFixed(numberOfDecsYouWant)
var num = 3.9050 * 9;
console.log(num); //35.144999999999996
console.log(num.toFixed(4)) //35.1450

Categories