How to handle MATLAB UNDERFLOW Problem?
Mostra commenti meno recenti
My CALCULATIONs involves ranage upto 1E-680, and due to underflow matlab give result as zero.
For the calculation which involves number up to the range 1E-300, Matlab returns the expected answers.
How to handle this?
To put in other words how to USE extended double precision IEEE standard for MATALB?
Risposte (1)
John D'Errico
il 25 Apr 2020
0 voti
Answer, you can't. That is, if you want to use an extended precision IEEE floating point number in MATLAB, you can't. What can you do?
- Use the symbolic toolbox, which can handle longer precision numbers.
- Use my HPF toolbox, which can handle whatever precision you want. (Find HPF on the File Exchange for free download.)
- Use logs.
- You could write the calls in C or some other language where you can do your computations in some other class. But if you can do the work in that other language, why are you asking here?
That is, most of the time when you have these underflows, you really needed to do your computations using logs anyway. That makes any issue with tiny numbers go away, they just turn into negative numbers. Done.
If you absolutely think you really desperately need to use those high precision numbers then use alternatives 1 or 2. You will find your code becomes slow. But that is the cost.
4 Commenti
Chetan Kumar Dabhi
il 25 Apr 2020
Is it possible to represent the HPF answers to the exponent format?
for example HPF output is = 0.00003567, and can it be converted to 3.567E-5?
Chetan Kumar Dabhi
il 25 Apr 2020
It seems "vpa" is also kind of same function....
John D'Errico
il 25 Apr 2020
HPF will automatically use a scientific notation for numbers that will have many leading zeros otherwise. Thus, we see this:
X = hpf('3.567e-50',20)
X =
3.567e-50
HPF decided it was a good idea to use scientific notation for that number.
But there is no direct way to toggle scientific notation to be on all the time. It would not be difficult to write a scientific notation utility though.
function scidisp(X)
% automatically formats a scalar HPF number in scientific form.
if ~isa(X,'hpf')
error('scidisp only applies to HPF input')
end
Xsci = char(mantissa(X) + '0');
% insert a decimal point
Xsci = [Xsci(1),'.',Xsci(2:end)];
% trash trailing zeros in the mantissa.
zind = find(Xsci ~= '0',1,'last') + 1;
zind = max(zind,4);
if ~isempty(zind) && (zind < numel(Xsci))
Xsci(zind:end) = [];
end
% the sign bit?
if X.Sign == -1
Xsci = ['-',Xsci];
end
% append an exponent
Xsci = [Xsci,'e',num2str(X.Exponent-1)];
disp(Xsci)
end
That would probably work, though a bit of a kludge.
>> X = hpf('0.1',20)
X =
0.1
>> scidisp(X)
1.0e-1
>> X = hpf('0.00003567',20)
X =
0.00003567
>> scidisp(X)
3.567e-5
Chetan Kumar Dabhi
il 26 Apr 2020
Thank You John
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!