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
7
votes
4 answers

Trick to divide a constant (power of two) by an integer

NOTE This is a theoretical question. I'm happy with the performance of my actual code as it is. I'm just curious about whether there is an alternative. Is there a trick to do an integer division of a constant value, which is itself an integer…
Evil Dog Pie
  • 2,300
  • 2
  • 23
  • 46
7
votes
5 answers

Hacks for clamping integer to 0-255 and doubles to 0.0-1.0?

Are there any branch-less or similar hacks for clamping an integer to the interval of 0 to 255, or a double to the interval of 0.0 to 1.0? (Both ranges are meant to be closed, i.e. endpoints are inclusive.) I'm using the obvious minimum-maximum…
7
votes
2 answers

Exact sum of a long array

In order to get the exact sum of a long[] I'm using the following snippet. public static BigInteger sum(long[] a) { long low = 0; long high = 0; for (final long x : a) { low += (x & 0xFFFF_FFFFL); high += (x >> 32); …
maaartinus
  • 44,714
  • 32
  • 161
  • 320
7
votes
3 answers

Compute sqrt(SIZE_MAX+1) using only integer constant expressions, catering for weird ABIs

OpenBSD's C library has an extension called reallocarray(3) which does realloc(array, size*nmemb) without blowing up if the multiplication overflows. The implementation contains this fragment: /* * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX *…
zwol
  • 135,547
  • 38
  • 252
  • 361
7
votes
3 answers

Multiplying two 128-bit ints

I'm trying to multiply two 128-bit integers in C. Here is my algorithm: Split the two 128-bit sequences into S1 and S2. Then split S1 into S11 (front/higher half) and S12 (back/lower half) and split S2 into S21 (front/higher half) and S22…
thetypist
  • 333
  • 1
  • 4
  • 16
7
votes
1 answer

Integral solution to equation `a + bx = c + dy`

In the equation a + bx = c + dy, all variables are integers. a, b, c, and d are known. How do I find integral solutions for x and y? If I'm thinking right, there will be an infinite number of solutions, separated by the lowest common multiple of b…
Joel
  • 2,654
  • 6
  • 31
  • 46
6
votes
1 answer

How does a processor without an overflow flag perform signed arithmetic?

I know that addition of two unsigned integers larger than the bus size of a given processor can be achieved via the carry flag. And normally, the same is true for signed integers using the overflow flag. However, the Intel 8085 only possesses a Sign…
6
votes
2 answers

Prolog: predicate for maximum without accumulator

Is it possible to create a predicate max/2 without an accumulator so that max(List, Max) is true if and only if Max is the maximum value of List (a list of integers)?
user2999349
  • 859
  • 8
  • 21
6
votes
4 answers

What's the fastest way to obtain the highest decimal digit of an integer?

What's the fastest way to implement template unsigned highest_decimal_digit(T x); (which returns e.g. 3 for 356431, 7 for 71 and 9 for 9)? The best I can think of is: constexpr-calculate the "middle-size" power of 10 which fits into…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
6
votes
3 answers

How to approximate Euclidean distance on the integer plane, without overflow?

I'm working on a platform that has only integer arithmetic. The application uses geographic information, and I'm representing points by (x, y) coordinates where x and y are distances measured in meters. As an approximation, I want to compute the…
Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
6
votes
2 answers

How does mentioning multiple arithmetic operation within two operands work in Java

I have an expression in my code - int i = 10 + + 11 - - 12 + + 13 - - 14 + + 15; The value of the variable 'i' evaluates to 75, which is the sum of all the integers mentioned in the expression. How does the evaluation happen in this scenario?
user2381832
  • 126
  • 1
  • 11
6
votes
1 answer

Is there a better way of doing this in Haskell?

I have written the following to assist grand kids with their home schooling work and to keep mind working by learning how to program (I thought haskell sounded awesome). main :: IO () main = do putStrLn "Please enter the dividend :" inputx …
ozhank
  • 69
  • 4
6
votes
1 answer

Reasonable way to implement "safe" math operations using _Generic?

I've been thinking about a way to make it easier to safely use math operations with C's basic datatypes (e.g. using the CERT C coding standard). So far, I've come up with something like this: #include #include #include…
acarlow
  • 914
  • 1
  • 7
  • 13
6
votes
1 answer

Find a number by the decimal part of its square root

I have a math problem consisting of two questions: can we find a number N knowing only the decimal part of its square root up to a precision (only an approximation of the decimal part because the decimal part never ends) is the answer unique? which…
Rachid
  • 69
  • 1
5
votes
2 answers

Quick alternatives to floating point multiplication to calculate percentages

I'm writing some code on an Arduino that needs to run fast and make rough approximations to percentages of integers. For example, given a number I want to find 90% of it, or 70% or 30% etc. The obvious way to do it is multiply by a floating point…
interstar
  • 26,048
  • 36
  • 112
  • 180