Questions tagged [integer-arithmetic]

Anything related to integer arithmetic, i.e. arithmetic operations on integer numbers. This is mostly relevant for languages which represent integer numbers with specific data-types (e.g. `int` or `long` in C, C++ or Java).

Anything related to integer arithmetic, i.e. arithmetic operations on integer numbers. This is mostly relevant for languages which represent integer numbers with specific data-types (e.g. int or long in C, C++ or Java).

452 questions
9
votes
4 answers

How to do arithmetic modulo another number, without overflow?

I'm trying to implement a fast primality test for Rust's u32 and u64 datatypes. As part of it, I need to compute (n*n)%d where n and d are u32 (or u64, respectively). While the result can easily fit in the datatype, I'm at a loss for how to compute…
9
votes
3 answers

Floored integer division

Is there an easy, efficient and correct (i.e. not involving conversions to/from double) way to do floored integer division (like e.g. Python offers) in C#. In other words, an efficient version of the following, that does not suffer from long/double…
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
9
votes
3 answers

Optimizing javascript code to use integer arithmetic

There are some algorithms which solve problems "very well" under the assumption that "very well" means minimizing the amount of floating point arithmetic operations in favor of integer arithmetic. Take for example Bresenham's line algorithm for…
Robz
  • 1,747
  • 5
  • 21
  • 35
8
votes
2 answers

How to implement arithmetic right shift in C

Many lossless algorithms in signal processing require an evaluation of the expression of the form ⌊ a / 2b ⌋, where a, b are signed (a possibly negative, b non-negative) integers and ⌊·⌋ is the floor function. This usually leads to the following…
DaBler
  • 2,695
  • 2
  • 26
  • 46
8
votes
4 answers

Convert x >= y into 1 or 0 without branching or Boolean expressions

I need to implement the following function without branching or Boolean expressions: uint8_t func(uint32_t num, uint8_t shl) { if (num >= (1 << shl)) { return shl; } else { return 0; } } The first step I did…
goodvibration
  • 5,980
  • 4
  • 28
  • 61
8
votes
2 answers

Multiplying integer by rational without intermediate overflow

I have a struct representing a nonnegative rational number p/q: struct rational { uint64_t p; uint64_t q; // invariant: always > 0 }; I would like to multiply my rational by a uint64 n and get an integer result, rounded down. That is, I…
ridiculous_fish
  • 17,273
  • 1
  • 54
  • 61
8
votes
2 answers

Division in C++

I am new to C++ and I tried this simple code: #include #include using namespace std; int main(){ double a; a=1/6; cout<
Silviu
  • 749
  • 3
  • 7
  • 17
8
votes
2 answers

How to divide by 9 using just shifts/add/sub?

Last week I was in an interview and there was a test like this: Calculate N/9 (given that N is a positive integer), using only SHIFT LEFT, SHIFT RIGHT, ADD, SUBSTRACT instructions.
Tracy
  • 1,988
  • 5
  • 25
  • 36
8
votes
1 answer

Big number arithmetic using LLVM (from Haskell)

An answer to my previous question indicated that Haskell represents plusWord2# as llvm.uadd.with.overflow. I'm looking to do long addition with carry, like how the x86 ADC instruction works. This instruction not only adds it's two arguments, but…
Clinton
  • 22,361
  • 15
  • 67
  • 163
8
votes
3 answers

What's the efficient algorithm to find the Integer square root of a very large number, digit by digit?

I need to write program to find the integer square root of a number which is thousands of digits long. I can't use Newton Raphson as I don't have data types to store and divide such large numbers. I am using a long array in C to store the number. …
Naman
  • 2,569
  • 4
  • 27
  • 44
7
votes
3 answers

Multiplication of large integers

I tried to multiply 111111111*111111111, which is the same as 111111111^2, and got incorrect results. It should give 12345678987654321, but instead it gives a rounding error. Do I need to use some special variable type for long numbers or is this a…
Puzzzled
7
votes
4 answers

How can I compute a * b / c when both a and b are smaller than c, but a * b overflows?

Assuming that uint is the largest integral type on my fixed-point platform, I have: uint func(uint a, uint b, uint c); Which needs to return a good approximation of a * b / c. The value of c is greater than both the value of a and the value of…
goodvibration
  • 5,980
  • 4
  • 28
  • 61
7
votes
10 answers

Find the a 4 digit number whose square is 8 digits AND last 4 digits are the original number

From the comments on my answer here, the question was asked (paraphrase): Write a Python program to find a 4 digit whole number, that when multiplied to itself, you get an 8 digit whole number whose last 4 digits are equal to the original number. I…
Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43
7
votes
1 answer

9933272057275866 is it a magic number?

I faced problem and I can't quite explain it. Actually I'm quite surprised. When I try to increment the number 9933272057275866 by 1, it automatically adds 2!!! Please see following code: let test = 9933272057275866; let test2 =…
7
votes
1 answer

Chaining checked arithmetic operations in Rust

When doing integer arithmetic with checks for overflows, calculations often need to compose several arithmetic operations. A straightforward way of chaining checked arithmetic in Rust uses checked_* methods and Option chaining: fn…
mzabaluev
  • 532
  • 5
  • 13
1 2
3
30 31