Questions tagged [math]

Math involves the manipulation of numbers within a program. For general math questions, please ask on math.stackexchange.com. Note: If your question is about unexpected results in floating point calculations, please read https://stackoverflow.com/questions/588004/is-floating-point-math-broken first.

Mathematics is the study of quantity, structure, space, and change. Mathematicians seek out patterns, formulate new conjectures, and establish truth by rigorous deduction from appropriately chosen axioms and definitions.

Through the use of abstraction and logical reasoning, mathematics evolved from counting, calculation, measurement, and the systematic study of the shapes and motions of physical objects. Practical mathematics has been a human activity for as far back as written records exist.

Mathematics is behind all programming at some level, but math questions here should be specifically related to a programmed implementation. General math questions can be asked at Mathematics. Research level mathematics questions can be asked at MathOverflow.

Notable questions that may be dupe targets. Check these questions before asking again!

43708 questions
335
votes
25 answers

How to determine if a point is in a 2D triangle?

Is there an easy way to determine if a point is inside a triangle? It's 2D, not 3D.
ET 0.618
  • 3,433
  • 4
  • 17
  • 5
331
votes
25 answers

How to determine if a list of polygon points are in clockwise order?

Having a list of points, how do I find if they are in clockwise order? For example: point[0] = (5,0) point[1] = (6,4) point[2] = (4,5) point[3] = (1,5) point[4] = (1,0) would say that it is anti-clockwise (or counter-clockwise, for some people).
Stécy
  • 11,951
  • 16
  • 64
  • 89
314
votes
22 answers

Why can't decimal numbers be represented exactly in binary?

There have been several questions posted to SO about floating-point representation. For example, the decimal number 0.1 doesn't have an exact binary representation, so it's dangerous to use the == operator to compare it to another floating-point…
Barry Brown
  • 20,233
  • 15
  • 69
  • 105
309
votes
22 answers

How does C compute sin() and other math functions?

I've been poring through .NET disassemblies and the GCC source code, but can't seem to find anywhere the actual implementation of sin() and other math functions... they always seem to be referencing something else. Can anyone help me find them? I…
Hank
  • 8,289
  • 12
  • 47
  • 57
307
votes
7 answers

Why is division in Ruby returning an integer instead of decimal value?

For example: 9 / 5 #=> 1 but I expected 1.8. How can I get the correct decimal (non-integer) result? Why is it returning 1 at all?
ErwinM
  • 5,051
  • 2
  • 23
  • 35
306
votes
2 answers

Is there a math nCr function in Python?

Is there a built-in nCr (n choose r) function included in the Python math library like the one shown below? I understand that the computation can be programmed, but I thought I'd check to see if it's built-in before I do.
James Mertz
  • 8,459
  • 11
  • 60
  • 87
306
votes
20 answers

The most efficient way to implement an integer based power function pow(int, int)

What is the most efficient way given to raise an integer to the power of another integer in C? // 2^3 pow(2,3) == 8 // 5^5 pow(5,5) == 3125
Doug T.
  • 64,223
  • 27
  • 138
  • 202
298
votes
23 answers

Generate a random point within a circle (uniformly)

I need to generate a uniformly random point within a circle of radius R. I realize that by just picking a uniformly random angle in the interval [0 ... 2π), and uniformly random radius in the interval (0 ... R) I would end up with more points…
aioobe
  • 413,195
  • 112
  • 811
  • 826
296
votes
20 answers

Mapping two integers to one, in a unique and deterministic way

Imagine two positive integers A and B. I want to combine these two into a single integer C. There can be no other integers D and E which combine to C. So combining them with the addition operator doesn't work. Eg 30 + 10 = 40 = 40 + 0 = 39 +…
harm
  • 10,045
  • 10
  • 36
  • 41
281
votes
11 answers

What are the most widely used C++ vector/matrix math/linear algebra libraries, and their cost and benefit tradeoffs?

It seems that many projects slowly come upon a need to do matrix math, and fall into the trap of first building some vector classes and slowly adding in functionality until they get caught building a half-assed custom linear algebra library, and…
Catskul
  • 17,916
  • 15
  • 84
  • 113
279
votes
9 answers

Safest way to convert float to integer in python?

Python's math module contain handy functions like floor & ceil. These functions take a floating point number and return the nearest integer below or above it. However these functions return the answer as a floating point number. For example: import…
Boaz
  • 25,331
  • 21
  • 69
  • 77
278
votes
12 answers

How can I round down a number in Javascript?

How can I round down a number in JavaScript? math.round() doesn't work because it rounds it to the nearest decimal. I'm not sure if there is a better way of doing it other than breaking it apart at the decimal point at keeping the first bit. There…
Ben Shelock
  • 20,154
  • 26
  • 92
  • 125
271
votes
6 answers

What is the behavior of integer division?

For example, int result; result = 125/100; or result = 43/100; Will result always be the floor of the division? What is the defined behavior?
T.T.T.
  • 33,367
  • 47
  • 130
  • 168
271
votes
24 answers

How to make rounded percentages add up to 100%

Consider the four percentages below, represented as float numbers: 13.626332% 47.989636% 9.596008% 28.788024% ----------- 100.000000% I need to represent these percentages as whole numbers. If I simply use Math.round(), I end…
poezn
  • 4,009
  • 4
  • 25
  • 27
270
votes
10 answers

Is log(n!) = Θ(n·log(n))?

I am to show that log(n!) = Θ(n·log(n)). A hint was given that I should show the upper bound with nn and show the lower bound with (n/2)(n/2). This does not seem all that intuitive to me. Why would that be the case? I can definitely see how to…
Mark
  • 6,123
  • 13
  • 41
  • 52