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
3861
votes
33 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?
1609
votes
39 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
  • 57,123
  • 20
  • 76
  • 103
1601
votes
37 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
  • 107,154
  • 87
  • 232
  • 265
1580
votes
19 answers

How to check for NaN values

float('nan') represents NaN (not a number). But how do I check for it?
Jack Ha
  • 19,661
  • 11
  • 37
  • 41
1336
votes
19 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
  • 173,523
  • 149
  • 402
  • 512
1275
votes
49 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
  • 376,812
  • 128
  • 561
  • 623
1143
votes
49 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
  • 15,027
  • 4
  • 37
  • 40
1088
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
  • 16,017
  • 8
  • 38
  • 47
847
votes
120 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,051
  • 5
  • 21
  • 36
845
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
  • 39,971
  • 43
  • 126
  • 190
792
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,545
  • 3
  • 16
  • 21
710
votes
31 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
  • 40,828
  • 14
  • 81
  • 115
701
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,898
  • 13
  • 71
  • 100
620
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
  • 9,286
  • 7
  • 28
  • 29
581
votes
16 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
  • 187,153
  • 97
  • 222
  • 204
1
2 3
99 100