76
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). ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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++...
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 ...
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 ...
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 ...
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. ...
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 ...
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 ...
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 ...
9
votes
Accepted
Convert version string to integer
No, there's no bulletproof way unless you know the maximum values in advance (the maximum number of digits per component). You will also need that information to decode the number.
You're trying to ...
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 ...
8
votes
Convert version string to integer
If the only requirement is that each version string has a unique integer identifier, you can use a function like:
int(x.y.z) = 2^x * 3^y * 5^z
This is easily reversible by finding the prime ...
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 ...
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 ...
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 (...
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 ...
7
votes
What is the best data model to represent mathematical range (in database, xml, json...)?
If you're working solely with integer ranges, you're probably over-thinking the problem. All you need to store is the lower bound and the upper bound, and use a fixed rule about whether the bounds are ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
math × 284algorithms × 78
programming-languages × 14
python × 13
java × 12
c# × 11
language-agnostic × 11
floating-point × 11
geometry × 10
c++ × 9
javascript × 9
performance × 9
optimization × 9
data-structures × 8
random × 8
architecture × 7
php × 7
c × 7
functional-programming × 7
computer-science × 7
design × 6
testing × 6
libraries × 6
graph × 6
theory × 6