FAQ Database Discussion Community

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

c,percentage,integer-overflow,integer-division
I am trying to express a battery voltage as a percentage. My battery level is a (global) uint16 in mV. I have a 16-bit CPU. Here is my code: static uint8 convertBattery(void){ uint16 const fullBattery = 3000; /* 3V = 3000mV */ uint8 charge; charge = ((battery*100)/fullBattery); return charge; }...

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

c,integer-division,divide-by-zero,integer-arithmetic,ppc
I am debugging a production code written in C and its simplest form can be shown as - void test_fun(int sr) { int hr = 0; #define ME 65535 #define SE 256 sr = sr/SE; <-- This should yield 0 if(sr == 1) hr = ME; else hr = (ME+1)/sr;...

## Divide by 9 without using division or multiplication operator

c,algorithm,math,multiplication,integer-division
This question I have tried to solve it but couldn't get any way. Any pointers would be appreciated. Regular subtraction way of doing division is not the intention here, ingenious way of using shifting operator to get this done is the intention....

## Dividing integers by floats resulting in proportional integers

algorithm,integer,division,integer-division
Recently I've came across a hard problem solving while doing a project. And I'd like to ask you guys for a solution / algorithm to solve it, because I'm really struggling to think about a clever idea to do so. I have an arbitrary number of floats: f1, f2, ...,...

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

python,integer-division
Since the Python integer division operator (a // b) always returns a value which can be safely stored in an int without losing precision (no matter of values for a and b), why is the following true? If one or both operands are of type float, this operator returns a...

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

algorithm,math,modulus,integer-division
Given three numbers N, A and B. Find how integers in range from 1 to N are divisible by A or B. I can't use modulus operator from range 1 to N because N can be as large as 10^12 and then I would run out of allocated time for...

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

r,sum,logical-operators,integer-division
I have an ex. where I have to see how many values of a vector are divisible by 2. I have this random sample: set.seed(1) y <- sample(c(0:99, NA), 400, replace=TRUE) I created a new variable d to see which of the values are or aren't divisible by 2: d...

c#,excel,double-precision,integer-division
I have trouble with the following code. It is not giving the same answer in excel as C# and I'm sure the excel answer is correct. I have already tried to change integer values to double by adding a decimal point. Please advise. the excel version =((1+BZ21)*BX21*CG21*(B21*1))-(0.5*BZ21*BX21*CG21*((C21*0)+(0*D21))) the c# version...

## Overflow while doing multiplication

c++,c++11,integer-division
I want to calculate value of (N* N *(N+1)/2) mod M where this N can go upto 10^18 and M is at max up to 10^7. I tried to code it but don't know the reason that why it is overflowing. Here is my code : In main I do...

## divide parallel arrays C++ [duplicate]

c++,arrays,integer-division,parallel-arrays
This question already has an answer here: Why does division result in zero instead of a decimal? 6 answers I have a question about dividing parallel arrays. I'm fairly new to C++. In my program I'm dividing parallel arrays (atBats[] and hits[], and storing the results in an empty...

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

c++,modulus,integer-division
Can someone please explain the under-the-hood mechanics of x % y !=0 in C++? It evaluates to 0 if there is no remainder in integer division and it evaluates to 1 if there is any remainder of any amount. I find this to be quite useful, but I'd like to...