Skip to main content
17 votes
Accepted

Is the exponential function, e^x, very expensive to compute in Matlab and harmful to my computer?

Computing the term $e^x$ is definitely significantly more expensive than computing a lower-order polynomial -- say $x^4$. But it may be ten to 100 times more expensive at most, not "crazy" expensive. ...
Wolfgang Bangerth's user avatar
12 votes
Accepted

Evaluating $\log(\exp(x)+1)$ for negative $x$

Use the (IEEE standard) library function log1p, which should be present in all programming languages. The function log1p(x) ...
Federico Poloni's user avatar
9 votes

Is `#define INT_MIN 0x80000000` okay?

This is more of a question appropriate to the CS forums, but here is the short of it: it requires you to know how the compiler parses numbers. In particular, when the compiler sees something like <...
Wolfgang Bangerth's user avatar
8 votes

Unexpected result when summing sorted (and unsorted) positive floating point numbers

Very interesting problem! I might have a partial answer. To start, I replicated a simple C++ demo that can reproduce the effect ...
Mikael Öhman's user avatar
6 votes
Accepted

Best software to do big number calculations quickly

Why not give GMPY2 a try? From the introduction: gmpy2 is a C-coded Python extension module that supports multiple-precision arithmetic. gmpy2 is the successor to the original gmpy module. The gmpy ...
GertVdE's user avatar
  • 6,199
6 votes

Method to compute $a^n - b^n$

That computation is ill-conditioned anyway when $a$ and $b$ are close. This is not something that you can fix by switching to a different method: any method that uses floating-point computations will ...
Federico Poloni's user avatar
5 votes
Accepted

Is `#define INT_MIN 0x80000000` okay?

No it's not OK. There are no negative literals per-se in C. "-12345" is interpreted as the combination of the positive literal 12345 with the unary minus operator. 0x80000000 is outside the ...
Peter Green's user avatar
4 votes

Evaluating $\log(\exp(x)+1)$ for negative $x$

If you want to use your own implementation to evaluate the function, take a look on this article. Otherwise, just go with log1p as suggested by @Frederico Poloni in his answer. For more information ...
The Doctor's user avatar
3 votes
Accepted

Dynamic tolerance in a conditional loop to obtain maximum precision allowed by machine floating point numbers

You are using the wrong measure for the achievable accuracy. $|x|\mu$ is in some way a lower bound for the error, the smallest increment that would give a different value, so that any smaller ...
Lutz Lehmann's user avatar
  • 6,159
3 votes

How can I detect lost of precision due to rounding in both floating point addition and multiplication?

Floating-point exceptions may help you here. C support varies by implementation (compiler) but see GCC here: https://www.gnu.org/software/libc/manual/html_node/FP-Exceptions.html Python support is ...
Jeff Hammond's user avatar
  • 2,136
2 votes

How can I detect lost of precision due to rounding in both floating point addition and multiplication?

Normally one does not try to detect loss of precision algorithmically, but rather analyzes and modifies algorithms to assess how they are affected by it. For instance, in your first example you would ...
Federico Poloni's user avatar
2 votes

Problems with first and second complement

Two's complement is always done with a specific bit width in mind, and you use modular arithmetic within that width. Basically, you should ignore that leading 1, it's the output carry and should be ...
rchilton1980's user avatar
  • 5,076
2 votes

Summation of trigonometric functions results in error with finite precision

Some thoughts: How many coefficients (N) are there? If you are solely interested in the zeroes, you might get away with 'stretching' the functions by a constant factor in amplitude and/or a constant ...
MPIchael's user avatar
  • 3,065
2 votes

What does this definition of two's complement representation of signed integers mean?

The idea is that if you have $n$ bits $b = [b_{n-1}, \ldots, b_0]$, then you can represent $2^n$ different things with them, as all $b_k \in \{0, 1\}$. You can decide to go for the numbers $0, \ldots,...
Eman Yalpsid's user avatar
1 vote

Method to compute $a^n - b^n$

Here is C++ code comparing the Mathematica computation with three more methods. naive automatic finite differences the method proposed in another answer One important thing to note: if you make $a$ ...
Dan Piponi's user avatar
1 vote

Method to compute $a^n - b^n$

you got pretty close to a good method. Specifically what you want to do is compute a^n*exp(n*log1p(-b/a)) when the values are close together. this will avoid ...
Oscar Smith's user avatar
1 vote

Best software to do big number calculations quickly

OP's comment on OP: yes,numbers are intergers For arbitrarily large integer calculations, MAPLE is an option worth trying. it has very efficient implementations for a number of such calculations.
Mark L. Stone's user avatar
1 vote

Best software to do big number calculations quickly

Using something such as numpy.divide, you most likely will see no considerable difference from doing it in C++. Although casting might be something to consider. C++...
ofey73's user avatar
  • 127

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