Multiplication int and float(double) in JavaScript [duplicate] - javascript

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 9 years ago.
I want to make multiplication using JavaScript.
Multiplication of 2 and 0.15 is 0.3 but 3 and 0.15 is 0.44999999999999996. I want to get 0.45 such as result. How can I do it with JavaScript?

It's a rounding error. You may want to round your number to a fixed amount of digits, like this:
parseFloat((your_number).toFixed(2));

Unfortunately this happens in any language using floating point arithmetic. It's an artifact arising when floating point operations are encoded into binary, operations performed, and decoded from binary to report the answer in a form you'd expect.
Depending on what you want to do with the output, you can call a round()-like function to round to a number of decimal places or compare the output to a value using a (small) number called a tolerance. E.g. two numbers are considered equal if their absolute difference is less than this tolerance.

Related

Any way to get JavaScript numbers without Euler's number [duplicate]

This question already has answers here:
How to avoid scientific notation for large numbers in JavaScript?
(27 answers)
Closed 6 years ago.
I try to get
Math.pow(2,1000);
The result is " 1.2676506002282294e+30 "
I need the number without Euler's number "e+30"
That's scientific notation, not Euler's number.
If you want to show the number without the e+NN part:
convert it to a string
parse the e+NN part
shift the decimal place the appropriate number of digits
return the output as a string
be aware that doing so will lead to inaccurate values for some calculations due to how floating point arithmetic works.
With very large numbers, JavaScript displays them in scientific notation. This is because it is very expensive and unreadable to list them.
For your example, it basically means
1.2676506002282294 * 10 ^ 30
You take the number and then multiply it by 10 to the 30th power.
Calculators often use "E" or "e" like this: 1.8004E+94
6E+5 is the same as 6 × 10^5
To get it without this notation, simply use smaller numbers as the exponent.
Example: Math.pow(2,10)
Mathisfun provides an excellent article on scientific notation. Check it out here
https://www.mathsisfun.com/numbers/scientific-notation.html
Euler's number is a constant that is the base of a natural number. It's an irrational number, meaning its digits go on forever. The first couple digits are 2.7182818284

Bug in javascript with multiply [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
Today I have entered random float number and multipled by hundred firstly using normal code and then in console as was giving me wrong number, console is returning me the same.
The given float number is: 1050.6
Therefore: 1050.6 * 100 should be 105060, but javascript is returning me 105059.99999999999
Anyone knows why?
JavaScript uses 64-bit floating point representation (double precision). Numbers are represented in this format as a whole number multiplied by a power of two.
This is based on the IEEE 754 standard
Rational numbers with a denominator that isn't a power of 2 can't be exactly represented. This is why floating point multiplication gives this result.
Source: https://en.wikipedia.org/wiki/IEEE_floating_point
If you want to get the real value, there are two methods you can use
Rounding with Math.round
Math.round(1050.6 * 100)
Or toFixed
(1050.6 * 1000).toFixed(0)
It's a feature of how computers handle floating point numbers. It turns out that decimal numbers can't always be perfectly represented in binary so the computer gives the closest approximation it can.
https://en.wikipedia.org/wiki/IEEE_floating_point

Convert number value to string [duplicate]

This question already has answers here:
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
Closed 8 years ago.
Can anybody solve the following problem with javascript
var i = 10152233307863175;
alert(i.toString());
alert shows value 10152233307863176. Any solution. Problem is when I get json object on client and when string is converted to json it contains wrong values.
This is a limitation in the precision of the numeric data format that javascript uses (double precision floating point).
The best way of storing that value, assuming you don't need to do any mathematical operations, is storing it as a string in the first place.
MDN has this to say about numbers in JavaScript.
Numbers in JavaScript are "double-precision 64-bit format IEEE 754 values", according to the spec.
There is no real integers in JavaScript. According to this source:
ECMAScript numbers are represented in binary as IEEE-754 (IEC 559) Doubles, with a resolution of 53 bits, giving an accuracy of 15-16 decimal digits; integers up to just over 9e15 are precise, ...
Your number 10152233307863175 contains 17 digits. Since the number is represented as a floating point number, JavaScript tries to do it's best and set bits in a way that the resulting number is closest to the supplied number.

4.25.toFixed(1) == 4.35.toFixed(1) == 4.3 but 2.35.toFixed(1)==2.4 [duplicate]

This question already has answers here:
Javascript toFixed function
(6 answers)
Closed 8 years ago.
When the non fractional part is bigger than 4 the fractional part it is truncated to .3 but when it is smaller than 4 it is rounded to .4.
Examples:
1.nr&gt4:
5.35.toFixed(1) => 5.3
15.35.toFixed(1) => 15.3
131.35.toFixed(1) => 131.3
2.nr&lt4:
2.35.toFixed(1) =>2.4
1.35.toFixed(1) =>1.4
Is this kind of behaviour normal?
The problem is that the exact values you're calling toFixed on aren't 1.35 etc... they're the nearest IEEE-754 64-bit representation. In this case, the exact values are:
1.350000000000000088817841970012523233890533447265625
2.350000000000000088817841970012523233890533447265625
5.3499999999999996447286321199499070644378662109375
15.3499999999999996447286321199499070644378662109375
Now look at those values and work out what you'd do in terms of rounding to 1 decimal place.
Basically you're falling foul of the fact that these are floating binary point values, so the value you express in decimal isn't always the value that's actually used. It's just an approximation. In other languages the preferred alternative is to use a type which represents floating decimal point values (e.g. BigDecimal in Java or decimal in C#) but I don't know of anything similar within standard Javascript. You may find some third party libraries though.

JavaScript multiplication arithmetic [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 9 years ago.
I tried this expression in both my FireFox and Chrome console:
17.99 * 100;
Expected Result: 1799
Actual Result: 1798.9999999999998
I also tried:
parseInt(17.99 * 100);
Expected Result: 1799
Actual Result: 1798
Why is this happening, and how do I get the expected result?
Floating point arithmetic isn't an exact science. The reason is that in memory, your precision is stored in binary (all powers of two). If it can't be represented by an exact power of two you can get some lost precision.
Your number, 1798.9999999999998 had enough lost precision that it didn't round up in the multiplication.
http://en.wikipedia.org/wiki/IEEE_floating_point
Try this:
Math.round(17.99*100)
As the previous answer explained, multiplying a float number is not exact science; what you can do is to expect a result in a certain precision range. Take a look at Number.prototype.toPrecision().
(17.99*100).toPrecision(4)

Categories