# Questions tagged [arithmetic]

The tag has no usage guidance.

28 questions
Filter by
Sorted by
Tagged with
158 views

### Semantics of simulating a 64bit integer with two 32bit integers (hi, lo)

I am working in a system which only supports 32bit integers, in order to have a 64bit (unsigned) integer I decided to simply use two 32bit integers with one being the upper 32 bits (hi), and the other ...
470 views

### Is the sum or subtraction of two positive machine numbers exact?

If you have two positive numbers that can be represented exactly by the finite arithmetic of your computer, that is, two machine numbers, if you perform their sum or subtraction with perfect ...
3k views

### Why does Integer.MIN_VALUE - Integer.MAX_VALUE equals to 1 in Java?

If you run the statement: System .out.println( (Integer.MIN_VALUE)-(Integer .MAX_VALUE )); In java it returns 1 as the answer. Is this because Java just considers those values as 32bit integers ...
184 views

### Is writing arithmetic functions to allow “type coercion” a good idea?

Let's say I want to write a function to compute factorials of nonnegative integers. I could write something like this: fact :: Num a => Int -> a fact n = fromInteger(product [1..n]) (Let's ...
149 views

### Describe a slope (N/M), approximately as small number of fractions (n/m)

What algorithm can I use to describe a specified gradient (N/M) approximately as the sum of a set of rational fractions { (n1/m1) + (n2/m2) … } ? Design constraints: The algorithm takes as input (N, ...
383 views

### For learning purposes, how should I set about implementing an arbitrary precision library in C or C++?

I know I am reinventing the wheel. But I'm really interested in implementing arbitrary precision numbers (integers, rationals, complex, etc) in C or C++ and their algorithms. Please be patient. My ...
409 views

### Shortening a boolean AND with third operand

I'm trying to calculate the sum of 2 bits using basic binary arithmetic and currently, I'm doing this: function Add(bool a, bool b, bool carry) { return { Result: a ^ b ^ carry, ...
3k views

### Understanding of the Carry Flag Bit

Intel 8085: In my textbook it is said: "Carry Flag - this flag is the carry out from the MSB of the A-register. CY is set after an ADD instruction if carry out was generated from the A-register." I ...
3k views

### Difficulties with Two's complement in Assembly

Everything discussed here will refer to Intel 8085 (8-bit architecture). When using two's complement number "conversion" : 1.) If we take SBI 0F (subtraction) for example; immediate value 0F(hex) or ...
72 views

### Multiprecision Arithmetic; The Carry Flag

Intel 8085: I'm having big troubles understanding addition or subtraction with more than 16-bit numbers in Intel 8085: 1.) If I execute ADC M instruction then this should happen: (A)<--(A)+((H)(L))...
2k views

### Why are arbitrary-precision decimals used over rational numbers in practice?

The basic arithmetic a computer chip can do only works on numbers(integers or floating point) of a fixed size. There are many algorithms that could be extended to work on numbers of arbitrary size (...
83 views

### How do two (programming) methods for capping daily rep stack up?

I wrote a supersuccessful answer to a question on an SE site that got (at least) 21 upvotes in a 24-hour period. In theory, I should have gotten 210 rep points, but there is a daily reputation limit ...
436 views

### Combine sequences of numbers with “variable bitlengths” into short unique strings

It is not unlikely that what I want to do is not possible, but it doesn't hurt to ask. Imagine a set of lists, each containing positive integers (in my case, a list always consists of four integers, ...
20k views

### Will a computer attempt to divide by zero?

We all know 0/0 is Undefined and returns an error if I were to put it into a calculator, and if I were to create a program (in C at least) the OS would terminate it when I try to divide by zero. But ...
58 views

### multiplication with insufficiently long registers [duplicate]

I want to multiply two numbers but since I don’t have long enough registers in the current architecture need to “break them down” into shorter ones and somehow perform the calculation. For example: ...
2k views

### Is order of arguments in an arithmetic expression important to achieve as most exact result as possible (speed is not necessary)?

Actually, in this question I do not ask about particular language or architecture, however I understand there might be some differences. In physics / engineering it is usually better to handle ...
208 views

### What do you get when you cross a multi-precision integer with a floating-point number?

I'm working on adding multi-precision integers to the suite of numeric types in my APL interpreter, but I'm not sure what to do about the odd type-combinations that now arise. I now have the following ...
4k views

### how to enumerate/generate all possible binary trees from N leaves and N-1 nodes?

I am trying to implement the 24 Game in ansi C. This game goes as follows: For a list of four given numbers, try to find a solution involving these four numbers, that, using addition(+), subtraction(-...
331 views

### How do you guard against hidden overflows

consider the following: (i+j)/2 i and j are both 32 bit integers and the result should also be. But in the little equation, there's a hidden overflow, i+j could become larger then a 32 bit integer ...
769 views

### Next power of 2 for a number (in search for better “bit-twiddling” way)

I just wonder if there exists better (i.e. faster?) way to get the next power of 2 for a given number than the following one (maybe some better sort of "bit-twiddling" hack is possible?) ... static ...
644 views

### What rationale is used when programming language designers decide what sign the result of modulo operation takes?

Going through Modulo operation (the avenue I entered while exploring the difference between rem and mod) I came across: In mathematics the result of the modulo operation is the remainder of the ...
5k views

### Name of the Countdown Numbers round problem - and algorithmic solutions?

For the non-Brits in the audience, there's a segment of a daytime game-show where contestants have a set of 6 numbers and a randomly generated target number. They have to reach the target number using ...
5k views

### Array Multiplication and Division

I came across a question that (eventually) landed me wondering about array arithmetic. I'm thinking specifically in Ruby, but I think the concepts are language independent. So, addition and ...
4k views

### Performance and other issues with using floating point types in C++

Being interested in C++ performance programming there is one aspect I really have no clue about- and that is the implications of using floating point calculations vs doubles vs normal integer ...
555 views

### Arithmetic Coding vs. Machine Numeric Precision

When subdividing intervals for arithmetic coding, the worst case scenario is that the final interval will have size 2^(1-n), where n is the number of unique symbols you are encoding. This will reach ...
782 views

### Arithmetic Coding Questions

I have been reading up on arithmetic coding and, while I understand how it works, all the guides and instructions I've read start with something like: Set up your intervals based upon the frequency ...