Divide by 10 using bit shifts?

Editor’s note: this is not actually what compilers do, and gives the wrong answer for large positive integers ending with 9, starting with div10(1073741829) = 107374183 not 107374182 (Godbolt). It is exact for inputs smaller than 0x40000005, though, which may be sufficient for some uses. Compilers (including MSVC) do use fixed-point multiplicative inverses for constant … Read more

Assembly Language – How to do Modulo?

If your modulus / divisor is a known constant, and you care about performance, see this and this. A multiplicative inverse is even possible for loop-invariant values that aren’t known until runtime, e.g. see https://libdivide.com/ (But without JIT code-gen, that’s less efficient than hard-coding just the steps necessary for one constant.) Never use div for … Read more

How should I do integer division in Perl?

There are at least 2 reasonable answers to this question. (I originally gave only answer 2.) Use the int() function to truncate the floating-point calculation result to an integer (throwing away the decimal part), as Bryan suggested in his self-answer: #539805 Use the use integer pragma to make Perl truncate both the inputs and results … Read more

What’s the mathematical reason behind Python choosing to round integer division toward negative infinity?

But why Python // choose to round towards negative infinity? I’m not sure if the reason why this choice was originally made is documented anywhere (although, for all I know, it could be explained in great length in some PEP somewhere), but we can certainly come up with various reasons why it makes sense. One … Read more

How to perform division in Go

The operands of the binary operation 3 / 10 are untyped constants. The specification says this about binary operations with untyped constants if the operands of a binary operation are different kinds of untyped constants, the operation and, for non-boolean operations, the result use the kind that appears later in this list: integer, rune, floating-point, … Read more

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