问题标签 [integer-arithmetic]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
36 回答
28483 浏览

php - 在解释语言上处理非常大的整数时出现意外结果

我试图得到 的总和1 + 2 + ... + 1000000000,但我在 PHP 和Node.js中得到了有趣的结果。

PHP

节点.js

正确答案可以使用

正确答案 = 500000000500000000,所以我决定尝试另一种语言。

但它工作正常!那么我的 PHP 和 Node.js 代码有什么问题呢?

也许这是解释语言的问题,这就是为什么它可以在像 Go 这样的编译语言中工作?如果是这样,其他解释语言(如 Python 和 Perl)会不会有同样的问题?

0 投票
2 回答
308 浏览

c - 大于 ULLONG_MAX 的数值

我需要存储和使用大于ULLONG_MAX.

我需要对这些值进行算术运算,所以我认为存储char**不是一个选项。

在这些情况下,有没有办法动态创建额外的long前缀?


谢谢你们。根据回复,非常有帮助,但我怀疑最佳性能选项。piokuc 引用了一个性能链接,但不清楚。在这个时候我在GMP或MPIR之间,有一些更快?

0 投票
2 回答
3115 浏览

64-bit - 如何在 16 位机器上做 64 位乘法?

我有一个嵌入式 16 位 CPU。在这台机器上,整数是 16 位宽的,它支持 32 位宽的长整数。我需要做一些需要以 64 位存储的乘法运算(例如,将 32 位数乘以 16 位数)。我怎样才能在给定的约束下做到这一点?我没有数学库来做到这一点。

0 投票
1 回答
153 浏览

cpu - 整数乘数在物理上是整数单位还是特殊功能单位?

我正在使用 McPAT,这是一种估计 CPU 功率的工具,但是,整数乘数似乎被视为一个特殊功能单元。这是为什么?它不应该是整数单位吗?特殊函数单元不应该只关心诸如sin、cos、rcp之类的超越函数吗?

0 投票
4 回答
160 浏览

c - C 代码中忽略的模操作数

我有以下代码:

这会生成以下反汇编:

unsigned short prev = ((wrLine - 1) % LINES_IN_FIFO);

wrLine = (wrLine + 1) % LINES_IN_FIFO;

有趣的是,如果 wrLine 为零,则 prev 最终将等于 0xFFFF,而当 wrLine 为 15 时,它将最终等于 0x0000。知道为什么只有其中一个有效吗?

谢谢,德文

0 投票
2 回答
205 浏览

combinatorics - 要检查一个数字是否为 n 选择 r

有没有一种有效的方法来找到数字 n,给定一个数字 N(可能大到 10^18),对于某些 n 和 r 等于 nCr?我们如何找到 n 的对应最小值?例如

0 投票
5 回答
1663 浏览

java - 如何检查 int 是否为偶数

这可能是一个简单的解决方案,只是让我望而却步。具体来说,我正在使用 sin() 函数在画布上动态创建位置,用于圆上的等距点。创建这些点后,我将通过计算点之间的斜率并在每个斜率步骤重绘形状来为从一个点移动到下一个点的形状设置动画。

问题是,根据坐标值,斜率步长可能只是从点 a 到点 b 的一步。我需要形状沿着路径移动,而不仅仅是点对点跳跃。

我想要做的是强制位置坐标 (x, y) 为偶数,允许斜率值始终可减少。所以,问题的简单部分是......

如何检查 int 值是否为偶数?如果不是,我将简单地将坐标值加 1。

0 投票
1 回答
1138 浏览

floating-point - 如何分析 C/C++ 程序中算术运算的数量?

有没有办法分析某个 C/C++ 程序中使用的算术运算的总数?算术运算是指在硬件中使用整数/浮点/特殊算术单元的任何运算。

0 投票
2 回答
132 浏览

scala - little mathematical thing : squares and roundings

in scala, given the integers d & x, I would have a boolean expression which should be true if and only if y = (x^2 - 1) / d^2 is a square.

I tried this:

but the 3-tuple (x = 2, d = <all values tested>, y = 0.0) seems to be always an answer of my problem, which is obviously wrong. I think my error comes from the rounding made: if x=2, d=4 (for example) then x * x - 1 == 3 and d * d == 16 so the division leads to 0.

do you know what is the good expression?

0 投票
1 回答
419 浏览

c++ - Integer arithmetic when overflow exists

Two 32 bit integer values A and B, are processed to give the 32 bit integers C and D as per the following rules. Which of the rule(s) is(are) reversible? i.e. is it possible to obtain A and B given c and D in all condition?

A. C = (int32)(A+B), D = (int32)(A-B)

B. C = (int32)(A+B), D= (int32)((A-B)>>1)

C. C = (int32)(A+B), D = B

D. C = (int32)(A+B), D = (int32)(A+2*B)

E. C = (int32)(A*B), D = (int32)(A/B)

A few questions about the integer arithmetic. Modular addition forms amathematical structure known as an abelian group. How about signed addition? It's also commutative (that’s where the “abelian” part comes in) and associative, is this forms a n an abelian group?

Given that integer addition is commutative and associative, C is apparently true, because we can retrieve A by (A+(B-B)). What about D? Can we assume that 2 * B = B + B st. B = A+B+B-(A+B)?

And multiplication is more complicated, but I know that it can not be retrieve A if there is an overflow.