How does number at JavaScript works [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 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

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 : 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 ?

Javascript: Subtracting a whole integer from a integer with a decimal alters the decimal value? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Get decimal portion of a number with JavaScript
(29 answers)
Closed 3 years ago.
The goal is to isolate the decimal value (with simply turning the integer into a string and splitting it).
To that affect I have:
var x = 1001.1;
var roundedDown = Math.floor(x); // Produces 1001
var decimal = x - roundedDown;
The expected value of decimal should be 0.1 (1001.1 - 1001). But instead I get 0.0999999 recurring.
I have tried with Math.trunc as well but get the same results.
Would anyone know why this is?

Why is the output of this subtraction wrong? [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 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

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