How to handle MATLAB UNDERFLOW Problem?
显示 更早的评论
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?
回答(1 个)
John D'Errico
2020-4-25
0 个投票
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 个评论
Chetan Kumar Dabhi
2020-4-25
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
2020-4-25
It seems "vpa" is also kind of same function....
John D'Errico
2020-4-25
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
2020-4-26
Thank You John
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!