floor-division

Can I rely on % (modulo) operator in C for negative numbers?

元气小坏坏 提交于 2020-07-10 03:15:47
问题 Using GCC: printf("%i \n", -1 % (int)4); printf("%u \n", -1 % (unsigned int)4); Output: -1 3 Can I rely on this behaviour across platforms? Should I explicitly define MOD and REM macros to be sure this isn't altered? 回答1: From C99 onwards the result of % is required to be rounded toward 0 as quoted by Chris Dodd. Prior to C99 standard, % operator's behavior on negative number is implementation defined . When integers are divided and the division is inexact, if both operands are positive the

Can I rely on % (modulo) operator in C for negative numbers?

限于喜欢 提交于 2020-07-10 03:14:53
问题 Using GCC: printf("%i \n", -1 % (int)4); printf("%u \n", -1 % (unsigned int)4); Output: -1 3 Can I rely on this behaviour across platforms? Should I explicitly define MOD and REM macros to be sure this isn't altered? 回答1: From C99 onwards the result of % is required to be rounded toward 0 as quoted by Chris Dodd. Prior to C99 standard, % operator's behavior on negative number is implementation defined . When integers are divided and the division is inexact, if both operands are positive the

Two forward slashes in Python

徘徊边缘 提交于 2019-12-17 15:46:31
问题 I came across this sample of code from a radix sort: def getDigit(num, base, digit_num): # pulls the selected digit return (num // base ** digit_num) % base What does the ' // ' do in Python? 回答1: // is the floor division operator. It produces the floor of the quotient of its operands, without floating-point rounding for integer operands. This is also sometimes referred to as integer division, even though you can use it with floats, because dividing integers with / used to do this by default.

What does the “variable //= a value” syntax mean in Python? [duplicate]

自作多情 提交于 2019-12-08 17:00:14
问题 This question already has answers here : What does //= in python do? [duplicate] (3 answers) Closed 3 years ago . I came across with the code syntax d //= 2 where d is a variable. This is not a part of any loop, I don't quite get the expression. Can anybody enlighten me please? 回答1: // is a floor division operator. The = beside it means to operate on the variable "in-place". It's similar to the += and *= operators, if you've seen those before, except for this is with division. Suppose I have

Fast floor of a signed integer division in C / C++

女生的网名这么多〃 提交于 2019-12-06 06:33:06
问题 In C a floor division can be done, eg: int floor_div(int a, int b) { int d = a / b; if (a < 0 != b < 0) { /* negative output (check inputs since 'd' isn't floored) */ if (d * a != b) { /* avoid modulo, use multiply instead */ d -= 1; /* floor */ } } return d; } But this seems like it could be simplified. Is there a more efficient way to do this in C? Note that this is nearly the reverse of this question: Fast ceiling of an integer division in C / C++ 回答1: Less assembly instructions in the

Fast floor of a signed integer division in C / C++

℡╲_俬逩灬. 提交于 2019-12-04 13:05:36
In C a floor division can be done, eg: int floor_div(int a, int b) { int d = a / b; if (a < 0 != b < 0) { /* negative output (check inputs since 'd' isn't floored) */ if (d * a != b) { /* avoid modulo, use multiply instead */ d -= 1; /* floor */ } } return d; } But this seems like it could be simplified. Is there a more efficient way to do this in C? Note that this is nearly the reverse of this question: Fast ceiling of an integer division in C / C++ Less assembly instructions in the generated code and quicker path to the result I think. For the RISC machines with huge numbers of registers

Two forward slashes in Python

泄露秘密 提交于 2019-11-27 19:59:47
I came across this sample of code from a radix sort : def getDigit(num, base, digit_num): # pulls the selected digit return (num // base ** digit_num) % base What does the ' // ' do in Python? sepp2k // is the floor division operator. It produces the floor of the quotient of its operands, without floating-point rounding for integer operands. This is also sometimes referred to as integer division, even though you can use it with floats, because dividing integers with / used to do this by default. In Python 3, the ordinary / division operator returns floating point values even if both operands