# Tag Info

## Hot answers tagged math

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
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
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
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

### 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
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
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 ...
• 100k

### 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
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

### 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

### 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

### 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

### 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

### 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
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
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 ...
• 100k
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'...
• 100k

### 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 ...
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
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

### 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

### 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

### 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
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

### 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
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 ...
• 17.9k