On Sunday, 24 March 2024 at 21:21:13 UTC, kdevel wrote:

Usually you do not translate mathematical expressions directly into code:

```
   n += pow(-1.0, i - 1.0) / (i * 2.0 - 1.0);
```

The term containing the `pow` invocation computes the alternating sequence -1, 1, -1, ..., which can be replaced by e.g.

```
   immutable int [2] sign = [-1, 1];
   n += sign [i & 1] / (i * 2.0 - 1.0);
```

This saves the expensive call to the pow function.

I know that the code can be simplified/optimized, I just wanted to compare the same expression in C and D.

Reply via email to