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
183
votes
5 answers

Mod in Java produces negative numbers

When I calculate int i = -1 % 2 I get -1 in Java. In Python, I get 1 as the result of -1 % 2. What do I have to do to get the same behavior in Java with the modulo function?
Christian
  • 25,249
  • 40
  • 134
  • 225
178
votes
19 answers

How to convert latitude or longitude to meters?

If I have a latitude or longitude reading in standard NMEA format is there an easy way / formula to convert that reading to meters, which I can then implement in Java (J9)? Edit: Ok seems what I want to do is not possible easily, however what I…
Adam Taylor
  • 7,534
  • 8
  • 44
  • 54
178
votes
7 answers

Java Round up Any Number

I can't seem to find the answer I'm looking for regarding a simple question: how do I round up any number to the nearest int? For example, whenever the number is 0.2, 0.7, 0.2222, 0.4324, 0.99999 I would want the outcome to be 1. So far I have int b…
Stevanicus
  • 7,561
  • 9
  • 49
  • 70
177
votes
16 answers

How to tell whether a point is to the right or left side of a line

I have a set of points. I want to separate them into 2 distinct sets. To do this, I choose two points (a and b) and draw an imaginary line between them. Now I want to have all points that are left from this line in one set and those that are right…
Aaginor
  • 4,516
  • 11
  • 51
  • 75
176
votes
12 answers

What is the method for converting radians to degrees?

I run into this occasionally and always forget how to do it. One of those things that pop up ever so often. Also, what's the formula to convert angles expressed in radians to degrees and back again?
Hans Sjunnesson
  • 21,745
  • 17
  • 54
  • 63
175
votes
32 answers

Least common multiple for 3 or more numbers

How do you calculate the least common multiple of multiple numbers? So far I've only been able to calculate it between two numbers. But have no idea how to expand it to calculate 3 or more numbers. So far this is how I did it LCM = num1 * num2 / …
paan
  • 7,054
  • 8
  • 38
  • 44
172
votes
3 answers

Should I use scipy.pi, numpy.pi, or math.pi?

In a project using SciPy and NumPy, should I use scipy.pi, numpy.pi, or math.pi?
Douglas B. Staple
  • 10,510
  • 8
  • 31
  • 58
166
votes
31 answers

How do you calculate the average of a set of circular data?

I want to calculate the average of a set of circular data. For example, I might have several samples from the reading of a compass. The problem of course is how to deal with the wraparound. The same algorithm might be useful for a clockface. The…
Nick Fortescue
  • 43,045
  • 26
  • 106
  • 134
166
votes
14 answers

Evaluating a mathematical expression in a string

stringExp = "2^4" intVal = int(stringExp) # Expected value: 16 This returns the following error: Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 10: '2^4' I know that…
Pieter
  • 31,619
  • 76
  • 167
  • 242
163
votes
3 answers

How to get mathemical PI constant in Swift

I am trying to find a way to include the PI constant in my Swift code. I already found help in another answer, to import Darwin which I know gives me access to C functions. I also checked the Math package in Darwin and came across the following…
nburk
  • 22,409
  • 18
  • 87
  • 132
160
votes
30 answers

How to create the most compact mapping n → isprime(n) up to a limit N?

Naturally, for bool isprime(number) there would be a data structure I could query. I define the best algorithm, to be the algorithm that produces a data structure with lowest memory consumption for the range (1, N], where N is a constant. Just an…
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
159
votes
18 answers

How can I get a count of the total number of digits in a number?

How can I get a count of the total number of digits of a number in C#? For example, the number 887979789 has 9 digits.
Arash
  • 3,013
  • 10
  • 52
  • 74
157
votes
25 answers

How can I check if two segments intersect?

How can I check if 2 segments intersect? I've the following data: Segment1 [ {x1,y1}, {x2,y2} ] Segment2 [ {x1,y1}, {x2,y2} ] I need to write a small algorithm in Python to detect if the 2 lines are intersecting.
aneuryzm
  • 63,052
  • 100
  • 273
  • 488
156
votes
19 answers

Performance of Java matrix math libraries?

We are computing something whose runtime is bound by matrix operations. (Some details below if interested.) This experience prompted the following question: Do folk have experience with the performance of Java libraries for matrix math (e.g.,…
dfrankow
  • 20,191
  • 41
  • 152
  • 214
153
votes
9 answers

How do I compute derivative using Numpy?

How do I calculate the derivative of a function, for example y = x2+1 using numpy? Let's say, I want the value of derivative at x = 5...
DrStrangeLove
  • 11,227
  • 16
  • 59
  • 72