Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does unary minus just change sign?

Consider for example the following double-precision numbers:

x = 1232.2454545e-89;
y = -1232.2454545e-89;

Can I be sure that y is always exactly equal to -x (or Matlab's uminus(x))? Or should I expect small numerical differences of the order or eps as it often happens with numerical computations? Try for example sqrt(3)^2-3: the result is not exactly zero. Can that happen with unary minus as well? Is it lossy like square root is?

Another way to put the question would be: is a negative numerical literal always equal to negating its positive counterpart?

My question refers to Matlab, but probably has more to do with the IEEE 754 standard than with Matlab specifically.

I have done some tests in Matlab with a few randomly selected numbers. I have found that, in those cases,

  • They turn out to be equal indeed.
  • typecast(x, 'uint8') and typecast(-x, 'uint8') differ only in the sign bit as defined by IEEE 754 double-precision format.

This suggests that the answer may be affirmative. If applying unary minus only changes the sign bit, and not the significand, no precision is lost.

But of course I have only tested a few cases. I'd like to be sure this happens in all cases.

like image 778
Luis Mendo Avatar asked Dec 02 '15 19:12

Luis Mendo


People also ask

What does unary minus do?

The - (unary minus) operator negates the value of the operand. The operand can have any arithmetic type. The result is not an lvalue. For example, if quality has the value 100 , -quality has the value -100 .

How do you use unary minus?

In some contexts, - is the unary minus operator. In other contexts, - is the subtraction operator. asks for 12 to be subtracted from 95. means add 3 to negative 12 (resulting in -9).

What is unary plus minus?

Unary plus ( + ) or minus ( - ) converts a non-numeric value into a number. The unary minus negates the value after the conversion. The prefix increment operator adds one to a value. The value is changed before the statement is evaluated.

Is the subtraction operator (-) binary or unary?

Formally, in the examples above we have two different operators that share the same symbol: the negation operator, a unary operator that reverses the sign, and the subtraction operator, a binary operator that subtracts one number from another.


1 Answers

This question is computer architecture dependent. However, the sign of floating point numbers on modern architectures (including x64 and ARM cores) is represented by a single sign bit, and they have instructions to flip this bit (e.g. FCHS). That being the case, we can draw two conclusions:

  1. A change of sign can be achieved (and indeed is by modern compilers and architectures) by a single bit flip/instruction. This means that the process is completely invertible, and there is no loss of numerical accuracy.
  2. It would make no sense for MATLAB to do anything other than the fastest, most accurate thing, which is just to flip that bit.

That said, the only way to be sure would be to inspect the assembly code for uminus in your MATLAB installation. I don't know how to do this.

like image 65
user664303 Avatar answered Oct 09 '22 22:10

user664303