Tag: math

58 votes
12 answers
930 views
Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?

I am doing some numerical optimization on a scientific application. One thing I noticed is that GCC will optim

avatar
Venice
0 1 0 9
asked 07 September, 2021
60 votes
5 answers
476 views
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 circumfe

avatar
Joseph
0 1 0 9
asked 07 September, 2021
57 votes
1 answers
2.8K views
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 = va

avatar
Rufus
0 1 0 9
asked 07 September, 2021
52 votes
3 answers
1.1K views
Why does Python's hash of infinity have the digits of π?

The hash of infinity in Python has digits matching pi: >>> inf = float('inf') >>>

avatar
Xavier
0 1 0 9
asked 07 September, 2021
53 votes
9 answers
2.8K views
Is log(n!) = Θ(n·log(n))?

I am to show that log(n!) = Θ(n·log(n)). A hint was given that I should show the upper bound with nn and sh

avatar
Santiago
0 1 0 9
asked 07 September, 2021
60 votes
11 answers
2.4K views
How can I round down a number in Javascript?

How can I round down a number in JavaScript? math.round() doesn't work because it rounds it to the neare

avatar
Rufus
0 1 0 9
asked 07 September, 2021
58 votes
10 answers
2.7K views
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 thi

avatar
Robt
0 1 0 9
asked 07 September, 2021
63 votes
6 answers
1.9K views
What is the behavior of integer division?

For example, int result; result = 125/100; or result = 43/100; Will result always be the floor of the

avatar
Hallie
0 1 0 9
asked 07 September, 2021
62 votes
8 answers
1.5K views
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 rea

avatar
Keila
0 1 0 9
asked 07 September, 2021
58 votes
11 answers
505 views
Safest way to convert float to integer in python?

Python's math module contain handy functions like floor & ceil. These functions take a floating point

avatar
Karolyn
0 1 0 9
asked 07 September, 2021
57 votes
21 answers
600 views
Generate a random point within a circle (uniformly)

I need to generate a uniformly random point within a circle of radius R. I realize that by just picking a uni

avatar
Wilber
0 1 0 9
asked 07 September, 2021
48 votes
19 answers
456 views
Mapping two integers to one, in a unique and deterministic way

Imagine two positive integers A and B. I want to combine these two into a single integer C. There can be no

avatar
Benton
0 1 0 9
asked 07 September, 2021
66 votes
26 answers
2.8K views
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&ap

avatar
Stacia
0 1 0 9
asked 07 September, 2021
63 votes
22 answers
1.1K views
How does C compute sin() and other math functions?

I've been poring through .NET disassemblies and the GCC source code, but can't seem to find anywhere

avatar
Thurman
0 1 0 9
asked 07 September, 2021
56 votes
19 answers
646 views
The most efficient way to implement an integer based power function pow(int, int)

What is the most efficient way given to raise an integer to the power of another integer in C? // 2^3 pow(2,3

avatar
Stacia
0 1 0 9
asked 07 September, 2021
61 votes
13 answers
1.2K views
Calculating arithmetic mean (one type of average) in Python

Is there a built-in or standard library method in Python to calculate the arithmetic mean (one type of average

avatar
Juliane
0 1 0 9
asked 07 September, 2021
62 votes
7 answers
940 views
Why is division in Ruby returning an integer instead of decimal value?

For example: 9 / 5 #=> 1 but I expected 1.8. How can I get the correct decimal (non-integer) result? Wh

avatar
Robt
0 1 0 9
asked 07 September, 2021
53 votes
23 answers
2K views
How to determine if a list of polygon points are in clockwise order?

Having a list of points, how do I find if they are in clockwise order? For example: point[0] = (5,0) point[1

avatar
Charis
0 1 0 9
asked 07 September, 2021
63 votes
21 answers
2.4K views
Why can't decimal numbers be represented exactly in binary?

There have been several questions posted to SO about floating-point representation. For example, the decimal n

avatar
Renaldo
0 1 0 9
asked 07 September, 2021
57 votes
11 answers
913 views
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

avatar
Percy
0 1 0 9
asked 07 September, 2021