-4

Note: I'm not asking about Is floating point math broken? , because I asks about number with integer value+another decimal number with dec instead of decimal number+decimal number.

for example, 10.0+0.1 generates a number with rounding errors, 10.1 generates another number with rounding errors, my question is , does 10.0+0.1 generate SAME amount of error as 10.1 so that 10.0+0.1===10.1 becomes equal to true?

For more example:

10.0+0.123 === 10.123
2.0+4.68===6.68

they are true by testing, and the first number are 10.0 and 2.0, which are integer values. Is it true that an integer + hardcoded float number (same sign) exactly equals to the hardcoded expected float number? Or in other words, does a.0+b.cde exactly equals to (a+b).cde (which a,b,c,d,e are hardcoded)?

ocomfd
  • 4,010
  • 2
  • 10
  • 19
  • 1
    javascript only has one number type. – Daniel A. White Jan 31 '18 at 02:59
  • 1
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Herohtar Jan 31 '18 at 03:03
  • 3
    does `1.0 + 0.973 == 1.973` – Jaromanda X Jan 31 '18 at 03:04
  • 1
    Your statement that you are not asking about [is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) because “I asks about integer+float instead of float+float” shows you have misconceptions about JavaScript. In JavaScript, there is no integer type. All numbers are the Number type, which follows the IEEE 754-2008 64-bit binary format. Numbers can have integer values, but they are floating-point entities. And your title refers to “hardcoded… decimal.” Every decimal numeral in the source is immediately converted to binary floating-point. – Eric Postpischil Jan 31 '18 at 14:36
  • So it is not possible in JavaScript to add an integer to a “decimal number” except for decimal numbers that happen to be exactly representable in floating-point. Every addition will add one floating-point value to another floating-point value, and the addition will follow the rules of floating-point. If you rephrase the question as whether adding an integer value to a non-integer value is always exact, the answer is no, as shown in my answer below. – Eric Postpischil Jan 31 '18 at 14:38

2 Answers2

1

No. JavaScript only has floats. Here's one case that fails.

10000.333333333333 + 1.0 // 10001.333333333332
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 1
    but 10000.333333333333+1.0===10001.333333333333 is true by my testing – ocomfd Jan 31 '18 at 03:05
  • 1
    @Ipad1gs its because its losing precision. – Daniel A. White Jan 31 '18 at 03:07
  • 1
    What is the point or claim you are making in this answer? The reason that `10000.333333333333 + 1.0` is not equal to 10001.333333333333 is that when `10000.333333333333` is converted to floating-point, there is some rounding error, producing 10000.33333333333212067373096942901611328125. In the actual addition of 1, there is no rounding error; the computed result of the addition equals the exact mathematical result of the addition. So this is not a counterexample for the OP’s question. – Eric Postpischil Jan 31 '18 at 13:03
1

It is not generally true that adding an integer value to a floating-point value produces a result equal to the exact mathematical result. A counterexample is that 10 + .274 === 10.274 evaluates to false.

You should understand that in 10.0+0.123 === 10.123, you are not comparing the result of adding .123 to 10 to 10.123. What this code does is:

  • Convert “10.0” to binary floating-point, yielding 10.
  • Convert “0.123” to binary floating-point, yielding 0.1229999999999999982236431605997495353221893310546875.
  • Add the above two, yielding 10.1229999999999993320898283855058252811431884765625. (Note this result is not the exact sum; it has been rounded to the nearest representable value.)
  • Convert “10.123” to binary floating-point, yielding 10.1229999999999993320898283855058252811431884765625.
  • Compare the latter two values.

Thus, the reason the comparison returns true is not because the addition had no rounding error but because the rounding errors on the left happened to equal the rounding errors on the right. (Note: Converting a string containing a decimal to binary floating-point is a mathematical operation. When the mathematical result is not exactly representable, the nearest representable value is produced instead. The difference is called rounding error.)

If you try 10 + .274 === 10.274, you will find they differ:

  • “10” converted to binary floating-point is 10.
  • “.274” converted to binary floating-point is 0.27400000000000002131628207280300557613372802734375.
  • Adding the above two produces 10.2740000000000009094947017729282379150390625.
  • “10.274” converted to binary-floating-point is 10.2739999999999991331378623726777732372283935546875.
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312