473,385 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

negative zeroes in program output?

Under what circumstances would a C program, containing nothing
that is not standard C, prefix a zero result of a calculation
with doubles with a minus sign? I have written a program that
does operations on matrices. Some, but not all, of the zeroes in
the result matrices are displayed by printf as -0.0. (I know that
floating point arithmetic algorithms use positive and negative
zero but this is the first time I've seen negative zeroes in
program output.)

Nov 14 '05 #1
9 7283
On Wed, 21 Jul 2004 19:41:31 GMT, in comp.lang.c , Elliot Marks
<em****@email.net> wrote:
Under what circumstances would a C program, containing nothing
that is not standard C, prefix a zero result of a calculation
with doubles with a minus sign? I have written a program that
does operations on matrices. Some, but not all, of the zeroes in
the result matrices are displayed by printf as -0.0.


Floating point is not exact. What mathematically might be zero is quite
likely to be 0 +/- some small amount. Your printf format string will then
round it and display as -0.0

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #2
Mark McIntyre wrote:
On Wed, 21 Jul 2004 19:41:31 GMT, in comp.lang.c , Elliot Marks
<em****@email.net> wrote:

Under what circumstances would a C program, containing nothing
that is not standard C, prefix a zero result of a calculation
with doubles with a minus sign? I have written a program that
does operations on matrices. Some, but not all, of the zeroes in
the result matrices are displayed by printf as -0.0.

Floating point is not exact. What mathematically might be zero is quite
likely to be 0 +/- some small amount. Your printf format string will then
round it and display as -0.0

I cancelled the original post because this behaviour went away
after using a different compiler. Your answer makes good sense
though.

Thanks,
Elliot

Nov 14 '05 #3
Mark McIntyre wrote:
Elliot Marks wrote:
Under what circumstances would a C program, containing nothing
that is not standard C, prefix a zero result of a calculation
with doubles with a minus sign? I have written a program that
does operations on matrices. Some, but not all, of the zeroes in
the result matrices are displayed by printf as -0.0.
Floating point is not exact. What mathematically might be zero
is quite likely to be 0 +/- some small amount.
Your printf format string will then round it and display as -0.0


No.
cat main.c #include <stdio.h>

int main(int argc, char* argv[]) {
double x = -0.0;
double y = +0.0;
fprintf(stdout, "%lf = x\n", x);
fprintf(stdout, "%lf = y\n", y);
if (x == y)
fprintf(stdout, "They are equal!\n");
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main

-0.000000 = x
0.000000 = y
They are equal!

A floating-point number has two representations for zero --
one positive and the other negative.
Nov 14 '05 #4
E. Robert Tisdale wrote:

Mark McIntyre wrote:
Elliot Marks wrote:
Under what circumstances would a C program, containing nothing
that is not standard C, prefix a zero result of a calculation
with doubles with a minus sign? I have written a program that
does operations on matrices. Some, but not all, of the zeroes in
the result matrices are displayed by printf as -0.0.
Floating point is not exact. What mathematically might be zero
is quite likely to be 0 +/- some small amount.
Your printf format string will then round it and display as -0.0


No.


That's what I was thinking.
double x = -0.0;
double y = +0.0;
fprintf(stdout, "%lf = x\n", x);
fprintf(stdout, "%lf = y\n", y);
if (x == y)
fprintf(stdout, "They are equal!\n"); -0.000000 = x
0.000000 = y
They are equal!

A floating-point number has two representations for zero --
one positive and the other negative.


It's allowed to have two, but floating point representation
is implementation defined.

The '-' character output of your program
is also implementation defined, even on implementations that
have two representations for zero --
one positive and the other negative.

--
pete
Nov 14 '05 #5
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
Mark McIntyre wrote:
Elliot Marks wrote:
Under what circumstances would a C program, containing nothing that
is not standard C, prefix a zero result of a calculation with
doubles with a minus sign? I have written a program that does
operations on matrices. Some, but not all, of the zeroes in the
result matrices are displayed by printf as -0.0. Floating point is not exact. What mathematically might be zero
is quite likely to be 0 +/- some small amount. Your printf format
string will then round it and display as -0.0


No.


Yes.
> cat main.c

#include <stdio.h>

int main(int argc, char* argv[]) {
double x = -0.0;
double y = +0.0;
fprintf(stdout, "%lf = x\n", x);
fprintf(stdout, "%lf = y\n", y);
if (x == y)
fprintf(stdout, "They are equal!\n");
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main

-0.000000 = x
0.000000 = y
They are equal!


Why do you use fprintf(stdout, ...) rather than printf(...)?

The correct format for type double is "%f". The "%lf" format is not
defined by the standard, so your first two fprintf calls invoke
undefined behavior.
A floating-point number has two representations for zero --
one positive and the other negative.


That's true for many (most?) existing floating-point representations,
but I don't think the C standard says anything about it. An
implementation for which the above program prints

0.000000 = x
0.000000 = y
They are equal!

would be conforming as far as I know.

But the following program:

#include <stdio.h>
int main(void)
{
double x = -1e-20;
double y = +1e-20;
printf("x = %lf\n", x);
printf("y = %lf\n", y);
printf(x == y ? "They're equal\n" : "They're not equal\n");
return 0;
}

produces the following output (in at least one implementation):

x = -0.000000
y = 0.000000
They're not equal

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
Keith Thompson wrote:
The correct format for type double is "%f".
Yes.
The "%lf" format is not defined by the standard,
so your first two fprintf calls invoke
undefined behavior.


Undefined in C89
Defined in C99
C89 last public draft:
4.9.6 Formatted input/output functions
4.9.6.1 The fprintf function

an optional l (ell) specifying that a following d , i , o ,
u , x , or X conversion specifier applies to a long int or unsigned
long int argument; an optional l specifying that a following n
conversion specifier applies to a pointer to a long int argument; or
an optional L specifying that a following e , E , f , g , or G
conversion specifier applies to a long double argument.
If an h , l ,
or L appears with any other conversion specifier, the behavior is
undefined.

N869
7.19.6.1 The fprintf function

l (ell) ... or has no effect on a following a,
A, e, E, f, F, g, or G conversion
specifier.

--
pete
Nov 14 '05 #7
Keith Thompson wrote:

[ snip ]

That's true for many (most?) existing floating-point representations,
but I don't think the C standard says anything about it. An
implementation for which the above program prints

0.000000 = x
0.000000 = y
They are equal!

would be conforming as far as I know.

But the following program:

#include <stdio.h>
int main(void)
{
double x = -1e-20;
double y = +1e-20;
printf("x = %lf\n", x);
printf("y = %lf\n", y);
printf(x == y ? "They're equal\n" : "They're not equal\n");
return 0;
}

produces the following output (in at least one implementation):

x = -0.000000
y = 0.000000
They're not equal


That program, on my implementation (gcc 3.1) looks like..

C:\work\c\clc>kt
x = 0.000000
y = 0.000000
They're not equal

Negative zero is weird.

Also on my implementation, "%f" suffices for both double and long
double. "%lf" is allowed by C99 and accepted quietly by gcc but is
not defined.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #8
pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:
The correct format for type double is "%f".
Yes.
The "%lf" format is not defined by the standard,
so your first two fprintf calls invoke
undefined behavior.


Undefined in C89
Defined in C99

[snip] N869
7.19.6.1 The fprintf function

l (ell) ... or has no effect on a following a,
A, e, E, f, F, g, or G conversion
specifier.


You're right, I missed that. (It's also in the final C99 standard.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #9
In <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> writes:
pete <pf*****@mindspring.com> writes:
Keith Thompson wrote:
> The correct format for type double is "%f".


Yes.
> The "%lf" format is not defined by the standard,
> so your first two fprintf calls invoke
> undefined behavior.


Undefined in C89
Defined in C99

[snip]
N869
7.19.6.1 The fprintf function

l (ell) ... or has no effect on a following a,
A, e, E, f, F, g, or G conversion
specifier.


You're right, I missed that. (It's also in the final C99 standard.)


Official reason: to improve symmetry between scanf and printf.
Unofficial reason: far too much broken code used %lf in printf and
practically all implementors ignored the l in this context:

fangorn:~/tmp 573> gcc -Wall test.c
fangorn:~/tmp 574> gcc -pedantic -Wall test.c
test.c: In function `main':
test.c:5: warning: ISO C90 does not support the `%lf' printf format

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Danny Winslow | last post by:
I get an unexpected result when I add two negative numbers in PHP on SPARC/Solaris 8. The same program works fine on Intel/Linux. I'm using PHP 4.3.1 on both systems. Here is my program,...
7
by: pj | last post by:
Why does M$ Query Analyzer display all numbers as positive, no matter whether they are truly positive or negative ? I am having to cast each column to varchar to find out if there are any...
0
by: Koji Ishii | last post by:
According to the doc, Stream.Read() returns: - # of bytes read, or - 0 for EOF But could that be a negative value? If so, how should we program against such case? I have a loop like this:...
1
by: Joe Harris | last post by:
I've searched everywhere but can't find a KB article or forum message about this one... Excel Pivot Tables differentiate between +0 (zero) and -0 (negative zero)and now that I've moved over to...
29
by: Peter Ammon | last post by:
What's the most negative double value I can get? Is it always guaranteed to be -DBL_MAX? If so, why (or rather, where)? Thanks.
11
by: drtimhill | last post by:
I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled. For example, to get the last character in a string, I can enter "x". To get the...
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
2
by: majie30 | last post by:
Write this program using functions. Write a program that accepts any number from the keyboard and tells you whether it is a nonnegative integer. The number should be sent to the function int_test(...
1
by: aLiase | last post by:
so heres my code input = open('DATA1.txt', 'r') output = open('OUT1.txt', 'w') numbers = input.readlines() for x in range(1): set = numbers a = float(set) b = float(set)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.