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
209
votes
17 answers

How to test if a double is an integer

Is it possible to do this? double variable; variable = 5; /* the below should return true, since 5 is an int. if variable were to equal 5.7, then it would return false. */ if(variable == int) { //do stuff } I know the code probably doesn't go…
JXPheonix
  • 2,618
  • 3
  • 16
  • 14
209
votes
2 answers

What does gcc's ffast-math actually do?

I understand gcc's --ffast-math flag can greatly increase speed for float ops, and goes outside of IEEE standards, but I can't seem to find information on what is really happening when it's on. Can anyone please explain some of the details and maybe…
Ponml
  • 2,917
  • 5
  • 20
  • 17
209
votes
26 answers

How to round a number to significant figures in Python

I need to round a float to be displayed in a UI. e.g, to one significant figure: 1234 -> 1000 0.12 -> 0.1 0.012 -> 0.01 0.062 -> 0.06 6253 -> 6000 1999 -> 2000 Is there a nice way to do this using the Python library, or do I have to write it…
Peter Graham
  • 11,323
  • 7
  • 40
  • 42
209
votes
7 answers

What's the difference between “mod” and “remainder”?

My friend said that there are differences between "mod" and "remainder". If so, what are those differences in C and C++? Does '%' mean either "mod" or "rem" in C?
songhir
  • 3,393
  • 3
  • 19
  • 27
204
votes
10 answers

What are the mathematical/computational principles behind this game?

My kids have this fun game called Spot It! The game constraints (as best I can describe) are: It is a deck of 55 cards On each card are 8 unique pictures (i.e. a card can't have 2 of the same picture) Given any 2 cards chosen from the deck, there…
Callmeed
  • 4,972
  • 5
  • 34
  • 49
204
votes
3 answers

How does the HyperLogLog algorithm work?

I've been learning about different algorithms in my spare time recently, and one that I came across which appears to be very interesting is called the HyperLogLog algorithm - which estimates how many unique items are in a list. This was particularly…
K2xL
  • 9,730
  • 18
  • 64
  • 101
199
votes
30 answers

Algorithm to find Largest prime factor of a number

What is the best approach to calculating the largest prime factor of a number? I'm thinking the most efficient would be the following: Find lowest prime number that divides cleanly Check if result of division is prime If not, find next lowest Go to…
mercutio
  • 22,151
  • 10
  • 36
  • 37
196
votes
22 answers

Unique (non-repeating) random numbers in O(1)?

I'd like to generate unique random numbers between 0 and 1000 that never repeat (i.e. 6 doesn't show up twice), but that doesn't resort to something like an O(N) search of previous values to do it. Is this possible?
dicroce
  • 45,396
  • 28
  • 101
  • 140
192
votes
13 answers

Why must a nonlinear activation function be used in a backpropagation neural network?

I've been reading some things on neural networks and I understand the general principle of a single layer neural network. I understand the need for aditional layers, but why are nonlinear activation functions used? This question is followed by this…
corazza
  • 31,222
  • 37
  • 115
  • 186
191
votes
18 answers

Evenly distributing n points on a sphere

I need an algorithm that can give me positions around a sphere for N points (less than 20, probably) that vaguely spreads them out. There's no need for "perfection", but I just need it so none of them are bunched together. This question provided…
Befall
  • 6,560
  • 9
  • 24
  • 29
188
votes
17 answers

What is the C++ function to raise a number to a power?

How do I raise a number to a power? 2^1 2^2 2^3 etc...
user98188
186
votes
9 answers

Why do Python's math.ceil() and math.floor() operations return floats instead of integers?

Can someone explain this (straight from the docs- emphasis mine): math.ceil(x) Return the ceiling of x as a float, the smallest integer value greater than or equal to x. math.floor(x) Return the floor of x as a float, the largest integer value less…
Yarin
  • 173,523
  • 149
  • 402
  • 512
186
votes
23 answers

Calculate the center point of multiple latitude/longitude coordinate pairs

Given a set of latitude and longitude points, how can I calculate the latitude and longitude of the center point of that set (aka a point that would center a view on all points)? EDIT: Python solution I've used: Convert lat/lon (must be in radians)…
zeke
  • 3,603
  • 2
  • 26
  • 41
186
votes
10 answers

Javascript: Round up to the next multiple of 5

I need a utility function that takes in an integer value (ranging from 2 to 5 digits in length) that rounds up to the next multiple of 5 instead of the nearest multiple of 5. Here is what I got: function round5(x) { return (x % 5) >= 2.5 ?…
Amit Erandole
  • 11,995
  • 23
  • 65
  • 103
183
votes
8 answers

Sort points in clockwise order?

Given an array of x,y points, how do I sort the points of this array in clockwise order (around their overall average center point)? My goal is to pass the points to a line-creation function to end up with something looking rather "solid", as convex…
Philipp Lenssen
  • 8,818
  • 13
  • 56
  • 77