parth
parth

Reputation: 272

getting rid of signed zero

I'm doing some matrix operations where the matrix elements obtains values from certain variables in the following way:

elem[1] = -x

but when x = 0 it sets -0 in the matrix which is undesirable. Any definitive way to prevent this?

Upvotes: 2

Views: 596

Answers (1)

Murilo Vasconcelos
Murilo Vasconcelos

Reputation: 4827

You can do the following:

elem[i] = -x + 0.0;

I've tested the following code:

float a = -0.0; 
printf("%f %f\n", a, a + 0.0);

Which produces the following output:

-0.000000 0.000000

Upvotes: 5

Related Questions