问题
I have an assignment where I basically need to count the number of floating point operations in a simple program, which involves a loop, a matrix, and operations such as *, + and ^.
From my understanding, a floating-point operation is an operation that involves floating-point numbers, and we may be interested in counting these operations because I think they may be more expensive for the computer. If you want to add more details to this part, it would be nice.
My problem is that I've no idea of knowing exactly which operations involve floating-point numbers, unless I use functions, such as isfloat
. In that case, would just one of the numbers in the operation be necessary to be floating-point to the operation be considered a floating-point operation, right? If not, why? Can you add more details on this?
For example, suppose I've the following simple function:
function [r, n] = naive(c, x)
% c is the vector of coefficients of the polynomial
% The coeffiecients should be given as follows
% c(1) = coefficient of x^0 (or 1).
% c(length(c)) = coefficient of the largest power of x
% x is the point to evaluate the polynomial at
% r is the result of the evaluation
% (Assumes that the entries are integers)
r = c(1);
n = 0;
for i=2:length(c)
r = r + c(i) * x^(i - 1);
n = n + 2 + (i - 1);
end
end
which basically calculates a normal polynomial evaluated at x
given the coefficients in a vector c
.
As you can see from the code, n
is actually keeping track of floating-point operations. But actually, I'm counting every mathematical operation (except the assignment) as a floating-point operation, but this of course might not be right, or is it? If yes or no, why?
Both the coefficients and c
might be floating-point numbers. So, instead of counting every operation as a floating point operation, should we first check with for example isfloat
if the numbers are floating point, and only then increment n
?
Note, I'm aware of the function flops
, which, from what I understood, it should count the floating-point operations, but it's deprecated, and mostly I would like to learn better these concepts, and therefore try to count them manually.
Thanks for any help!
来源:https://stackoverflow.com/questions/35795333/whats-a-floating-point-operation-and-how-to-count-them-in-matlab