Why does this account return a negative number?

Asked

Viewed 81 times

-1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        console.log(1 - 3 * 2);
    </script>
</body>
</html>

I’m confused in the code above returning me a negative and not positive number because it follows my logic was not for him to do multiplication first, since in this case multiplication comes first, so it was not for him to do 3 * 2 that will give 6 and then subtract 1 that will give 5? and even putting the parentheses in order to force it to do the multiplication first and then subtract it keeps returning negative number:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        console.log(1 - (3 * 2));
    </script>
</body>
</html>

  • 1

    Instead of trying to make self-contained whole HTML files to run a single line of program, there is an interactive Javascript prompt in the browsers - exactly n console where you see the output of "console.log". I suggest you play a little with mathematical expressions and string, as well as other basic structures, before trying to make whole programs - understanding happens much easier.

  • 2

    I voted to close because it’s a purely mathematical question, not a programming question. If you disagree, please make your argument here: https://pt.meta.stackoverflow.com/q/7731/112052

  • 1

    @hkotsubo I voted to close based on the mistaken interpretation of commutativity of subtraction in Reals. But what he put, to close because it is a purely mathematical question, is well-founded because to give an exact and adequate answer to this question would require an extensive algebraic demonstration on homomorphism and commutative bodies that goes far beyond the scope of the site

  • 1

    When you multiply 3 * 2 will give 6, but you’re taking 6 of 1 just imagine a horizontal line with positive and negative numbers -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 when you subtract 6 of 1 He’s gonna fall right into -6 in which this will be the result of the account, to be able to return a positive number just do the opposite 6 * 2 - 1.

  • @Augusto Vasques, what is algebraic?

  • @hkotsubo, not quiet!

  • @ledeveloper,algebraic is derivative of Algebra, the field of mathematics that studies the formalities of numerical groupings such as sets, rings, vector spaces, magmas, monoids,... and the transformations applicable to these fields.

Show 2 more comments

3 answers

4


In the algorithm, he’s understanding that Voce is doing the expression

"+ 1 - 3 * 2"

First thing that is done is multiplication, on account of the natural order of operations

"- 3 * 2"

resulting in - 6

Then he executes the second part

"+ 1 - 6"

Which results in - 5

Notice that when the order of the numbers is exchanged the expression works

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        var res = 3 * 2 - 1;
        console.log(res);
    </script>
</body>
</html>

This occurs by the polarization of signals.

  • Good explanation!

  • I just didn’t get that part "+ 1 - 3 * 2".

  • It is that there is no signal, but the program considers that if there is no signal, then it is positive or "+".

3

Apparently all right with the expression

console.log(1 - 3 * 2);
[1 - ]
[3 * 2] => 3 * 2 => 6
1 - 6
-5

you can think of her like this: (1 - (3*2))
Order of precedence of mathematical operations:
1. Anything between parentheses, keys or brackets;
2. Multiplications and/or Divisions;
3. Sums and/or subtractions.

  • 1

    Welcome Pixies, your answer might be useful, however how about explaining it better ?

  • done! ;) still new here!!

2

The reason is simpler that it seems, there is nothing wrong in the account, it actually multiplies first having the value of 6, however, then becomes like 1 - 6 which is equal to -5

I have 100 real and bag 600 real removing from my limit, my balance would then -500 real # Exemplo

1 - 3 * 2
1 - 6
-5

the correct would be left so to return the positive value

3 * 2 - 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.