Negative zeros in Matlab
Asked Answered
R

2

7

Basically I wanted to ask two things:

  1. Why does this happen? (negative zero in Matlab)
  2. When does this happen?

I came up with this. Octave has some similarities with Matlab, so the usefulness of this feature is clear, but one of the things they said, is that it does not appear in the default output. and I just tackled it right now. So, maybe a new insight on this?

For the second question, in the answered question I referred to, they just said it could happen in some calculations, and in the following calculation which I just did, it doesn't seem really necessary to use (or get) that negative zero.

The code where I encountered this is:

xcorr([1 0 1 1], [0 1 1 0 0])

where it's output is:

-0.0000   -0.0000    1.0000    1.0000    1.0000    2.0000    1.0000    0.0000    0.0000

The xcorr is actually a cross corelation function, which does only some simple operations like summing and multiplications, where it's exact function details can be found here. Anyway, nothing like "complex branch cuts and transformations of the complex plane"

Thanks

Ruck answered 30/4, 2016 at 15:11 Comment(0)
S
4

These values do not represent zeros. Instead, they are negative values which are very close to zero. The reason for getting these values and not simply zeros is due to approximations which are performed in the function implementation. According to Matlab documentation: "xcorr estimates the cross-correlation sequence of a random process".

In other words - the values which are displayed on the screen are just approximations for negative values.

In order to test this, you can change the display format of Matlab.

code:

format shortE;
xcorr([1 0 1 1], [0 1 1 0 0])

Result:

ans =

  Columns 1 through 5

 -6.2450e-017 -5.5511e-017  1.0000e+000  1.0000e+000  1.0000e+000

  Columns 6 through 9

  2.0000e+000  1.0000e+000  1.1102e-016  1.1796e-016

As you can see, the values in coordinates 1,2,8 and 9 are actually negative.

Shoreline answered 30/4, 2016 at 15:22 Comment(3)
Thanks for your answer. It's as you said, not zero, but a very small number. But still, why does this happen? As the Matlab documentation (and of course, the formula of this function), is just a sum of multiplications.Ruck
According to the formula you are correct. However, MATLAB's implementation for xcorr is only an approximation for this formula. As written in the official Matlab's documentation: "xcorr estimates the cross correlation sequence ". Therefore, the final output is not 100% accurate.Shoreline
I don't get any negative numbers from xcorr([1 0 1 1], [0 1 1 0 0]) on Matlab R2020a, win64Disembowel
R
2

In the specific sample case, the -0.0000 turned out to actually be tiny non-zero negative numbers. However, in an effort to make printout human readable, Matlab and Octave tries to avoid using scientific notation in printing. As a result, when mixed with big numbers, small number are reduced to 0.0000 or -0.000. This can be changed by setting the default preferences in Matlab or Octave.

But this is NOT the only answer to the asked questions:

  1. Why does this happen? (negative zero in Matlab),
  2. When does this happen?

In fact, in Matlab and Octave, and any computing environment that works with floating points, -0. really a thing. This is not a bug in the computing environment, but rather an allowance in the IEEE-754 standard for binary representation of a floating point number, and it is a product of the floating point processor in the CPU (not the programming language).

Whereas binary representation of integer does not have a special sign bit, IEEE-754 reserves a bit for the sign, separate from the number. So while the number part might mean 0, the sign bit is left to mean negative.

It happens wherever your CPU (Intel or AMD) does a product with 0. and any negative number (including -0.). I don't know if it is required by IEEE-754 for that to happen or if it simply the result of CPU design optimization (maximize speed, minimize size).

In either case, this -0. is a non-issue in that IEEE-754 requires it to be comparatively and arithmetically exactly the same as 0.. That is:

-0. < 0      --> FALSE
-0. == 0     --> TRUE
1+ -0. == 1  --> TRUE
etc...
Redress answered 29/9, 2020 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.