Questions tagged [arithmetic]

Questions about implementing elementary arithmetic operations on a computer with hardware or algorithms. The numbers are often assumed to be in a binary representation, add the [floating-point] tag for arithmetic operations on numbers in a floating point representation.

Computer Arithmetic, a subset of Computer Architecture, deals with methods of representing integers and real values (e.g., fixed- and floating-point numbers) in digital systems and efficient algorithms for manipulating such numbers by means of hardware circuits or software routines.

On the hardware side, various types of adders, subtractors, multipliers, dividers, square-rooters, and circuit techniques for function evaluation are considered.

Software aspects of computer arithmetic include complexity, error characteristics, stability, and certifiability of computational algorithms.

Reference and further learning: https://www.ece.ucsb.edu/~parhami/pubs_folder/parh02-arith-encycl-infosys.pdf

302 questions
78
votes
9 answers

Why is addition as fast as bit-wise operations in modern processors?

I know that bit-wise operations are so fast on modern processors, because they can operate on 32 or 64 bits on parallel, so bit-wise operations take only one clock cycle. However addition is a complex operation that consists of at least one and…
60
votes
5 answers

Factorial algorithm more efficient than naive multiplication

I know how to code for factorials using both iterative and recursive (e.g. n * factorial(n-1) for e.g.). I read in a textbook (without been given any further explanations) that there is an even more efficient way of coding for factorials by dividing…
user65165
  • 745
  • 1
  • 7
  • 7
54
votes
8 answers

The math behind converting from any base to any base without going through base 10?

I've been looking into the math behind converting from any base to any base. This is more about confirming my results than anything. I found what seems to be my answer on mathforum.org but I'm still not sure if I have it right. I have the…
Griffin
  • 653
  • 1
  • 6
  • 7
34
votes
8 answers

What is most efficient for GCD?

I know that Euclid’s algorithm is the best algorithm for getting the GCD (great common divisor) of a list of positive integers. But in practice you can code this algorithm in various ways. (In my case, I decided to use Java, but C/C++ may be another…
Jonathan Prieto-Cubides
  • 2,229
  • 3
  • 18
  • 26
29
votes
6 answers

Why Do Computers Use the Binary Number System (0,1)?

Why Do Computers Use the Binary Number System (0,1)? Why don't they use Ternary Number System (0,1,2) or any other number system instead?
Rai Ammad Khan
  • 449
  • 1
  • 5
  • 6
19
votes
5 answers

Confused about XORing and addition modulo $2$

It's my understanding that when you XOR something, the result is the sum of the two numbers mod $2$. Why then does $4 \oplus 2 = 6$ and not $0$? $4+2=6$, $6%2$ doesn't equal $6$. I must be missing something about what "addition modulo 2" means, but…
snerd
  • 404
  • 1
  • 3
  • 9
18
votes
5 answers

Signed and unsigned numbers

How would the ALU in a microprocessor differentiate between a signed number, -7 that is denoted by 1111 and an unsigned number 15, also denoted by 1111?
penguin99
  • 303
  • 2
  • 11
16
votes
5 answers

Time complexity of addition

Wikipedia lists the time complexity of addition as $n$, where $n$ is the number of bits. Is this a rigid theoretical lower bound? Or is this just the complexity of the current fastest known algorithm. I want to know, because the complexity of…
Tobi Alafin
  • 1,647
  • 4
  • 17
  • 22
15
votes
4 answers

Inequality caused by float inaccuracy

At least in Java, if I write this code: float a = 1000.0F; float b = 0.00004F; float c = a + b + b; float d = b + b + a; boolean e = c == d; the value of $e$ would be $false$. I believe this is caused by the fact that floats are very limited in the…
Known Zeta
  • 327
  • 1
  • 2
  • 8
14
votes
3 answers

Time complexity of base conversion

Why can't arbitrary base conversion be as fast as converting from base $b$ to base $b^k$ ? Seems to be a big time complexity difference! I am also interested in reading material about it. Old. Original detailed question Conversion between…
13
votes
3 answers

How does 0 have two values in one's complement?

It is said that in 2's complement 0 has only one value, while in 1's complement both +0 and -0 have separate values. What are they?
user136782
  • 233
  • 1
  • 2
  • 5
13
votes
2 answers

Standard constructive definitions of integers, rationals, and reals?

Natural numbers are defined inductively as (using Coq syntax as an example) Inductive nat: Set := | O: nat | S: nat -> nat. Is there a standard way to define integers (and maybe other sets like rationals and reals) constructively?
Alex
  • 273
  • 1
  • 5
10
votes
1 answer

Minimal basis for set of binary vectors using XOR

I would be surprised if this isn't a well-studied problem, but I'm not sure what else to search for at this point: you're given a set of binary $n$-vectors $S \subset \{0,1\}^n$. The problem is to find another set of binary $n$-vectors $B \subset…
10
votes
8 answers

Represent a real number without loss of precision

Current floating point (ANSI C float, double) allow to represent an approximation of a real number. Is there any way to represent real numbers without errors? Here's an idea I had, which is anything but perfect. For example, 1/3 is…
10
votes
1 answer

Why does floating point modulus exactness matters?

Most Smalltalk dialects currently implement a naive inexact floating modulus (fmod/remainder). I just changed this to improve Squeak/Pharo and eventually other Smalltalk adherence to standards (IEEE 754, ISO/IEC 10967), as I already did for other…
aka.nice
  • 201
  • 1
  • 4
1
2 3
20 21