-1

I am trying to calculate an angle in js using math js. I am experienced that when the division is between negative numbers js give me bad results. e.g. -6/-3 give me 20093 instead of 2.

How can I solve this? here below you can see a portion of console.log. Console.log

Here is the code:

var num = math.eval(parseInt(p[1]) - parseInt(d3.event.y));
  var den = math.eval(parseInt(p[0]) - parseInt(d3.event.x));
  if (den==0){
    var angle = 0;
  }else{    
    var m = math.eval(num/den);
    if(m<1){
      theta = m*100;
    }else{
      theta = m*100;
    }
  }

Syntax in code is num/den as you can see.

Thanks in advance

Pierpaolo Ercoli
  • 1,028
  • 2
  • 11
  • 32
  • What about using the absolute value? `var m = Math.abs(num) / Math.abs(den);` – Lars Beck May 09 '17 at 09:01
  • 4
    A side question: Accordng the internet math.eval() needs String as an argument. What will happen if you convert your argument to a String? – Reporter May 09 '17 at 09:02
  • 4
    I'm not familiar with _math.js_, so what is `math.eval(num/den)` supposed to do? Why can't you just use `num / den` without `math.eval`? _edit:_ @reporter probably is right. Still I don't understand why you'd use a _math library_ for simple calculations like these. – Luuuud May 09 '17 at 09:02
  • 1
    `-6/-3 give me 20093 instead of 2` - no it doesn't, not in plain javascript and not even `math.eval(-6/-3)` nor even `math.eval("-6/-3")` - your question begins with a flawed argument – Jaromanda X May 09 '17 at 09:03
  • 1
    `here below you can see a portion of console.log` as your code has no console.log in it, I can only assume the code that produces those nonsense results is nothing like the code you've presented in the question – Jaromanda X May 09 '17 at 09:12
  • @LuudJacobs I used Math.js because if I use num/den gives me same bad results. – Pierpaolo Ercoli May 09 '17 at 09:20
  • none of the code as shown requires `math.js` - you work with numbers, not strings of math expressions that need "evaluating" - console.log(-6/-3) logs 2 ... in every browser I've tried - which browser logs 20093? – Jaromanda X May 09 '17 at 09:22
  • Your results are `x * 100 + "93"` so there must be a line in your code that does that. – georg May 09 '17 at 09:28
  • How about using actual angle computations, `theta = Math.atan2(num, den)`, then if necessary convert from radians to degrees. – Lutz Lehmann May 09 '17 at 10:40

1 Answers1

-1

You can simply do -6/-3.

Run this in your developer tools console: alert(-6/-3); and you will see.

I think math.eval() expects a string like "-6/-3".

Reporter
  • 3,897
  • 5
  • 33
  • 47
Orlandster
  • 4,706
  • 2
  • 30
  • 45
  • 1
    if you pass math.evil -6/-3 - you pass it 2 as an argument, which is possibly coerced into a string or perhaps recognised as a Number by mathjs - either way, what math.eval gets as an argument is `2` (with no way of knowing how it got that, so 6/3 should produce the same nonsensical result) ... the nonsense `20093` that the OP claims is probably a result of his **actual** code, not the stripped down code shown in the question :p – Jaromanda X May 09 '17 at 09:18
  • THX, great answer! – Orlandster May 09 '17 at 09:24
  • 1
    Tried to add an edit to correct 'math.evil' to 'math.eval' but edits must be 6 characters long... why is this? – Martha May 09 '17 at 09:26
  • 1
    @Martha I did it for you. Maybe it will be enough, when you add a comment. – Reporter May 09 '17 at 09:41