D
David Mathog
IEEE 754 math rounds -.1, or -0 itself, to -0. Not 0. Fine, that has
some specific uses numerically, as in 1/-inf. However, a displaying
value of -0 is a mistake in most real world applications, were no one
expects "your balance is -0.00 dollars"!
http://www.gnu.org/s/hello/manual/libc/Rounding.html
says "Negative zero behaves IDENTICALLY to zero except when the
copysign or signbit functions are used to check the sign bit
directly." (Emphasis added). Sadly nobody told printf about this:
double dv=0;
dv=-dv;
printf("Prints -0, not 0: %f\n",dv)
I could not find any IEEE bits to set to disable math using -0. Is
there a flag somewhere in C99
for printf that causes -0 to print as 0 (without also causing -1 to
print as 1)? One solution is to add 0.0 to all printf arguments, but
that is really ugly. For instance this:
#include <stdlib.h>
#include <stdio.h>
int main(void){
double dv=0.0;
fprintf(stdout,"%f\n",dv);
fprintf(stdout,"%f\n",-dv);
fprintf(stdout,"%f\n",-dv+0.0);
}
prints
0.000000
-0.000000
0.000000
Thanks,
David Mathog
some specific uses numerically, as in 1/-inf. However, a displaying
value of -0 is a mistake in most real world applications, were no one
expects "your balance is -0.00 dollars"!
http://www.gnu.org/s/hello/manual/libc/Rounding.html
says "Negative zero behaves IDENTICALLY to zero except when the
copysign or signbit functions are used to check the sign bit
directly." (Emphasis added). Sadly nobody told printf about this:
double dv=0;
dv=-dv;
printf("Prints -0, not 0: %f\n",dv)
I could not find any IEEE bits to set to disable math using -0. Is
there a flag somewhere in C99
for printf that causes -0 to print as 0 (without also causing -1 to
print as 1)? One solution is to add 0.0 to all printf arguments, but
that is really ugly. For instance this:
#include <stdlib.h>
#include <stdio.h>
int main(void){
double dv=0.0;
fprintf(stdout,"%f\n",dv);
fprintf(stdout,"%f\n",-dv);
fprintf(stdout,"%f\n",-dv+0.0);
}
prints
0.000000
-0.000000
0.000000
Thanks,
David Mathog