- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在为我目前正在从事的项目使用 Javascript 做一些数学运算,并且偶然发现了以下内容:
我正在计算结果列表中剩余的结果数量(有一个“加载更多”按钮)
代码如下:
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。
我将其追踪到 javascript 在加法之前执行减法。您可以在此处的 jsfiddle 中重现它: http://jsfiddle.net/JamesPattison/3hvr4vsr/
解决方法是将添加内容括在方括号中,但我想我真正要问的是:
我要疯了吗?
最佳答案
在 JavaScript 中加减法 have the same precedence并且两者都具有从左到右的结合性。因此,使用 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# 和我能想到的所有其他语言中的计算方式。
如果加法有一个比减法更高的优先级,它会以不同的方式分解,就像乘法一样:
16 - 0 * 5 => 16 - (0 * 5)
但事实并非如此。
Re BODMAS,确实应该这样写:
BODMAS
来自 the WikiPedia article on order of operations :
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.
类似地,以下所有引用资料都说在标准算术中,除法和乘法具有相同的优先级,加法和减法具有相同的优先级:
The Order of Operations: PEMDAS - purplemath.com :
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)
The rules are:
- Multiply and divide from left to right.
- Add and subtract from left to right.
Rules of arithmetic - mathcentre.ac.uk :
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 - Javascript 如何处理 BODMAS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30346249/
我以为我理解了BODMAS(英国的BIDMAS)。我不确定为什么以下表达式以不同的顺序求值: a = 8*4//3 我预计“除法”首先发生,给出a = 8*1 - 相反,“乘法”首先发生,给出a =
我以为我理解了BODMAS(英国的BIDMAS)。我不确定为什么以下表达式以不同的顺序求值: a = 8*4//3 我预计“除法”首先发生,给出a = 8*1 - 相反,“乘法”首先发生,给出a =
我正在解决这样的问题 - 实现一个计算由以下内容组成的表达式的函数以下操作数:“(”、“)”、“+”、“-”、“*”、“/”。中的每个数字表达式可能很大(与由字符串表示的一样大)1000 位)。 “/
我正在尝试创建一个遵循 bodmas 规则并首先尝试乘法的计算器。当点击等号按钮时,诸如“4*5+6*7”之类的字符串将存储在 displayValue 中。作为第一次尝试,我使用 reg ex 在字
什么是 BODMAS,为什么它在编程中很有用? 最佳答案 http://www.easymaths.com/What_on_earth_is_Bodmas.htm : What do you thin
我需要找到一种方法,将作为字符串编写的标准算术公式转换为计算格式的另一个字符串,将 BODMAS 实现为一组值和操作,其中每个值和操作都由从左到右读取的管道分隔。 我不想要公式的结果,我正在尝试编写一
我正在尝试重新排列我的计算器数组以逃避 BODMAS 条件。基本上,我读取一个字符串,将数字和运算符添加到不同的数组中,然后尝试重新排列每个数组以使操作具有优先级(在数组中从右到左)。我对“prior
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我一直在为我目前正在从事的项目使用 Javascript 做一些数学运算,并且偶然发现了以下内容: 我正在计算结果列表中剩余的结果数量(有一个“加载更多”按钮) 代码如下: if (data.leng
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我今天发现.NET框架在做计算时遵循BODMAS操作顺序。即计算按以下顺序进行: 括号 订单 部门 乘法 添加 减法 但是我四处搜索并找不到任何文档确认 .NET 绝对 遵循此原则,是否有此类文档?如
在 C/C++ 中计算表达式时,它是否遵循 BODMAS [Bracket open Division Multiply Addition Substraction] 规则?如果不是,那么他们是如何评
我是一名优秀的程序员,十分优秀!