Skip to main content
26 votes
Accepted

Approximating constant π² to within error

You don't need to compare prev and new during each iteration. The difference between the new and the previous sum is simply the ...
Eric Duminil's user avatar
  • 3,991
19 votes

Approximating constant π² to within error

So, you have a polynomial sequence and want to sum its terms while they are greater than a provided tolerance (i.e.: the tolerance is lower than the current computed term). This is easily expressed ...
301_Moved_Permanently's user avatar
19 votes
Accepted

IEEE 754 square root with Newton-Raphson

This answer uses pointer-casting for type-punning just to save space. In practice keep using your union (safe in ISO C99, and in C++ as a GNU and MSVC extension) or memcpy (safe in C and C++). This ...
Rainer P.'s user avatar
  • 3,110
17 votes
Accepted

Definite Integral Approximation using the Trapezoidal Method

If you are writing mathematical code in Python, then it is worth looking at NumPy, which is a library implementing (among other things) fast arithmetic operations on arrays of floating-point numbers. ...
Gareth Rees's user avatar
  • 50.1k
15 votes

Approximating constant π² to within error

Please fix the indentation (probably just a copy-pasting issue). In Python, you don't need parenthesis around the expressions for block statements like if and ...
Solomon Ucko's user avatar
  • 1,576
15 votes
Accepted

Numerical integration in C++: Newton-Cotes formulas

I wonder whether the #include "NewtonCotesFormulas.cpp" at the end of the NewtonCotesFormulas.h header file could be somehow avoided. I used it because without I'd get linking errors. Yea, ...
JDługosz's user avatar
  • 11.7k
14 votes
Accepted

Mean π: Archimedes vs. Gauss - π computation through generalized means

Convergence testing if pi == piold: break This is not usually done, because float equality has a lot of gotchas. In this case it's possible due to ...
Reinderien's user avatar
  • 71.1k
13 votes
Accepted

Python π = 1 + (1/2) + (1/3) + (1/4) - (1/5) + (1/6) + (1/7) + (1/8) + (1/9) - (1/10) ...1748 Euler

What you are approximating is $$ \pi =\sum_{m=1}^{\infty}\frac{(-1)^{s(m)}}{m},$$ where \$s(m)\$ counts the number of appearances of primes of the form \$4k+1\$ in the prime decomposition of \$m\$, ...
Martin R's user avatar
  • 24.2k
12 votes
Accepted

Simulating a two-body collision problem to find digits of Pi

First, this is an awesome video! Upvoted for that reason alone. :) If n=2, the program finishes within milliseconds, whereas for n=3, it takes a whopping 115 seconds. Do you know about big-O ...
Quuxplusone's user avatar
  • 19.7k
12 votes

Numerical integration in C++: Newton-Cotes formulas

Use an enum to give names to choices Consider using an enum, or even better an enum class, ...
G. Sliepen's user avatar
  • 69.3k
11 votes

AVX assembly for fast atan2 approximation

So, just for fun, I've converted your code to intrinsics (and probably made a bunch of mistakes in the process): ...
FUZxxl's user avatar
  • 741
11 votes

Implementing numerical integration

I see a few things that may help you improve your program. Avoid pow for float The use of ...
Edward's user avatar
  • 67.2k
11 votes
Accepted

Calculate Pi using Monte Carlo

One could consider at least the following points: Instead of including <stdlib.h>, I'd include <cstdlib>. In ...
Juho's user avatar
  • 3,649
11 votes

IEEE 754 square root with Newton-Raphson

FYI, in IEEE754 sqrt is a "basic" operation that's required to be correctly-rounded (rounding error <= 0.5ulp), same as + - * /. Hardware FPUs (I think) always ...
Peter Cordes's user avatar
  • 3,761
9 votes

AVX assembly for fast atan2 approximation

Functional problems. Sign test Rather than x < 0, use signbit(x). Although I suspect this is faster, it is to allow code to ...
chux's user avatar
  • 36.4k
9 votes

pure Python Bézier curve implementation

Unnecessary Generator You've got an unnecessary generator expression here: ...
AJNeufeld's user avatar
  • 35.3k
9 votes

A probability distribution function, to be called repeatedly during numerical integration

This is a possible solution to harolds open point about exp in his answer. Note that exp(x+y) = exp(x) * exp(y) holds for any ...
BlameTheBits's user avatar
8 votes
Accepted

Finding the root of a function by Bisection Method

Headers and namespaces The <cmath> header is referenced, but never used; it can be removed. If we're to use std::vector, ...
Toby Speight's user avatar
  • 88.3k
8 votes

Finding the root of a function by Bisection Method

A std::vector<T> vec is an arbitrary collection of Ts. vec might contain none, one or ...
Zeta's user avatar
  • 19.7k
8 votes

Definite Integral Approximation using the Trapezoidal Method

You can use sum with a comprehension to create part_2. You can move all the maths together if you think it looks nicer. ...
Peilonrayz's user avatar
  • 44.6k
8 votes

IEEE 754 square root with Newton-Raphson

I don't see why you define this constant yourself: #define MANTISSA_SIZE 52 Given we already assume that FLT_RADIX is 2, we can ...
Toby Speight's user avatar
  • 88.3k
8 votes
Accepted

C++ class to create and evaluate Chebyshev approximations of arbitrary functions

Naming things You have a bounds checking evaluation function, operator(), and one that doesn't do bounds checking named ...
G. Sliepen's user avatar
  • 69.3k
8 votes
Accepted

Genetic algorithm to guess coefficient of a polynomial

This is not the best algorithm If the goal is to get the best coefficients for a polynomial so it fits the given points, then a polynomial regression algorithm such as ...
G. Sliepen's user avatar
  • 69.3k
7 votes

Calculating Maclaurin series for sin(x)

I am not a Haskell expert. Few words from a numerical analyst point of view. Your concern about recalculating factorial is very well founded. I recommend to take one more step and realize that ...
vnp's user avatar
  • 58.7k
7 votes

Implementing numerical integration

Improve robustness and readability Don't using namespace with namespaces not designed for it. Use const and ...
Toby Speight's user avatar
  • 88.3k
7 votes
Accepted

A simple definite integrator class of a single variable in C++

You made multiple non-trivial edits while I wrote my answer, so there might be some divergence. (Personal annotation: Code should be (mostly) self explanatory. Don’t add a wall of text beforehand that ...
Emma X's user avatar
  • 443
7 votes

Integrator 2.0: A Simple Integrator in C++17

#pragma once is non-standard. size_t is never defined. If it's a misspelling of std::size_t...
Toby Speight's user avatar
  • 88.3k
7 votes

A probability distribution function, to be called repeatedly during numerical integration

A typical way to reduce such cosines with an angle that is steadily counting up by the same increment, is to take a vector and rotate it step by step. That way, one cosine and one sine are calculated, ...
user555045's user avatar
  • 12.4k
7 votes
Accepted

Implementing a joint differential equation and eigenvalue solver

My primary goal here is to verify the correctness of my method and its implementation in code You've done some things well - you have seemingly descriptive variable names and comments. You need to ...
Reinderien's user avatar
  • 71.1k
6 votes
Accepted

Multithreaded Monte-Carlo Integration

Since C++17 (and its now 2018) You don;t need to do this ugly thing namespace boost { namespace math { namespace quadrature { }}} You can simply do this: ...
Loki Astari's user avatar
  • 97.7k

Only top scored, non community-wiki answers of a minimum length are eligible