www.digitalmars.com         C & C++   DMDScript  

D - Numeric sign improvement

reply Mark Evans <Mark_member pathlink.com> writes:
The online D documentation says

Properties for Integral Data Types
sign    should we do this?

Properties for Floating Point Types
sign    1 if -, 0 if +

This spec is really, really bad.  I use signs in algorithm work.  Most of the
time they end up as multiplication factors.  So I want negative signs to yield
-1.0, and positive signs to yield +1.0.  The way it should work is

sign    -1.0 if negative, +1.0 if positive or zero or NaN

for both ints and floats.  The zero case should be considered positive.  (I
don't want .sign to return zero, otherwise I can't use it as a multiplication
factor without special checking.)

Mark
Oct 05 2002
parent reply "Walter" <walter digitalmars.com> writes:
Interesting. The .sign used the C signbit() function as a model. How do you
do your code in C/C++?

"Mark Evans" <Mark_member pathlink.com> wrote in message
news:anng69$1m7f$1 digitaldaemon.com...
 The online D documentation says

 Properties for Integral Data Types
 sign    should we do this?

 Properties for Floating Point Types
 sign    1 if -, 0 if +

 This spec is really, really bad.  I use signs in algorithm work.  Most of
the
 time they end up as multiplication factors.  So I want negative signs to
yield
 -1.0, and positive signs to yield +1.0.  The way it should work is

 sign    -1.0 if negative, +1.0 if positive or zero or NaN

 for both ints and floats.  The zero case should be considered positive.
(I
 don't want .sign to return zero, otherwise I can't use it as a
multiplication
 factor without special checking.)

 Mark
Oct 05 2002
parent reply Mark Evans <Mark_member pathlink.com> writes:
The closest function is sgn() though some implementations return zero for zero.
I've not heard of signbit() and don't consider it a good model.  The problem is
that .sign wants to return a bool (saying whether the sign bit is set), but D
offers no bool type. The return must then be encoded as an integer.  It is very
confusing to get +1 from .sign when the numeric sign is -1.

I don't know any scenarios in which I care whether the sign bit is set; what I
always want is the value of the sign, -1 or +1.  The return value should have
the same numeric sign as the argument.  In this mode the return value really is
an integer or float, not an integer masquerading as a boolean.

M.

In article <ano1r9$27pp$2 digitaldaemon.com>, Walter says...
Interesting. The .sign used the C signbit() function as a model. How do you
do your code in C/C++?
Oct 05 2002
parent reply "chris jones" <flak clara.co.uk> writes:
should it be that...

signbit() should return 0 or 1 as a representation of the sign bit,

sign() should return -1 and 1 as a representation of the sign.

i agree with you that the latter is much more usefull and the result should
bo of the same type as the argument.

chris


"Mark Evans" <Mark_member pathlink.com> wrote in message
news:ano76a$2cqd$1 digitaldaemon.com...
 The closest function is sgn() though some implementations return zero for
zero.
 I've not heard of signbit() and don't consider it a good model.  The
problem is
 that .sign wants to return a bool (saying whether the sign bit is set),
but D
 offers no bool type. The return must then be encoded as an integer.  It is
very
 confusing to get +1 from .sign when the numeric sign is -1.

 I don't know any scenarios in which I care whether the sign bit is set;
what I
 always want is the value of the sign, -1 or +1.  The return value should
have
 the same numeric sign as the argument.  In this mode the return value
really is
 an integer or float, not an integer masquerading as a boolean.

 M.

 In article <ano1r9$27pp$2 digitaldaemon.com>, Walter says...
Interesting. The .sign used the C signbit() function as a model. How do
you
do your code in C/C++?
Oct 06 2002
parent Mark Evans <Mark_member pathlink.com> writes:
Yes there could be two properties, .sign and .signbit; .sign is by far the more
useful.

Mark
Oct 06 2002