Hot answers tagged

96 votes
Accepted

Why is negative zero important?

You need to keep in mind that in FPU arithmetics, 0 doesn't necessarily has to mean exactly zero, but also value too small to be represented using given datatype, e.g. a = -1 / 1000000000000000000.0 ...
  • 2,776
75 votes
Accepted

Will a computer attempt to divide by zero?

The CPU has built in detection. Most instruction set architectures specify that the CPU will trap to an exception handler for integer divide by zero (I don't think it cares if the dividend is zero). ...
  • 32.4k
75 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 ...
  • 24.9k
68 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 ...
  • 1,246
65 votes

Why is Math.Sqrt() a static function?

Suppose we're designing a new language and we want Sqrt to be an instance method. So we look at the double class and begin designing. It obviously has no inputs (other than the instance) and returns ...
  • 1,175
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 ...
  • 38.7k
47 votes
Accepted

Why does Math.min work with a one element array

According to MDN Math.min accepts only numbers, and if one of the arguments is not a number, it'll return NaN. That's not what it says (bold emphasis mine): If at least one of arguments cannot be ...
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 ...
  • 36.7k
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 ...
  • 69.7k
34 votes

Will a computer attempt to divide by zero?

It depends on the language, on the compiler, on whether you are using integers or floating point numbers, and so on. For floating point number, most implementations use the IEEE 754 standard, where ...
  • 38.5k
25 votes

Why is Math.Sqrt() a static function?

Mathematical operations are often very performance-sensitive. Therefore, we will want to use static methods that can be fully resolved (and optimizied, or inlined) at compile time. Some languages do ...
  • 126k
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 ...
  • 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++...
  • 188k
23 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 ...
  • 13.2k
21 votes
Accepted

Why is Math.Sqrt() a static function?

It is entirely a choice of language design. It also depends on the underlying implementation of primitive types, and performance considerations due to that. .NET has just one static Math.Sqrt method ...
  • 326
19 votes
Accepted

Why is the "period of a (pseudo)random number generator" important?

Computers are not Turing Machines. They are Deterministic Finite State Machines. Turing Machines have infinite memory, computers have finite memory. Turing Machines have arbitrarily many (though ...
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'...
15 votes

Why is Math.Sqrt() a static function?

I would be motivated by the fact that there's a ton of special-purpose math functions, and rather than populate every math type with all (or a random subset) of those functions you put them in a ...
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 ...
  • 35.6k
14 votes
Accepted

What can one do to improve readability of math-oriented code in C#, Java, and similar?

You could define local functions that call the global static functions. Hopefully the compiler will inline the wrappers, and then the JIT compiler will produce tight assembly code for the actual ...
  • 2,460
13 votes

Will a computer attempt to divide by zero?

Seems like you're wondering what would happen if someone made a CPU that doesn't explicitly check for zero before dividing. What would happen depends entirely on the implementation of the division. ...
  • 163
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 ...
  • 7,894
12 votes

Why is negative zero important?

First, how do you create a -0? There are two ways: (1) do a floating-point operation where the mathematical result is negative, but so close to zero that it gets rounded to zero and not to a non-zero ...
  • 38.5k
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 ...
  • 304
11 votes

Why are floats called "real numbers" in some languages?

Short answer: because it is the default approximation of a real number the language in question provides.
  • 188k
11 votes
Accepted

In what programming language did the use of the percent sign (%) to mean modulo/remainder originate?

The earliest reference I can find is from the "B" programming language developed by Ken Thompson with Dennis Ritchie around 1969. B Reference As this was an implementation/improvement of BPCL I have ...
10 votes

get weighted random item

This is a simple Python implementation: from random import random def select(container, weights): total_weight = float(sum(weights)) rel_weight = [w / total_weight for w in weights] # ...
  • 4,146

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