Javascript如何处理BODMAS? [英] How does Javascript handle BODMAS?

查看:103
本文介绍了Javascript如何处理BODMAS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为当前正在研究的项目使用Javascript做一些数学运算,但偶然发现了以下内容:

I've been doing a bit of math with Javascript for a project I'm currently working on, and have just stumbled across the following:

我正在计算结果列表中剩余的结果量(有一个加载更多"按钮)

I'm calculating an amount of results remaining in a list of results (there's a "Load More" button)

代码如下:

if (data.length > startIndex + maxLoadAmount) {
    var loadMoreButton = new LoadButton()
    loadButton.ToLoad = data.length - startIndex + maxLoadAmount;

    loadMoreButton.click(function () {
    var amountOfCurrentWidgets = $(this).parent().children('.widget').length;
    this.remove();
        loadChildren(amountOfCurrentWidgets, selector, data, widgetType);
    });
    $(selector).append(loadMoreButton);
}

我对结果感到惊讶!

在特定测试中,有16个可用的小部件,并且从索引0开始(最多显示5)时,结果为21.

In a particular test, there are 16 available widgets, and when starting at index 0 with a maximum of 5 displayed, the result is 21.

在添加之前,我一直跟踪到javascript执行减法运算. 您可以在jsfiddle中将其复制: http://jsfiddle.net/JamesPattison/3hvr4vsr/

I tracked this down to javascript performing the subtraction before the addition. You can reproduce it in jsfiddle here: http://jsfiddle.net/JamesPattison/3hvr4vsr/

解决方法是将加法内容括在方括号中,尽管我想我真正要问的是:

The workaround is to surround the addition in brackets, though I guess what i'm really asking is:

我疯了吗?

推荐答案

在JavaScript中添加和减去

In JavaScript addition and subtraction have the same precedence and both have left-to-right associativity. So with the code in your fiddle:

var x = 16, y = 0, z = 5;
var result = x-y+z; //16-0+5, expected by BODMAS: 16-(0+5) = 11
$('#results').html(result.toString())

result的计算方式如下:

(16 - 0) + 5

这就是用JavaScript,Java,C,C ++,C#和我能想到的所有其他语言对其进行评估的方式.

That's how it's evaluated in JavaScript, Java, C, C++, C#, and every other language I can think of.

将加法分解的唯一方法是,加法的优先级比减法的,就像乘法一样:

The only way it would be broken up differently would be if addition had a higher precedence than subtraction, like multiplication does:


16 - 0 * 5 => 16 - (0 * 5)

但事实并非如此.

关于BODMAS,确实应该这样写:

Re BODMAS, it really should be written:


B
O
DM
AS

来自有关操作顺序的WikiPedia文章:

以这种方式编写时,这些助记符可能会产生误导,特别是如果用户不知道乘法和除法的优先级相同,加法和减法也是如此.

These mnemonics may be misleading when written this way, especially if the user is not aware that multiplication and division are of equal precedence, as are addition and subtraction.

类似地,以下所有参考文献都说在标准算术中,除法和乘法具有相同的优先级,加法和减法具有相同的优先级:

Similarly, all of the following references say that in standard arithmetic, division and multiplication have the same precedence, and addition and subtraction have the same precedence:

操作顺序:PEMDAS -purplemath.com :

The Order of Operations: PEMDAS - purplemath.com:

记住操作顺序的常用技术是缩写"PEMDAS",它变成短语请打扰我亲爱的萨莉姨妈".它代表括号,指数,乘法和除法以及加法和减法".这将告诉您操作的等级:括号的指数高于乘数和除法(但乘积和除法的等级相同),而这两个指数的乘积加法和减法(即在一起排在最底)

A common technique for remembering the order of operations is the abbreviation "PEMDAS", which is turned into the phrase "Please Excuse My Dear Aunt Sally". It stands for "Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction". This tells you the ranks of the operations: Parentheses outrank exponents, which outrank multiplication and division (but multiplication and division are at the same rank), and these two outrank addition and subtraction (which are together on the bottom rank)

数学步骤-eduplace.com :

Math Steps - eduplace.com:

规则是:

  1. 乘以并从左向右划分.
  2. 从左到右加减.

算术规则-mathcentre.ac .uk :

Rules of arithmetic - mathcentre.ac.uk:

这很难记住,因此这里是一个缩写词,可以帮助您-BODMAS.这意味着:

This is hard to remember so here’s an acronym to help you — BODMAS. It means:

除法和乘法具有相同的优先级,加法和减法具有相同的优先级,因此在每种情况下,我们将它们放在一起.

where division and multiplication have the same priority, and also addition and subtraction have the same priority, so in each case we have bracketed them together.

这篇关于Javascript如何处理BODMAS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆