Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get signed zeros in C++/C?

67 views
Skip to first unread message

Peng Yu

unread,
Jun 10, 2015, 10:51:01 AM6/10/15
to
I don't find how to express signed zeros in C++/C. Does anybody know what is the proper way to do so? Are they just -0.0 and +0.0? How to distinguish signed zeros from true zeros.

http://en.wikipedia.org/wiki/Signed_zero

Regard,
Peng

Alain Ketterlin

unread,
Jun 10, 2015, 11:18:17 AM6/10/15
to
AFAIK there is no literal for those. You need to use either copysign(),
or the signbit() macro (from <cmath>).

-- Alain.

Victor Bazarov

unread,
Jun 10, 2015, 11:19:32 AM6/10/15
to
On 6/10/2015 10:50 AM, Peng Yu wrote:
> I don't find how to express signed zeros in C++/C. Does anybody know
what is the proper way to do so? Are they just -0.0 and +0.0? How to
distinguish signed zeros from true zeros.

What's "true zeros"?

The IEEE 754 floating point standard has a negative zero representation
in which if the sign bit is set and the rest are clear. A "regular"
zero (all bits clear) is considered positive. (I just looked at the
Signed zero article, and it has that description).

I am almost sure C++ has no way to express negative zero since the
hosted system may not be able to represent it. The Standard does not
require any particular representation of floating point values.

If your system has IEEE 754, your compiler can have its own way of
expressing it either in a literal or by invoking some function (possibly
intrinsic) but that is *implementation-specific* and not general.

> http://en.wikipedia.org/wiki/Signed_zero

V
--
I do not respond to top-posted replies, please don't ask

Öö Tiib

unread,
Jun 10, 2015, 12:14:14 PM6/10/15
to
On Wednesday, 10 June 2015 17:51:01 UTC+3, Peng Yu wrote:
> I don't find how to express signed zeros in C++/C.
> Does anybody know what is the proper way to do so? Are they
> just -0.0 and +0.0?

In C++ '-0.0' and '+0.0' are not 'double' literals by
its parsing rules.

These are expressions where unary minus or plus
operators are applied to 'double' literal '0.0'.

What such floating point arithmetic does (like those
unary operators) is not regulated by C or C++
standard. It is left implementation-specific.
Target platform may for example (and often does)
follow IEC 559/IEEE 754 standard of floating point
arithmetic. You can check if it does with
'std::numeric_limits<double>::is_iec559'. It works
compile-time so you can 'static_assert' if your code
assumes that it does.

#include <iostream>
#include <limits>

int main()
{
std::cout << (std::numeric_limits<double>::is_iec559
? "double follows IEEE 754 on your platform"
: "RTFM about double on your platform") << '\n';

double someZero = -0.0;
std::cout << "Some zero:" << someZero << '\n'
<< "Some zero negated:" << -someZero << std::endl;
return 0;
}


> How to distinguish signed zeros from true zeros.
>
> http://en.wikipedia.org/wiki/Signed_zero

The link that you posted answers that question.
Can't you read it? Either use 'copysign' from <cmath>
or see if '1/someZero' produces negative or positive
infinity.

Mr Flibble

unread,
Jun 10, 2015, 1:09:14 PM6/10/15
to
In maths 1/0 is not infinity, 1/0 is undefined. In maths negative zeros
don't exist. IEEE floating point is wrong.

/Flibble

Öö Tiib

unread,
Jun 10, 2015, 2:34:59 PM6/10/15
to
That is like saying that 'int' is wrong since it is not
behaving like "integer" of maths. It does not even
pretend to be correct in that sense.

IEEE 754 does define nothing about maths. It defines
formats, rules and operations for implementing floating
point computation and exchange.

Mr Flibble

unread,
Jun 10, 2015, 3:34:12 PM6/10/15
to
Using 'int' is wrong; use the typedefs from <cstdint> instead.

>
> IEEE 754 does define nothing about maths. It defines
> formats, rules and operations for implementing floating
> point computation and exchange.

Doesn't make it right. If IEEE 754 was designed by mathematicians rather
than engineers it may well have been a lot better.

/Flibble

gwowen

unread,
Jun 11, 2015, 3:32:16 AM6/11/15
to
On Wednesday, June 10, 2015 at 6:09:14 PM UTC+1, Mr Flibble wrote:

> In maths 1/0 is not infinity, 1/0 is undefined. In maths negative zeros
> don't exist. IEEE floating point is wrong.
>
> /Flibble

Sure, in \mathbb{R} it is undefined. But real mathematicians don't limit themselves to a single model of numbers, if other models are better for their purposes.

On the real projective line \mathbb{R}^*, you can absolutely have 1/0 = infinity. The numbers stop being a field of course, but IEEE floats aren't a field either.

Since IEEE floats can't reasonably model \mathbb{R} while the field axioms, a mathematician might very reasonably decide they might as well model the projective line instead.

Öö Tiib

unread,
Jun 11, 2015, 3:58:14 AM6/11/15
to
On Thursday, 11 June 2015 10:32:16 UTC+3, gwowen wrote:
>
> Sure, in \mathbb{R} it is undefined.

Are there Usenet clients that support LaTeX?
Some support Unicode ... u8"ℝ" <- tried to achieve u8"\u211D" .

gwowen

unread,
Jun 11, 2015, 6:31:34 AM6/11/15
to
Not that I know of. I just used the LaTeX markup on the basis that anyone who cared about the projective real line and field axioms would just parse it mentally. FWIW: on the hated google group interace, that UTF-8 shows up correctly.

David Brown

unread,
Jun 11, 2015, 7:51:34 AM6/11/15
to
It's also fine here in Thunderbird on Linux.

Mr Flibble

unread,
Jun 11, 2015, 2:18:29 PM6/11/15
to
Nonsense. In maths 1/0 is undefined not infinity. IEEE floating point
is wrong about this and about negative zero.

/Flibble


gwowen

unread,
Jun 11, 2015, 2:27:33 PM6/11/15
to
Is there no subject on which your confidence does not exceed your knowledge?

Go read up on how the mathematical object called the Real Projective Line is defined. You may learn something. As Earl Weaver said, the important stuff is what you learn after you know everything.

Mr Flibble

unread,
Jun 11, 2015, 2:42:10 PM6/11/15
to
The real projective line looks like nonsense to me mate. 1/0 is undefined.

/Flibble


Paavo Helde

unread,
Jun 11, 2015, 4:43:40 PM6/11/15
to
Mr Flibble <flibbleREM...@i42.co.uk> wrote in
news:qfmdnZa-WeB3TeTI...@giganews.com:

> Nonsense. In maths 1/0 is undefined not infinity. IEEE floating point
> is wrong about this and about negative zero.

As this is a C++ group, math is no concern and neither is whether IEEE is
conceptually wrong in anything or not. If the C++ implementation is using
IEEE, that's what we have got and that's what we have to work with:

>echo '
#include <iostream>
int main() {
double x = 0;
std::cout << 1/x << "\n";
}
' > test1.cpp
>g++ test1.cpp
>./a.exe
inf

See there. It is inf, regardless or whether you or me like it or not (I
do!)

Cheers
Paavo


gwowen

unread,
Jun 11, 2015, 4:46:08 PM6/11/15
to
On Thursday, June 11, 2015 at 7:42:10 PM UTC+1, Mr Flibble wrote:

> The real projective line looks like nonsense to me mate. 1/0 is undefined.

(... shakes head sadly ...)

There is little worse than a man who is proudly ignorant.

Mr Flibble

unread,
Jun 11, 2015, 7:55:51 PM6/11/15
to
If you write code that treats division by zero as infinity then you
better keep that code to yourself; the world doesn't need it.

/Flibble
0 new messages