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

32-bit fixed point overflow

I'm doing some 'early computing' on a 32-bit Windows PC, and looking at the limits. Now, 2**32 is 4,294,967,296, and I find that 4294967290 + 5 is perfectly OK, and 4294967290 + 6 quite properly overflows. What puzzles me is that …
John White
  • 131
  • 1
  • 3
  • 19
4
votes
4 answers

How to optimize printing out the difference between the greater and the lesser of two integers?

UVA Problem no. 10055, Hashmat the Brave Warrior, probably the easiest problem there. The input consists of a series of pairs of unsigned integers ≤ 2^32 (thus mandating the use of 64bit integers…) For each pair the task is to print out the…
user4385532
4
votes
3 answers

perl integer arithmetic giving floating point answer

The below code mimics actual production code. double quotes are used as the actual data comes from an XML file, parsed using XML:Twig: #!/usr/bin/perl use strict; use warnings; use diagnostics; use Devel::Peek; my $linetotalinclusive = "8458.80" *…
ftumsh
  • 89
  • 5
4
votes
3 answers

prolog how to use math operation

I am a novice in prolog programming, i use swi-prolog. Now I'm stucked by some math problems as we know the predicate :A is 3+3.works well,the answer is A=6. but if I want to find two digits (A and B) from 0~9 that a+b=6 6 is A+B does't work. so I…
Lin Jiayin
  • 499
  • 1
  • 6
  • 11
4
votes
2 answers

Calculating the modulo of two intervals

I want to understand how the modulus operator works when applied to two intervals. Adding, subtracting and multiplying two intervals is trivial to implement in code, but how do you do it for modulus? I'd be happy if someone can show me the formula,…
Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122
4
votes
2 answers

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

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

How do I solve a simple String arithmetic expression such as 5-2*10?

I'm having a lot of trouble trying to do this for some reason. I have a class which wants me to evaluate a complex Java expression such as (3 + 5[3*2-4]), using recursion. I think I have an idea on how I want to approach it, but I can't seem to…
Mikey Chen
  • 2,370
  • 1
  • 19
  • 31
4
votes
5 answers

From a loop index k, obtain pairs i,j with i < j?

I need to traverse all pairs i,j with 0 <= i < n, 0 <= j < n and i < j for some positive integer n. Problem is that I can only loop through another variable, say k. I can control the bounds of k. So the problem is to determine two arithmetic…
a06e
  • 18,594
  • 33
  • 93
  • 169
4
votes
6 answers

Round long from 1004L to 1000L (or 1006L to 1010L)

Suppose I have Long someLong = 1004L. What efficient method can I use to round this down to 1000L? Note that I do not actually know that someLong == 1004L so I can't simply do someLong -= 4L;. I need a generalizable method. I also want the ability…
user2763361
  • 3,789
  • 11
  • 45
  • 81
4
votes
1 answer

How can i do arithmetic operations in one TextBox?

For example i write in TextBox1 4*5 or 3-2 how can make the answer appear in the same textbox ? I tried this but it did not work anyway Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click textbox1.text =…
user3428637
  • 39
  • 1
  • 1
  • 10
4
votes
1 answer

infinite precision integers: division by 2

In C, if I want to divide an int by 2, x%2 should run as fast as (x%10)% 2 because a good compiler will just look at the last bit. But what about in a language with infinite precision arithmetic? In particular, in Haskell which would be faster (or…
4
votes
1 answer

Protect against missbehaviour caused by twos compliment

I read the following question: Value of i for (i == -i && i != 0) to return true in Java and was left slightly dazzled. One reads about spacecraft being lost because of using the wrong unit system, so seeing the behaviour mentioned in the question…
ted
  • 4,791
  • 5
  • 38
  • 84
4
votes
2 answers

Find out (in C++) if binary number is prefix of another

I need a function with a header like this: bool is_prefix(int a, int b, int* c) { // ... } If a is, read as a binary number string, a prefix of b, then set *c to be the rest of b (i.e. "what b has more than a") and return true. Otherwise,…
Johannes
  • 2,901
  • 5
  • 30
  • 50
4
votes
2 answers

Printing short int using various format specifiers

Please have a look at this code: #include int main(void) { short s = -1; printf("sizeof(short) = %lu\n", sizeof(short)); printf("sizeof(int) = %lu\n", sizeof(int)); printf("sizeof(long) = %lu\n", sizeof(long)); printf("s = %hd\n",…
rootkea
  • 1,474
  • 2
  • 12
  • 32
3
votes
1 answer

Signed Word to Integer Conversion in Lisp

I'd like some help in understanding and fixing an SBCL compiler note that says: ; in: DEFUN PRINT-SEARCH-PROGRESS-GRAPH ; (- (1+ WOULDWORK-PKG::*N*) ; (LENGTH WOULDWORK-PKG::*L*)) ; ; note: doing signed word to integer coercion (cost 20),…
davypough
  • 1,847
  • 11
  • 21