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
269
votes
8 answers

How to scale down a range of numbers with a known min and max value

So I am trying to figure out how to take a range of numbers and scale the values down to fit a range. The reason for wanting to do this is that I am trying to draw ellipses in a java swing jpanel. I want the height and width of each ellipse to be…
user650271
  • 2,853
  • 3
  • 17
  • 8
265
votes
26 answers

Too many 'if' statements?

The following code does work how I need it to, but it's ugly, excessive or a number of other things. I've looked at formulas and attempted to write a few solutions, but I end up with a similar amount of statements. Is there a type of math formula…
TomFirth
  • 2,334
  • 4
  • 20
  • 31
263
votes
24 answers

Remove insignificant trailing zeros from a number?

Have I missed a standard API call that removes trailing insignificant zeros from a number? var x = 1.234000; // to become 1.234 var y = 1.234001; // stays 1.234001 Number.toFixed() and Number.toPrecision() are not quite what I'm looking for.
Steven
  • 2,789
  • 2
  • 18
  • 12
255
votes
15 answers

Mod of negative number is melting my brain

I'm trying to mod an integer to get an array position so that it will loop round. Doing i % arrayLength works fine for positive numbers but for negative numbers it all goes wrong. 4 % 3 == 1 3 % 3 == 0 2 % 3 == 2 1 % 3 == 1 0 % 3 == 0 -1 % 3…
gormenghastly
252
votes
1 answer

Rounding BigDecimal to *always* have two decimal places

I'm trying to round BigDecimal values up, to two decimal places. I'm using BigDecimal rounded = value.round(new MathContext(2, RoundingMode.CEILING)); logger.trace("rounded {} to {}", value, rounded); but it doesn't do what I want…
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
251
votes
10 answers

How can I ensure that a division of integers is always rounded up?

I want to ensure that a division of integers is always rounded up if necessary. Is there a better way than this? There is a lot of casting going on. :-) (int)Math.Ceiling((double)myInt1 / myInt2)
Karsten
  • 8,015
  • 8
  • 48
  • 83
250
votes
6 answers

How do I calculate a point on a circle’s circumference?

How can the following function be implemented in various languages? Calculate the (x,y) point on the circumference of a circle, given input values of: Radius Angle Origin (optional parameter, if supported by the language)
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
247
votes
3 answers

Why does Python's hash of infinity have the digits of π?

The hash of infinity in Python has digits matching pi: >>> inf = float('inf') >>> hash(inf) 314159 >>> int(math.pi*1e5) 314159 Is that just a coincidence or is it intentional?
wim
  • 338,267
  • 99
  • 616
  • 750
235
votes
19 answers

Why prefer two's complement over sign-and-magnitude for signed numbers?

I'm just curious if there's a reason why in order to represent -1 in binary, two's complement is used: flipping the bits and adding 1? -1 is represented by 11111111 (two's complement) rather than (to me more intuitive) 10000001 which is binary 1…
Ray
  • 3,468
  • 8
  • 26
  • 27
227
votes
10 answers

Why use softmax as opposed to standard normalization?

In the output layer of a neural network, it is typical to use the softmax function to approximate a probability distribution: This is expensive to compute because of the exponents. Why not simply perform a Z transform so that all outputs are…
Tom
  • 6,601
  • 12
  • 40
  • 48
224
votes
29 answers

Circle line-segment collision detection algorithm?

I have a line from A to B and a circle positioned at C with the radius R. What is a good algorithm to use to check whether the line intersects the circle? And at what coordinate along the circles edge it occurred?
Mizipzor
  • 51,151
  • 22
  • 97
  • 138
221
votes
10 answers

How can I specify the base for Math.log() in JavaScript?

I need a log function for JavaScript, but it needs to be base 10. I can't see any listing for this, so I'm assuming it's not possible. Are there any math wizards out there who know a solution for this?
MetaGuru
  • 42,847
  • 67
  • 188
  • 294
218
votes
13 answers

How to transform black into any given color using only CSS filters

My question is: given a target RGB color, what is the formula to recolor black (#000) into that color using only CSS filters? For an answer to be accepted, it would need to provide a function (in any language) that would accept the target color as…
glebm
  • 20,282
  • 8
  • 51
  • 67
214
votes
4 answers

numpy max vs amax vs maximum

numpy has three different functions which seem like they can be used for the same things --- except that numpy.maximum can only be used element-wise, while numpy.max and numpy.amax can be used on particular axes, or all elements. Why is there more…
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
210
votes
4 answers

How do I calculate the normal vector of a line segment?

Suppose I have a line segment going from (x1,y1) to (x2,y2). How do I calculate the normal vector perpendicular to the line? I can find lots of stuff about doing this for planes in 3D, but no 2D stuff. Please go easy on the maths (links to worked…
Piku
  • 3,526
  • 6
  • 35
  • 38