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
25
votes
1 answer

Passing arbitrary-sized integers from Prolog to C

Right now, I'm learning how to interface SICStus Prolog with C code. I would like to have/use/see a C implementation of "Hamming weight" of arbitrary-sized integers in SICStus Prolog version 4. It seems to me that I need C functions for testing term…
repeat
  • 18,496
  • 4
  • 54
  • 166
25
votes
7 answers

Is arithmetic overflow equivalent to modulo operation?

I need to do modulo 256 arithmetic in C. So can I simply do unsigned char i; i++; instead of int i; i=(i+1)%256;
avmohan
  • 1,820
  • 3
  • 20
  • 39
18
votes
1 answer

What's special about 787?

In ghci, using the arithmoi package: Math.NumberTheory.Powers.General> :set +s Math.NumberTheory.Powers.General> integerRoot 786 ((10^32)^786) 100000000000000000000000000000000 (0.04 secs, 227,064 bytes) Math.NumberTheory.Powers.General> integerRoot…
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
18
votes
3 answers

Why do different algorithms of summing not match?

Assume that I want to get sum of all squares from M to N. I googled a bit and found this formula: (1^2 + 2^2 + 3^2 + ... + N^2) = (N * (N + 1) * (2N + 1)) / 6 so I write this code: static void Main(string[] args) { const int from = 10; …
Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
16
votes
3 answers

Efficiently detect that rational numbers are equal

I have a collection of many rational numbers, with the numerator and denominator of each stored as a large (hundreds or thousands of bits) unsigned integer. I'd like to be able to efficiently test whether any given rational number a/b in the set is…
Sneftel
  • 40,271
  • 12
  • 71
  • 104
16
votes
8 answers

How do you store an arbitrarily large integer value in memory?

I have to store an integer value that is larger than the maximum value for the long datatype. How would I store and manipulate this value in memory? Please illustrate it through an example, if possible.
yatin
  • 187
  • 1
  • 1
  • 3
14
votes
2 answers

Why does Rust's u64.pow expect a u32?

Why is it that Rust's u64 primitive expects a u32 exponent? error[E0308]: mismatched types --> src/protagonists.rs:13:25 | 13 | return root.pow(self.secret) % prime; | ^^^^^^^^^^^ expected u32, found u64 help:…
joedborg
  • 17,651
  • 32
  • 84
  • 118
14
votes
4 answers

How to perform ceiling-division in integer arithmetic?

It's basically returning the boxes_needed. 1 box can contain 10 items. So if the items typed by the user is 102 then the code should return 11 boxes. Is there a way to divide that rounds upwards if there is a non-zero remainder?
Newbie_Android
  • 225
  • 2
  • 3
  • 12
13
votes
17 answers

Subtraction without minus sign in C

How can I subtract two integers in C without the - operator?
SyncMaster
  • 9,754
  • 34
  • 94
  • 137
13
votes
3 answers

Why int plus uint returns uint?

int plus unsigned int returns an unsigned int. Should it be so? Consider this code: #include #include #include class test { static const int si = 0; …
Vahagn
  • 4,670
  • 9
  • 43
  • 72
12
votes
1 answer

speed of elementary mathematical operations in Numpy/Python: why is integer division slowest?

EDIT2: As @ShadowRanger pointed out, this is a Numpy phenomenon, not Python. But when doing the calculations in Python with list comprehensions (so x+y becomes [a+b for a,b in zip(x,y)]) then all arithmetic operations still take equally long…
PDiracDelta
  • 2,348
  • 5
  • 21
  • 43
12
votes
2 answers

Could type punning signed to unsigned integers make bounds checking faster by eliminating the need for >= comparison?

Say I had a really performance-critical loop in my program where I need to check if a point was inside a rectangle, but I know at compile time that the lower bounds are always going to be 0, like the following: (x >= 0 && y >= 0 && x < width && y <…
Stuntddude
  • 228
  • 1
  • 16
11
votes
1 answer

Why does divMod round division down instead of ensuring a positive remainder?

The Euclidean division theorem, with which most math students and Haskellers are familiar, states that Given two integers a and b, with b ≠ 0, there exist unique integers q and r such that a = bq + r and 0 ≤ r < |b|. This gives the conventional…
dfeuer
  • 48,079
  • 5
  • 63
  • 167
11
votes
9 answers

Perfect square and perfect cube

Is there any predefined function in c++ to check whether the number is square of any number and same for the cube..
d3vdpro
  • 2,887
  • 4
  • 25
  • 29
10
votes
1 answer

When to pick 'Natural' over 'Integer' in Haskell?

Not long ago I've discovered Natural data type in base. It's supposed to be taken (as it seems to me) when you intend to use non-negative integer type. But it's not exactly clear why should I prefer Natural to Integer. Both types have arbitrary…
Shersh
  • 9,019
  • 3
  • 33
  • 61
1
2
3
30 31