-2

While I was working on Collatz Conjecture, I seen that there was a starting point 1.001298642*10^9 where I were getting 1047 iterations to reach 1. And against these iterations my matlab programming was returning 1001298642 in a string which is 1.001298642*10^9. But when I further work and tested on Matlab

>> 1.001298642*10^9 == 1001298642
ans =

  logical

    0

which means 1.001298642*10^9 is not equal to 1001298642. But actually both values are same.

I also tested these values on R Studio and same result I got. What is the problem. Am I doing some mistake?

Syed Muhammad Asad
  • 208
  • 1
  • 2
  • 13
  • 2
    Can't say for the others, but Javascript is returning true because `1.001298642*10^9 == 1001298642` evaluates to `1.001298642*10^(9 == 1001298642)`, which evaluates to `10` (which is truthy), and `^` is a bitwise operator, not exponentiation – CertainPerformance Sep 15 '19 at 10:36
  • 1
    Even if you use the correct operator (`**` instead of `^`), it will still return false: [Is floating point math broken?](https://stackoverflow.com/questions/588004) – adiga Sep 15 '19 at 10:38
  • What you guys are saying, I not understand... I check on R Studio and 001298642*10^9 = 1001298642. But 001298642*10^9 == 1001298642 returning false. – Syed Muhammad Asad Sep 15 '19 at 10:39
  • because 10^9 is 3 – Jaromanda X Sep 15 '19 at 10:39
  • `10^9` should be `10 ** 9`. In javascript, `^` is [Bitwise XOR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#(Bitwise_XOR)) – adiga Sep 15 '19 at 10:39
  • 2
    Matlab is returning false because of floating point representation, see e.g. [Why is 24.0000 not equal to 24.0000 in MATLAB?](https://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab) – rinkert Sep 15 '19 at 10:40
  • and `1.001298642*10**9` is `1001298642.0000001` - due to floating point being inprecise – Jaromanda X Sep 15 '19 at 10:40
  • @adiga Ok, I edit it. – Syed Muhammad Asad Sep 15 '19 at 10:41
  • So, guys what is the solution of this? How I can get out of it? – Syed Muhammad Asad Sep 15 '19 at 10:45
  • Round the value you want to compare to an integer, or compare if they are within a certain error margin. But read the links that were posted in the comments, since they would have told you this as well. – rinkert Sep 15 '19 at 10:49

1 Answers1

0

You are using the wrong operator here. ^ is Bitwise XOR. You should be using ** for Exponentiation opeartor

Note that, this will return false because 1.001298642 * 10 ** 9 returns 1001298642.0000001 and not 1001298642 (More on that here: Is floating point math broken?)

console.log(1.001298642 * 10 ** 9 == 1001298642)
console.log(1.001298642 * 10 ** 9)

But, it is interesting that the snippet returns true. 1.001298642*10^9 == 1001298642 enters the if block because the expression returns 10 which is a truthy value:

console.log(1.001298642*10^9 == 1001298642)

In the Operator precedence table, Equality operator(==) appears before Bitwise XOR (^). If you check the the expression in AST Explorer, it is grouped like this:

(1.001298642*10)^(9 == 1001298642)
10.01298642 ^ false
10.01298642 ^ 0 // coerced to zero
adiga
  • 34,372
  • 9
  • 61
  • 83