language agnostic - Does float have a negative zero? (-0f) -


ieee floating point numbers have bit assigned indicate sign, means can technically have different binary representations of 0 (+0 , -0). there arithmetic operation example in c result in negative 0 floating point value?

this question inspired called question whether can safely compare 0.0f using ==, , wondered further if there other ways represent 0 cause float1 == 0.0f break seemingly equal values.

[edit] please, not comment safety of comparing floats equality! not trying add overflowing bucket of duplicate questions.

according standard, negative 0 exists equal positive zero. purposes, 2 behave same way , many consider existence of negative implementation detail. there are, however, functions behave quite differently, namely division , atan2:

#include <math.h> #include <stdio.h>  int main() {     double x = 0.0;     double y = -0.0;     printf("%.08f == %.08f: %d\n", x, y, x == y);     printf("%.08f == %.08f: %d\n", 1 / x, 1 / y, 1 / x == 1 / y);     printf("%.08f == %.08f: %d\n", atan2(x, y), atan2(y, y), atan2(x, y) == atan2(y, y)); } 

the result code is:

0.00000000 == -0.00000000: 1 1.#inf0000 == -1.#inf0000: 0 3.14159265 == -3.14159265: 0 

this mean code correctly handle limits without need explicit handling. it's not relying on feature values close limits idea, since simple calculation error can change sign , make value far correct, can still take advantage of if avoid calculations change sign.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -