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!

38791 questions
3313
votes
30 answers

Is floating point math broken?

Consider the following code: 0.1 + 0.2 == 0.3 -> false 0.1 + 0.2 -> 0.30000000000000004 Why do these inaccuracies happen?
Cato Johnston
  • 38,149
  • 10
  • 35
  • 42
1527
votes
35 answers

Fastest way to determine if an integer's square root is an integer

I'm looking for the fastest way to determine if a long value is a perfect square (i.e. its square root is another integer): I've done it the easy way, by using the built-in Math.sqrt() function, but I'm wondering if there is a way to do it faster…
Kip
  • 99,109
  • 82
  • 222
  • 258
1368
votes
37 answers

Determine Whether Two Date Ranges Overlap

Given two date ranges, what is the simplest or most efficient way to determine whether the two date ranges overlap? As an example, suppose we have ranges denoted by DateTime variables StartDate1 to EndDate1 and StartDate2 to EndDate2.
Ian Nelson
  • 51,299
  • 20
  • 72
  • 100
1225
votes
17 answers

How can I check for NaN values?

float('nan') results in Nan (not a number). But how do I check for it? Should be very easy, but I cannot find it.
Jack Ha
  • 15,691
  • 11
  • 34
  • 41
1201
votes
48 answers

Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing

I had an interesting job interview experience a while back. The question started really easy: Q1: We have a bag containing numbers 1, 2, 3, …, 100. Each number appears exactly once, so there are 100 numbers. Now one number is randomly picked out of…
polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
1080
votes
18 answers

How to perform an integer division, and separately get the remainder, in JavaScript?

In JavaScript, how do I get: The whole number of times a given integer goes into another? The remainder?
Yarin
  • 144,097
  • 139
  • 361
  • 489
1013
votes
21 answers

What is JavaScript's highest integer value that a number can go to without losing precision?

Is this defined by the language? Is there a defined maximum? Is it different in different browsers?
TALlama
  • 15,070
  • 8
  • 36
  • 46
991
votes
46 answers

Calculate distance between two latitude-longitude points? (Haversine formula)

How do I calculate the distance between two points specified by latitude and longitude? For clarification, I'd like the distance in kilometers; the points use the WGS84 system and I'd like to understand the relative accuracies of the approaches…
Robin Minto
  • 13,373
  • 3
  • 32
  • 40
845
votes
118 answers

Designing function f(f(n)) == -n

A question I got on my last interview: Design a function f, such that: f(f(n)) == -n Where n is a 32 bit signed integer; you can't use complex numbers arithmetic. If you can't design such a function for the whole range of numbers, design it for…
Hrvoje Prgeša
  • 2,021
  • 5
  • 21
  • 36
839
votes
28 answers

Understanding "randomness"

I can't get my head around this, which is more random? rand() OR: rand() * rand() I´m finding it a real brain teaser, could you help me out? EDIT: Intuitively I know that the mathematical answer will be that they are equally random, but I can't…
Trufa
  • 35,711
  • 41
  • 118
  • 180
781
votes
6 answers

How do I determine whether my calculation of pi is accurate?

I was trying various methods to implement a program that gives the digits of pi sequentially. I tried the Taylor series method, but it proved to converge extremely slowly (when I compared my result with the online values after some time). Anyway, I…
Ishan Sharma
  • 6,433
  • 3
  • 14
  • 19
691
votes
48 answers

Divide a number by 3 without using *, /, +, -, % operators

How would you divide a number by 3 without using *, /, +, -, %, operators? The number may be signed or unsigned.
Green goblin
  • 9,536
  • 13
  • 63
  • 97
630
votes
25 answers

How to check if a number is a power of 2

Today I needed a simple algorithm for checking if a number is a power of 2. The algorithm needs to be: Simple Correct for any ulong value. I came up with this simple algorithm: private bool IsPowerOfTwo(ulong number) { if (number == 0) …
configurator
  • 38,246
  • 13
  • 76
  • 112
585
votes
16 answers

How to sum array of numbers in Ruby?

I have an array of integers. For example: array = [123,321,12389] Is there any nice way to get the sum of them? I know, that sum = 0 array.each { |a| sum+=a } would work.
brainfck
  • 8,816
  • 7
  • 26
  • 29
512
votes
15 answers

What is the difference between '/' and '//' when used for division?

Is there a benefit to using one over the other? In Python 2, they both seem to return the same results: >>> 6/3 2 >>> 6//3 2
Ray
  • 169,974
  • 95
  • 213
  • 200
1
2 3
99 100