integer-arithmetic

Times-two faster than bit-shift, for Python 3.x integers?

℡╲_俬逩灬. 提交于 2019-11-28 15:25:53
问题 I was looking at the source of sorted_containers and was surprised to see this line: self._load, self._twice, self._half = load, load * 2, load >> 1 Here load is an integer. Why use bit shift in one place, and multiplication in another? It seems reasonable that bit shifting may be faster than integral division by 2, but why not replace the multiplication by a shift as well? I benchmarked the the following cases: (times, divide) (shift, shift) (times, shift) (shift, divide) and found that #3

Unexpected results when working with very big integers on interpreted languages

断了今生、忘了曾经 提交于 2019-11-28 02:51:11
I am trying to get the sum of 1 + 2 + ... + 1000000000 , but I'm getting funny results in PHP and Node.js . PHP $sum = 0; for($i = 0; $i <= 1000000000 ; $i++) { $sum += $i; } printf("%s", number_format($sum, 0, "", "")); // 500000000067108992 Node.js var sum = 0; for (i = 0; i <= 1000000000; i++) { sum += i ; } console.log(sum); // 500000000067109000 The correct answer can be calculated using 1 + 2 + ... + n = n(n+1)/2 Correct answer = 500000000500000000 , so I decided to try another language. GO var sum , i int64 for i = 0 ; i <= 1000000000; i++ { sum += i } fmt.Println(sum) //

signed and unsigned arithmetic implementation on x86

拈花ヽ惹草 提交于 2019-11-27 14:53:25
C language has signed and unsigned types like char and int. I am not sure, how it is implemented on assembly level, for example it seems to me that multiplication of signed and unsigned would bring different results, so do assembly do both unsigned and signed arithmetic or only one and this is in some way emulated for the different case? harold If you look at the various multiplication instructions of x86, looking only at 32bit variants and ignoring BMI2, you will find these: imul r/m32 (32x32->64 signed multiply) imul r32, r/m32 (32x32->32 multiply) * imul r32, r/m32, imm (32x32->32 multiply)

How do you store an arbitrarily large integer value in memory?

自闭症网瘾萝莉.ら 提交于 2019-11-27 07:20:35
I have to store an integer value that is larger than the maximum value for the long datatype. How would I store and manipulate this value in memory? Please illustrate it through an example, if possible. Think about storing a numbers as sequences of decimal digits using a struct like this: struct num { int ndigits; char d[MAXDIGITS]; }; For example, the number 123456 could be initialized as struct num n = { 6, { 6, 5, 4, 3, 2, 1 } }; The reversed digit order turns out to be important for easy calculation. In particular, the place value of n.d[i] is n.d[i] * 10^i. Now, a few questions: How would

signed and unsigned arithmetic implementation on x86

一世执手 提交于 2019-11-27 04:06:08
问题 C language has signed and unsigned types like char and int. I am not sure, how it is implemented on assembly level, for example it seems to me that multiplication of signed and unsigned would bring different results, so do assembly do both unsigned and signed arithmetic or only one and this is in some way emulated for the different case? 回答1: If you look at the various multiplication instructions of x86, looking only at 32bit variants and ignoring BMI2, you will find these: imul r/m32 (32x32-

Unexpected results when working with very big integers on interpreted languages

安稳与你 提交于 2019-11-26 23:50:50
问题 I am trying to get the sum of 1 + 2 + ... + 1000000000 , but I'm getting funny results in PHP and Node.js. PHP $sum = 0; for($i = 0; $i <= 1000000000 ; $i++) { $sum += $i; } printf("%s", number_format($sum, 0, "", "")); // 500000000067108992 Node.js var sum = 0; for (i = 0; i <= 1000000000; i++) { sum += i ; } console.log(sum); // 500000000067109000 The correct answer can be calculated using 1 + 2 + ... + n = n(n+1)/2 Correct answer = 500000000500000000 , so I decided to try another language.

How do you store an arbitrarily large integer value in memory?

送分小仙女□ 提交于 2019-11-26 13:08:37
问题 I have to store an integer value that is larger than the maximum value for the long datatype. How would I store and manipulate this value in memory? Please illustrate it through an example, if possible. 回答1: Think about storing a numbers as sequences of decimal digits using a struct like this: struct num { int ndigits; char d[MAXDIGITS]; }; For example, the number 123456 could be initialized as struct num n = { 6, { 6, 5, 4, 3, 2, 1 } }; The reversed digit order turns out to be important for

Is unsigned integer subtraction defined behavior?

 ̄綄美尐妖づ 提交于 2019-11-26 01:44:48
问题 I have come across code from someone who appears to believe there is a problem subtracting an unsigned integer from another integer of the same type when the result would be negative. So that code like this would be incorrect even if it happens to work on most architectures. unsigned int To, Tf; To = getcounter(); while (1) { Tf = getcounter(); if ((Tf-To) >= TIME_LIMIT) { break; } } This is the only vaguely relevant quote from the C standard I could find. A computation involving unsigned

Is unsigned integer subtraction defined behavior?

独自空忆成欢 提交于 2019-11-26 00:50:28
I have come across code from someone who appears to believe there is a problem subtracting an unsigned integer from another integer of the same type when the result would be negative. So that code like this would be incorrect even if it happens to work on most architectures. unsigned int To, Tf; To = getcounter(); while (1) { Tf = getcounter(); if ((Tf-To) >= TIME_LIMIT) { break; } } This is the only vaguely relevant quote from the C standard I could find. A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer

How can I add numbers in a bash script

孤人 提交于 2019-11-26 00:39:09
问题 I have this bash script and I had a problem in line 16. How can I take the previous result of line 15 and add it to the variable in line 16? #!/bin/bash num=0 metab=0 for ((i=1; i<=2; i++)); do for j in `ls output-$i-*`; do echo \"$j\" metab=$(cat $j|grep EndBuffer|awk \'{sum+=$2} END { print sum/120}\') (line15) num= $num + $metab (line16) done echo \"$num\" done 回答1: For integers : Use arithmetic expansion: $((EXPR)) num=$((num1 + num2)) num=$(($num1 + $num2)) # also works num=$((num1 + 2 +