Avoid negative sign with compose() when output is zero, e.g. '-0' or '-0.0'
Afficher commentaires plus anciens
When compose outputs zero, it includes a negative sign if the number was negative, giving, e.g. -0 or -0.0. How can I avoid this? (Other than writing another function that does a string compare for every element output by compose and removes the negative sign if there are only zeros.)
num = -0.04;
compose('%.1f', num)
Réponse acceptée
Plus de réponses (2)
num = -0.04;
fprintf('%.1f\n', num)
This is the normal and expected behavior when the value is rounded because it IS negative.
Changing it would be deceiving to the user who would then presume it was positive.
But, if one is adamant, then to post process just a string substitution is all that is required since the precision of the output is given, one doesn't have to do anything exotic.
8 commentaires
As I said, I can write a function to check for this every time I use compose and remove the negative sign, but it's such a common thing that I thought it was worth asking if MATLAB could do it.
No. MATLAB is a mathematical, not an engineering software. You'll have to write the little function.
John D'Errico
le 20 Fév 2025
If a number is negative, and NOT zero, that is an important thing. Glossing over it is a terribly bad idea for most users. Just pretending it does not exist is a problem, not a good thing.
In your case, you don't see it as such, but I'd bet that one day, you will learn differently. (Said as an engineer myself. At least I was in the past.)
Anyway, if it bothers you, rather than post processing it in the form of a string, you could use a tolerance. If the number is close enough to zero, you can just make it zero, and THEN display it using frintf.
Leon
le 20 Fév 2025
Walter Roberson
le 20 Fév 2025
When I see "0" in a paper (my field is engineering), I know it can be a small negative number
Personally, when I see "0", I certainly expect it to be an exact zero. My expectation is perhaps a bit weaker if I see "0.00"
John D'Errico
le 20 Fév 2025
Modifié(e) : John D'Errico
le 20 Fév 2025
No, you completely misunderstand me, but perhaps I could have said it differently.
It is a terribly bad idea for MATLAB to do something like that automatically in any way. Those negative numbers were generated by some process, some computation that apparently did something you do not like. That minus sign should be a flag to you that something happened, that there is something down there, NOT just a zero. I'm sorry though. Ignoring a problem, and pretending it does not exist is something serious.
Far better is to understand where they came from, and fix THAT. Fix the problem, rather than patching the symptom.
However, if you want to remove them anyway, without understanding why they happened, then that is YOUR choice to make. All you need do is essentially round those small values up to zero. And that is trivially done, with a simple test. You can write a function that checks for small negatives, and replaces them with zero, and you can do that without any string parsing. Your choice, and not at all difficult to do.
3 commentaires
Simpler:
x = [0.1 -0.1 0.01 -0.01 3 -3];
x(abs(x) < 0.1) = 0; % Make it zero if it's close enough to 0
compose('%.1f', x)
Note that the numeric rounding and the compiled text may diverge from each other, which leads to incorrect outputs for many values:
compose('%.2f', -0.025) % correct
zerocompose('%.2f', -0.025) % wrong
function c = zerocompose(formatSpec, A)
c = compose(formatSpec, A);
for ii = 1:numel(A)
val = A(ii);
str = c{ii};
if str(1) == '-' && round(val) == 0
c{ii} = str(2:end);
end
end
end
Catégories
En savoir plus sur Environment and Settings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!