Why does division return zero
Either declare set1 and set2 as floats instead of integers or cast them to floats as part of the calculation: SET @weight= CAST(@set1 AS float) / CAST(@set2 AS float);
Either declare set1 and set2 as floats instead of integers or cast them to floats as part of the calculation: SET @weight= CAST(@set1 AS float) / CAST(@set2 AS float);
You’re dividing integers, which means that you’re using integer division. In integer division the fractional part of the result is thrown away. Try the following: float res = (float) quantity / standard; ^^^^^^^ The above forces the numerator to be treated as a float which in turn promotes the denominator to float as well, and … Read more
The reason the quotients in your test case are not equal is that in the math.floor(a/b) case, the result is calculated with floating point arithmetic (IEEE-754 64-bit), which means there is a maximum precision. The quotient you have there is larger than the 253 limit above which floating point is no longer accurate up to … Read more
Can’t we multiply rax with edi directly? We can’t imul rax, rdi because the calling convention allows the caller to leave garbage in the high bits of RDI; only the EDI part contains the value. This is a non-issue when inlining; writing a 32-bit register does implicitly zero-extend to the full 64-bit register, so the … Read more
The current answers all focus on decimal digits, when applying the “add all digits and see if that divides by 3”. That trick actually works in hex as well; e.g. 0x12 can be divided by 3 because 0x1 + 0x2 = 0x3. And “converting” to hex is a lot easier than converting to decimal. Pseudo-code: … Read more
3 and 6 are both Int, and dividing one Int by another gives an Int: that’s why you get back 0. To get a non-integer value you need to get the result of the division to be a non-integer value. One way to do this is convert the Int to something else before dividing it, … Read more
The performance difference comes from the efficiency of 128-bit divisions/modulus with GCC/Clang in this specific case. Indeed, on my system as well as on godbolt, sizeof(long long) = 8 and sizeof(__int128_t) = 16. Thus operation on the former are performed by native instruction while not the latter (since we focus on 64 bit platforms). Additions, … Read more
Do floating point division then convert to an int. No extra modules needed. Python 3: >>> int(-1 / 2) 0 >>> int(-3 / 2) -1 >>> int(1 / 2) 0 >>> int(3 / 2) 1 Python 2: >>> int(float(-1) / 2) 0 >>> int(float(-3) / 2) -1 >>> int(float(1) / 2) 0 >>> int(float(3) / … Read more
The reason is that unsigned division by 2^n can be implemented very simply, whereas signed division is somewhat more complex. unsigned int u; int v; u / 4096 is equivalent to u >> 12 for all possible values of u. v / 4096 is NOT equivalent to v >> 12 – it breaks down when … Read more
Ideally, we would like to have two operations div and mod, satisfying, for each b>0: (a div b) * b + (a mod b) = a 0 <= (a mod b) < b (-a) div b = -(a div b) This is, however, a mathematical impossibility. If all the above were true, we would have … Read more