# Tag Info

## Hot answers tagged numerical-methods

Accepted

### PI Calculator, Interview Challenge

In an interview it usually doesn't matter if you actually solve the problem. What is most important is the way you (try to) solve it. If they don't tell you it should be the possibly fastest solution ...
• 44.3k

### PI Calculator, Interview Challenge

I'm going to give an alternative approach to tchbot's answer There are places where SOLID prinicpals are required. I don't see this as one of them. Here, we have a simple "Are you capable of ...
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 ...
• 3,950

### PI Calculator, Interview Challenge

Flavio's answer addresses a part of something that matters greatly for this kind of problem: rounding errors will kill you. If you look at the expression: $$1-\frac13+\frac15-\frac17+...$$ This is ...
• 397

### 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 ...
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 ...
• 2,622
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. ...
• 49.6k

### 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 ...
• 1,566
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, ...
• 11.4k
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 ...
• 62.5k
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$, ...
• 23.3k

### PI Calculator, Interview Challenge

There is clearly no 'best way' to do this. From a 'Numerical Analysis' approach, you should never sum that way a series with alternating-sign terms: loss of accuracy and slow convergence speed can be ...

### Implementing numerical integration

I see a few things that may help you improve your program. Avoid pow for float The use of ...
• 66.5k
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 ...
• 19.4k
Accepted

### Calculate Pi using Monte Carlo

One could consider at least the following points: Instead of including <stdlib.h>, I'd include <cstdlib>. In ...
• 3,589

### 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, ...
• 58.3k

### 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 ...
• 3,346

### pure Python Bézier curve implementation

Unnecessary Generator You've got an unnecessary generator expression here: ...
• 33.8k

### 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 ...
• 333

### PI Calculator, Interview Challenge

Observations and Suggestions Operate on pi directly. You can eliminate the pi = 4 * number;. You are using more operations than are necessary. I prefer ...
• 6,016

### 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 ...
• 19.3k
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, ...
• 77.1k

### 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. ...
• 43.4k

### 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 ...
• 77.1k
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 ...
• 58.3k
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 ...
• 58.3k

### Monte Carlo estimation of π

In the code below you must realise that score is a variable that is shared between the threads. Which means that it requires synchronization, as you have omitted ...
• 16.6k

### Implementing numerical integration

Improve robustness and readability Don't using namespace with namespaces not designed for it. Use const and ...
• 77.1k