Why does fprintf attach a negative sign to a zero?

84 ビュー (過去 30 日間)
Matt J
Matt J 2025 年 12 月 4 日 22:08
Here is some code to set up the demonstration,
load data
fid = fopen('runsim.py','w');
rotation=[0,0,-GantryAngles(1)];
This next line verifies that rotation(3) is exactly equal to zero,
isZero = (rotation(3)==0)
isZero = logical
1
Nevertheless, when I do a formatted file print, the 3rd zero ends up printed to the file with a negative sign attached:
fprintf(fid, "vN_%d.set_rotation(%g, %g, %g, 'deg')\n",1, rotation);
type runsim.py
vN_1.set_rotation(0, 0, -0, 'deg')
Why does this happen?

採用された回答

dpb
dpb 2025 年 12 月 4 日 22:49
移動済み: dpb 2025 年 12 月 4 日 22:49
load data
%fid = fopen('runsim.py','w');
rotation=[0,0,-GantryAngles(1)];
fprintf("vN_%d.set_rotation(%g, %g, %g, 'deg')\n",1, rotation)
vN_1.set_rotation(0, 0, -0, 'deg')
rotation=[0,0,GantryAngles(1)];
fprintf("vN_%d.set_rotation(%g, %g, %g, 'deg')\n",1, rotation)
vN_1.set_rotation(0, 0, 0, 'deg')
MATLAB keeps signed zero internally and the various C i/o formatting honors it whereas the default command line format short is a prettified output that removes the minus.
There is a "+" flag that can be added to a format specifier to force the sign, but there isn't one to remove the minus; you would have to preprocess as does the internal command line output formatting to remove it.
  6 件のコメント
Stephen23
Stephen23 約9時間 前
"if I want to ensure that I'm writing .txt files with agnostic 0, I have to expend overhead in M-code doing things like..."
It is simpler to add zero:
writematrix(0+A)
Walter Roberson
Walter Roberson 約7時間 前
I agree with @Stephen23 that adding 0 is easier.
format long g
A = -0
A =
0
fprintf('%g %g\n', A, A+0)
-0 0

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2025 年 12 月 4 日 22:43
fprintf('%g %g\n', 0, -0)
0 -0
Negative zero is represented differently than non-negative zero, This is because it is mathematically different:
fprintf('%g %g\n', 1/0, 1/-0)
Inf -Inf
  4 件のコメント
Steven Lord
Steven Lord 約21時間 前
Also see the Wikipedia page for "signed zero".
Note that there is at least one behavior described in the Arithmetic section on that page where MATLAB differs from IEEE. As stated in the IEEE Compliance section on the sqrt page:
format hex
negativeZero = -0 % note the sign bit
negativeZero =
8000000000000000
positiveZero = 0
positiveZero =
0000000000000000
x = sqrt(negativeZero) % matches positiveZero not negativeZero
x =
0000000000000000
FYI the hypot, atan2, and power documentation pages also have IEEE Compliance sections. Hypot's involves combinations of NaN and Inf, atan2's involves combinations of +0 and -0, and power's involves some NaN cases.
Paul
Paul 約20時間 前
Checking out sqrt I discovered realsqrt, which I'd never known about.
The error message for a real, negative input
try
realsqrt(-1)
catch ME
ME.message
end
ans = 'Realsqrt produced complex result.'
suggests realsqrt actually did a computation but caught the error on the result.
OTOH, with a complex input
try
realsqrt(-1+1i)
catch ME
ME.message
end
ans = 'Invalid argument at position 1. Value must be real.'
it seems like realsqrt does error checking on the input.

サインインしてコメントする。


John
John 約21時間 前
I'll add that negative zero is part of the IEEE 754 spec for floating point numbers. This is not MATLAB-specific.

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

タグ

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by