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
546
votes
12 answers

JavaScript math, round to two decimal places

I have the following JavaScript syntax: var discount = Math.round(100 - (price / listprice) * 100); This rounds up to the whole number. How can I return the result with two decimal places?
Smudger
  • 10,451
  • 29
  • 104
  • 179
517
votes
11 answers

What is the standard way to add N seconds to datetime.time in Python?

Given a datetime.time value in Python, is there a standard way to add an integer number of seconds to it, so that 11:34:59 + 3 = 11:35:02, for example? These obvious ideas don't work: >>> datetime.time(11, 34, 59) + 3 TypeError: unsupported operand…
Paul Stephenson
  • 67,682
  • 9
  • 49
  • 51
509
votes
21 answers

Is there a standard sign function (signum, sgn) in C/C++?

I want a function that returns -1 for negative numbers and +1 for positive numbers. http://en.wikipedia.org/wiki/Sign_function It's easy enough to write my own, but it seems like something that ought to be in a standard library somewhere. Edit:…
batty
  • 7,528
  • 9
  • 31
  • 30
498
votes
13 answers

Difference between Math.Floor() and Math.Truncate()

What is the difference between Math.Floor() and Math.Truncate() in .NET?
Anonymous User
  • 5,053
  • 2
  • 18
  • 3
436
votes
31 answers

Calculate distance between 2 GPS coordinates

How do I calculate distance between two GPS coordinates (using latitude and longitude)?
nicudotro
  • 7,129
  • 6
  • 25
  • 17
415
votes
7 answers

Fastest way to determine if an integer is between two integers (inclusive) with known sets of values

Is there a faster way than x >= start && x <= end in C or C++ to test if an integer is between two integers? UPDATE: My specific platform is iOS. This is part of a box blur function that restricts pixels to a circle in a given square. UPDATE: After…
jjxtra
  • 20,415
  • 16
  • 100
  • 140
396
votes
40 answers

Fastest way to list all primes below N

This is the best algorithm I could come up. def get_primes(n): numbers = set(range(n, 1, -1)) primes = [] while numbers: p = numbers.pop() primes.append(p) numbers.difference_update(set(range(p*2, n+1, p))) …
jbochi
  • 28,816
  • 16
  • 73
  • 90
389
votes
19 answers

How to round up the result of integer division?

I'm thinking in particular of how to display pagination controls, when using a language such as C# or Java. If I have x items which I want to display in chunks of y per page, how many pages will be needed?
Ian Nelson
  • 57,123
  • 20
  • 76
  • 103
385
votes
27 answers

How to evaluate a math expression given in string form?

I'm trying to write a Java routine to evaluate math expressions from String values like: "5+3" "10-4*5" "(1+10)*3" I want to avoid a lot of if-then-else statements. How can I do this?
Shah
  • 4,878
  • 8
  • 25
  • 33
382
votes
20 answers

Convert a number range to another range, maintaining ratio

I'm trying to convert one range of numbers to another, maintaining ratio. Maths is not my strong point. I have an image file where point values may range from -16000.00 to 16000.00 though the typical range may be much less. What I want to do is…
SpliFF
  • 38,186
  • 16
  • 91
  • 120
370
votes
13 answers

JavaScript % (modulo) gives a negative result for negative numbers

According to Google Calculator (-13) % 64 is 51. According to Javascript (see this JSBin) it is -13. How do I fix this?
Alec Gorge
  • 17,110
  • 10
  • 59
  • 71
369
votes
9 answers

How can I divide two integers to get a double?

How do I divide two integers to get a double?
leora
  • 188,729
  • 360
  • 878
  • 1,366
354
votes
11 answers

Fast ceiling of an integer division in C / C++

Given integer values x and y, C and C++ both return as the quotient q = x/y the floor of the floating point equivalent. I'm interested in a method of returning the ceiling instead. For example, ceil(10/5)=2 and ceil(11/5)=3. The obvious approach…
andand
  • 17,134
  • 11
  • 53
  • 79
351
votes
7 answers

What is "entropy and information gain"?

I am reading this book (NLTK) and it is confusing. Entropy is defined as: Entropy is the sum of the probability of each label times the log probability of that same label How can I apply entropy and maximum entropy in terms of text mining? Can…
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
336
votes
14 answers

How can I remove the decimal part from JavaScript number?

I have the results of a division and I wish to discard the decimal portion of the resultant number. How can I do this?
JacobTheDev
  • 17,318
  • 25
  • 95
  • 158