Programming questions that are significantly affected or best defined by the underlying mathematics of the problem.
Questions tagged [math]
283 questions
92
votes
26 answers
What does mathematics have to do with programming?
I just started a diploma in software development. Right now we're starting out with basic Java and such (so right from the bottom you might say) - which is fine, I have no programming experience apart from knowing how to do "Hello World" in Java.
I…

Rory
- 237
- 1
- 4
- 3
85
votes
6 answers
Why is negative zero important?
I'm confused about why we care about different representations for positive and negative zero.
I vaguely recall reading claims that having a negative zero representation is extremely important in programming that involves complex numbers. I've never…

jpmc26
- 5,389
- 4
- 25
- 37
78
votes
67 answers
Do you have to be good at math to be a good programmer?
It seems that conventional wisdom suggests that good programmers are also good at math. Or that the two are somehow intrinsically linked. Many programming books I have read provide many examples that are solutions to math problems, or are somehow…

Charles Roper
- 1,363
- 1
- 13
- 14
75
votes
10 answers
Is it good practice to replace division with multiplication when possible?
Whenever I need division, for example, condition checking, I would like to refactor the expression of division into multiplication, for example:
Original version:
if(newValue / oldValue >= SOME_CONSTANT)
New version:
if(newValue >= oldValue *…

ocomfd
- 5,652
- 8
- 29
- 37
62
votes
1 answer
Treating a 1D data structure as 2D grid
I am working with a native class that represents a 2D image as a 1D array. If you want to change one pixel, for example, you need to now how to derive the index from the x,y coordinates.
So, let's say we have a 1D array array1d like this:
array1d =…

GladstoneKeep
- 2,629
- 4
- 19
- 15
61
votes
5 answers
Will a computer attempt to divide by zero?
We all know 0/0 is Undefined and returns an error if I were to put it into a calculator, and if I were to create a program (in C at least) the OS would terminate it when I try to divide by zero.
But what I've been wondering is if the computer even…

Ankush
- 821
- 1
- 8
- 12
58
votes
5 answers
get weighted random item
I have, for example, this table
+-----------------+
| fruit | weight |
+-----------------+
| apple | 4 |
| orange | 2 |
| lemon | 1 |
+-----------------+
I need to return a random fruit. But apple should be picked 4 times as…

fl00r
- 691
- 1
- 6
- 8
53
votes
14 answers
Why does DirectX use a left-handed coordinate system?
I considered posting on Stack Overflow, but the question strikes me as being far too subjective since I can't think of a reasonable technical explanation for Microsoft's choice in this matter. But this question has bugged me for so long and the…

greyfade
- 11,103
- 2
- 40
- 43
51
votes
11 answers
Is there a canonical book on mathematics for programmers?
I'm a self-taught programmer. I am honestly not good in math. What advice you can give to improve my Mathematical skills so that I will not be so insecure around my fellow programmers? What are the steps or guidelines that you can recommend to…

adietan63
- 735
- 1
- 7
- 9
40
votes
12 answers
How to check if 4 points form a square?
Assume I have 4 points (they are 2-dimension), which are different from each other, and I want to know whether they form a square. How to do it? (let the process be as simple as possible.)

MarshalSHI
- 511
- 1
- 4
- 8
37
votes
1 answer
Why was the caret used for XOR instead of exponentiation?
Not that it's really a problem for anyone that has faced this syntactic issue before, but I see a wild amount of confusion stemming from the use of the caret (^) as the XOR operation in lieu of the widely accepted mathematical exponentiation…

Qix - MONICA WAS MISTREATED
- 1,896
- 16
- 32
36
votes
4 answers
Prerequisite math skill for Introduction to Algorithms (CLRS) book
I already have knowledge about basic algorithms. Now I plan to study more advance algorithms and I decide to go with Introduction to Algorithms.
I'm not sure, do I need to refresh my math's skill before read this book or not? (I forget almost math…

Anonymous
- 2,029
- 3
- 22
- 24
36
votes
16 answers
Why use other number bases when programming
My coworkers and I have been bending our minds to figuring out why anyone would go out of their way to program numbers in a base other than base 10.
I suggested that perhaps you could optimize longer equations by putting the variables in the correct…

JMD
- 530
- 4
- 9
35
votes
11 answers
Why do we still use floats?
I understand why floats served a purpose in the past. And I think I can see why they're useful in some simulation examples today. But I think those example are more exceptional than common. So I don't understand why floats are more prevalent in…
anon
32
votes
10 answers
Why is Math.Sqrt() a static function?
In a discussion about static and instance methods, I always think, that Sqrt() should be a instance method of number types instead of a static method. Why is that? It obviously works on a value.
// looks wrong to me
var y = Math.Sqrt(x);
// looks…

Residuum
- 3,282
- 28
- 31