# Questions tagged [arithmetic]

The tag has no usage guidance.

27 questions
Filter by
Sorted by
Tagged with
7answers
441 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 ...
2answers
2k 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 ...
2answers
177 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 ...
2answers
146 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, ...
1answer
295 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 ...
4answers
346 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, ...
2answers
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 ...
1answer
2k 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 ...
1answer
69 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))...
4answers
1k 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 (...
1answer
79 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 ...
1answer
433 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, ...
5answers
17k 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 ...
1answer
57 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: ...
3answers
1k 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 ...
2answers
192 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 ...
1answer
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(-...
2answers
306 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 ...
1answer
717 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 ...
3answers
583 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 ...
2answers
4k 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 ...
2answers
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 ...
2answers
3k 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 ...
2answers
533 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 ...
2answers
731 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 ...
3answers
40k views

### Is there something special about the number 65535?

2¹⁶-1 & 2⁵ = 2⁵ (or? obviously ?) A developer asked me today what is bitwise 65535 & 32 i.e. 2¹⁶-1 & 2⁵ = ? I thought at first spontaneously 32 but it seemed to easy whereupon I thought ...
1answer
1k views

### Dummy float values to force floating-point operations

It is common to use idioms such as: x/60.0 to force a floating-point division when x is an integer in languages which do not have distinct operators for integer and decimal division. Is this an ...