A Program that Works for Either Single or Double Precision
Now let's look at a function to compute enough terms in the Fibonacci sequence so the ratio is less
than the correct machine epsilon (eps) for datatype single or double.
% How many terms needed to get single precision results?
fibodemo('single')
ans = 19
% How many terms needed to get double precision results?
fibodemo('double')
ans = 41
% Now let's look at the working code.
type fibodemo
function nterms = fibodemo(dtype)
%FIBODEMO Used by SINGLEMATH demo.
% Calculate number of terms in Fibonacci sequence.
% Copyright 1984-2014 The MathWorks, Inc.
fcurrent = ones(dtype);
fnext = fcurrent;
Single Precision Math
4-29
Précédent

- 173/1450

Suivant