Menu
  • HOME
  • TAGS

32 bit signed integer division gives 0x7fffffff as quotient on PowerPC

c,integer-division,divide-by-zero,integer-arithmetic,ppc

Division by zero is undefined behaviour, see C11 standard 6.5.5#5 (final draft). Getting a trap or SIGFPE is just a courtesy of the CPU/OS. PowerPC as typical RISC CPU does not catch it, as it can safely be detected by a simple check of the divisor right before doing the...

Overflow while doing multiplication

c++,c++11,integer-division

The thing is, every operation will overflow if the right (or wrong) set of operands are provided. justanothercode has hinted in comments that modulo 10^7 will not overflow a long long (in fact it won't overflow a long) You need to make use of the identities that (a+b)%c == ((a%c)...

Haskell Novice Trouble with Splitting a List in Half

haskell,integer-division

The / is used for when you're happy to have a non-integer answer. Since you've already checked the number's even, you can hapily use integer division with div: halve :: [a] -> ([a], [a]) halve x | even len = (take half x, drop half x) | otherwise = error...

Divide by 9 without using division or multiplication operator

c,algorithm,math,multiplication,integer-division

Here's a solution heavily inspired by Hacker's Delight that really uses only bit shifts: def divu9(n): q = n - (n >> 3) q = q + (q >> 6) q = q + (q>>12) + (q>>24); q = q >> 3 r = n - (((q << 2) <<...

Mechanics of 'x % y != 0' in C++

c++,modulus,integer-division

% is the integer remainder operator. For example: 21 % 7 == 0 22 % 7 == 1 25 % 7 == 4 27 % 7 == 6 28 % 7 == 0 x % y != 0 is true if the integer division yields a non-zero remainder, false if...

divide parallel arrays C++ [duplicate]

c++,arrays,integer-division,parallel-arrays

batAvg[i] = (hits[i] / atBats[i]) * 1000; inside the parenthesis, both numbers are integers, so the expression is an integer. So if hits[i] < atBats[i] the result is zero, which then you multiply by 1000. Try this instead: batAvg[i] = (1000.0 * hits[i]) / atBats[i]; EDITED: 1000.0 instead of...

division an integer into k parts

java,algorithm,integer-division

The idea is a backtraking algorithm approach (with recursion), You can decrease parameters and get partials solutions and then check if you have a right solution. public class Problem { private static void algorithm(int n, int k, int m) { algorithmRecursive(Collections.EMPTY_LIST, n, k, m, 1); } private static void algorithmRecursive(List<Integer>...

Why does Python 3.x have unusual floored (integer) division behaviour?

python,integer-division

Python states that both arguments are coerced to a common type. From the Numeric types documentation: Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where integer is narrower than...

Division to yield result in multiples of hundred or thousand etc

java,integer-division

For the first value (150), you can try setting weekSpecial = weekSpecial/100 * 100 after the calculation of the weekSpecial parameter. Integer division in Java drops the decimal part, so first dividing and then multiplying by 100 should give you what you asked. Of course, the calculation can be integrated...

Dividing integers by floats resulting in proportional integers

algorithm,integer,division,integer-division

Here's how I'd approach it, at least initially. Every bucket has a desired amount that it needs. This is based on their float values, and all the float values sum to 1. So, go through the "objects" to be distributed one by one. To figure out which bucket gets it,...

ZeroDivisionError Exception with int, Nan with float

ruby,division,ruby-2.0,integer-division

Integer 0 is exactly zero; there is no error. And since division by zero is mathematically undefined, it makes sense that integer division by 0 raises an error. On the other hand, float 0.0 is not necessarily representing exactly zero. It might originate from a number whose absolute value is...

Unexpected result when calculating a percentage - even when factoring in integer division rules

c,percentage,integer-overflow,integer-division

Diagnosis The value you expect is, presumably, 91. The problem appears to be that your compiler is using 16-bit int values. You should identify the platform on which you're working and include information about unusual situations such as 16-bit int types. It is reasonable for us to assume 32-bit or...

Why is modulus operator slow?

modulo,integer-division,cpu-speed,programming-pearls

The modulus/modulo operation is usually understood as the integer equivalent of the remainder operation - a side effect or counterpart to division. Except for some degenerate cases (where the divisor is a power of the operating base - i.e. a power of 2 for most number formats) this is just...

How many values of a vector are divisible by 2? Use R

r,sum,logical-operators,integer-division

you can sum over the mod operator like so: sum(1-y%%2) or sum(y%%2 == 0). Note that x %% 2 is the remainder after dividing by two which is why this solution works.

Dividing a large number by 2

algorithm,pascal,integer-division,largenumber

You will proceed from left to right, dividing the digits by two. Every time a digit is odd, you will propagate a carry (10) to the next digit. Example: divide 123 1 divided by 2 is 0, carry = 10 2 + 10 divided by 2 is 6, no carry...

Integer division by zero [closed]

c++,integer-division

Since greatestCommonDivisor is global, simply you can change your convertToGreatestCommonDivisor function to void convertToGreatestCommonDivisor(int numerator, int denominator) //Change return type to void { // Remove the local variable "greatestCommonDivisor" int i; // checks if i is greater than the numerator *and* the denominator for (i = 1; i <= numerator...

How to count number of divisible terms without using modulus operator?

algorithm,math,modulus,integer-division

I just want to use some equation to evaluate this thing rather than a modulus operator because the program needs to produce results within 1 sec. I have tried this counter=(((int)(N/A))+((int)(N/B)))-((int)(N/(A*B))); but it fails for input N=200 A=20 B=8 You are already on the right track, your formula indicates...

Excel Spreadsheet not giving same answer as C# program [closed]

c#,excel,double-precision,integer-division

I think something's funny in your Excel spreadsheet. When I tried this, I get the same answer in both C# and Excel. I believe the correct answer is 9.577520323. Here's a screenshot of my Excel spreadsheet: ...

Swift: remainder operator (%) not working as documented

swift,modulo,floating-point-precision,modulus,integer-division

52 goes into 66 1 time evenly with 14 left over. When you divide 66 / 52, you get 1.2692, or 52 / 52 + 14 / 52. So, if you're only after the decimals here, they can be acquired like this (66 % 52) / 52