Skip to main content
76 votes
Accepted

Is it good practice to replace division with multiplication when possible?

Two common cases to consider: Integer arithmetic Obviously if you are using integer arithmetic (which truncates) you will get a different result. Here's a small example in C#: public static void ...
John Wu's user avatar
  • 26.6k
71 votes
Accepted

Why was the caret used for XOR instead of exponentiation?

Although there were older precursors, the influential French mathematician Rene Descartes is usually credited for introducing superscripted exponents (ab) into mathematical writing, in his work ...
njuffa's user avatar
  • 1,306
54 votes
Accepted

What is the size of the number 65535 in bytes?

TL;DR The key takeaway here is that there is a world of difference between the number 65535 and a piece of text which represents the digits '6', '5', '5', '3' and '5'. It may look the same to you when ...
Flater's user avatar
  • 52.7k
46 votes

What is the mathematics foundation for first/second/third class values in programming languages?

There isn't any, and it's pretty arbitrary. The only useful distinction is between first class, and all others. Every case that's in the "other" bracket has its own distinct set of rules in each case ...
DeadMG's user avatar
  • 36.9k
38 votes
Accepted

Why is the norm of a complex number in the C++ complex library actually the square of the norm?

This is not a C++ library issue but a question of mathematical terminology. In mathematics, a norm can mean different things: What you call norm is the Euclidian norm, which is the distance to the ...
Christophe's user avatar
  • 78.7k
32 votes

Is it possible to build a system to generate UUIDs where every UUID is guaranteed unique?

there is a finite number of ids which can fit into some structure Correct. If you have n bits in your structure, after you have generated 2^n IDs, the next one must be a collision; this is the ...
Philip Kendall's user avatar
25 votes

Is it good practice to replace division with multiplication when possible?

I like your question as it potentially covers many ideas. On the whole, I suspect the answer is it depends, probably on the types involved and the possible range of values in your specific case. My ...
dave's user avatar
  • 351
25 votes

Why is the norm of a complex number in the C++ complex library actually the square of the norm?

Christophe's post, whilst fully correct, does not actually answer the question why the terms look like they do. To give you definite answer for the reasons, you would have to ask someone from the C++...
Doc Brown's user avatar
  • 210k
24 votes

Is it good practice to replace division with multiplication when possible?

No. I'd probably call that premature optimization, in a broad sense, regardless of whether you're optimizing for performance, as the phrase generally refers to, or anything else that can be ...
svidgen's user avatar
  • 15.1k
20 votes

Is it possible to build a system to generate UUIDs where every UUID is guaranteed unique?

You want version 1 UUIDs. They don't collide but they leak the generating computer's MAC address and require a monotonic clock. From RFC4122 we have this format 8 bytes time-low 2 bytes time-mid 2 ...
Joshua's user avatar
  • 1,664
17 votes
Accepted

Why do computer scientists seem to avoid closed-form solutions to mathematical problems?

Why do we implement Fibonacci numbers naively using its definition instead of using the explicit formula https://brilliant.org/discussions/thread/the-explicit-formula-for-fibonacci-sequence/. We don'...
Jörg W Mittag's user avatar
15 votes
Accepted

Is it possible to store N bits of unique combinations, in N-1 bits? If not; why does MD5 get reprimanded for collissions?

Of course, the pigeonhole principle states that colisions are inevitable for hashing algorithms. The point of hashing algorithms is not to prevent colisions. But to make intentional collisions ...
Euphoric's user avatar
  • 37.5k
14 votes

Is it possible to build a system to generate UUIDs where every UUID is guaranteed unique?

Is it possible, when K different hosts are generating IDs? Yes. And we can do it with readily available libraries. ULIDs do a pretty good job already, but they're not collision free. It looks like the ...
J_H's user avatar
  • 7,600
13 votes

Is it good practice to replace division with multiplication when possible?

Use whichever one is less buggy and makes more logical sense. Usually, division by a variable is a bad idea anyway, since usually, the divisor can be zero. Division by a constant is usually just ...
user541686's user avatar
  • 8,112
12 votes
Accepted

Swapping variable values without creating a new one

That is a longer and less efficient variant of XOR swap algorithm. I'll just reiterate most-commonly known solutions here from the wiki: # solution 1 A = A + B B = A - B A = A - B # solution 2 (^ is ...
mostruash's user avatar
  • 304
11 votes

Is it possible to build a system to generate UUIDs where every UUID is guaranteed unique?

It is easy enough, but require some degree of coordination. The general scheme is relatively simple: partition the bit-space of the ID in two sections, one with a "generator ID" unique to ...
Matthieu M.'s user avatar
  • 15.1k
10 votes

Interdependent variables

As you describe it, b is not a variable at all. Instead, it is a linear function of a. The solution is to create a method that computes the value for b from a and use it wherever you were going to ...
Kilian Foth's user avatar
9 votes
Accepted

Storing a straight line equation

The equations can be all rewritten into form : a*X + b*Y + c = 0 That means you can store three doubles a, b and c. This doesn't have a problem in representing an arbitrary slope. You can also ...
Euphoric's user avatar
  • 37.5k
8 votes
Accepted

Smallest Rubik's cube state representation

You can encode the 8! possible permutations of the corners in 16 bits, and the 12! possible permutations in 29 bits, by numbering the permutations from 1 to 8! (or 1 to 12!), and storing the number. I ...
Doc Brown's user avatar
  • 210k
8 votes
Accepted

Integrating TeX into a Java desktop application

I have no experience with those specific libraries and tools, but since you asked this here on SE.SE, let me give you an answer on how to approach such an evaluation process as a software engineer. It ...
Doc Brown's user avatar
  • 210k
8 votes
Accepted

Why is What Every Computer Scientist Should Know About Floating-point Arithmetic considered mandatory reading?

Why is this the case? What aspects of the article make it stand out as still being important to this day? This part all sounds terribly opinion-based. Upon my reading of it, I found that a lot of is ...
Useless's user avatar
  • 12.6k
8 votes

What is the viability of engineering an integral type with values ranging from -1 to 254? Do types like this even exist?

You're taking the use specifically of -1 too literally here. APIs like this didn't really pick -1; they're either picking any negative number, or they're picking an integer with all of its bits set (...
Nicol Bolas's user avatar
8 votes

What is the size of the number 65535 in bytes?

There are likely several variables at play here, but 65535 is 5 characters. 1 byte per character = 5 bytes. Text editors don't have a notion of what a number is the same way that your code does, so ...
mmathis's user avatar
  • 5,498
7 votes

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

One reason might be simply that programmers are more used to decimal number representations, or that their arbitrary-precision library does not support rationals. Another reason might be an additional ...
Martin Nyolt's user avatar
7 votes

What is the mathematics foundation for first/second/third class values in programming languages?

Denotational Semantics provides a mathematic bases for describing how values and variables works in a programming language. It was explained so well in my Computer Sci Degree that I got a top mark ...
Ian's user avatar
  • 4,603
7 votes
Accepted

How do I unit test nuanced mathematical procedures?

Tests are always just examples – they can never prove correctness. But even if the test space is vast we can extract a lot of value a few from well-chosen examples. Many inputs are basically ...
amon's user avatar
  • 135k
7 votes
Accepted

How a Proof Assistant Proves ∀ or ∃

In general, #2. The language takes the declared initial states and uses Boolean logic transitions to make sure that there are no contradictions in the interactions between all of the rules defined. ...
Telastyn's user avatar
  • 109k
6 votes

Why is the norm of a complex number in the C++ complex library actually the square of the norm?

Aside from the already given reasons, there is a strong performance reason: taking the square root is significantly slower than the other needed calculations. For many applications, the returned ...
Aganju's user avatar
  • 1,463
6 votes
Accepted

How to show fluctuating data smoothly and accurately?

As @amon already noted, domain knowledge may help to find an appropriate function. For a vehicle tank you need to consider two types of value change: slow fuel consumption and fast fill. There are ...
Hans-Martin Mosner's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible