Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
October 05, 2002 Numeric sign improvement | ||||
---|---|---|---|---|
| ||||
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 |
October 06, 2002 Re: Numeric sign improvement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark Evans | 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 > > |
October 06, 2002 Re: Numeric sign improvement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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++? > |
October 06, 2002 Re: Numeric sign improvement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark Evans | 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++? > > > > |
October 06, 2002 Re: Numeric sign improvement | ||||
---|---|---|---|---|
| ||||
Posted in reply to chris jones | Yes there could be two properties, .sign and .signbit; .sign is by far the more useful. Mark |
Copyright © 1999-2021 by the D Language Foundation