Why does integer division code give the wrong answer? [duplicate]

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

Is integer division always equal to the floor of regular division?

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

Why does division by 3 require a rightshift (and other oddities) on x86?

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

Why is __int128_t faster than long long on x86-64 GCC?

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)