Change behavior of float::nonpositive/nonnegative

Rather than being defined as !positive and !negative, these should act the
same as negative and positive (respectively).  The only effect of this change
should be that all four functions will now return false for NaN.
This commit is contained in:
Matt Brubeck 2011-10-27 17:45:04 -07:00 committed by Brian Anderson
parent 000b2fe9a6
commit 45d7777991
2 changed files with 30 additions and 8 deletions

View File

@ -259,17 +259,39 @@ pure fn ge(x: float, y: float) -> bool { ret x >= y; }
/* Predicate: gt */
pure fn gt(x: float, y: float) -> bool { ret x > y; }
/* Predicate: positive */
/*
Predicate: positive
Returns true if `x` is a positive number, including +0.0 and +Infinity.
*/
pure fn positive(x: float) -> bool { ret x > 0. || (1./x) == infinity(); }
/* Predicate: negative */
/*
Predicate: negative
Returns true if `x` is a negative number, including -0.0 and -Infinity.
*/
pure fn negative(x: float) -> bool { ret x < 0. || (1./x) == neg_infinity(); }
/* Predicate: nonpositive */
pure fn nonpositive(x: float) -> bool { ret !positive(x); }
/*
Predicate: nonpositive
/* Predicate: nonnegative */
pure fn nonnegative(x: float) -> bool { ret !negative(x); }
Returns true if `x` is a negative number, including -0.0 and -Infinity.
(This is the same as `float::negative`.)
*/
pure fn nonpositive(x: float) -> bool {
ret x < 0. || (1./x) == neg_infinity();
}
/*
Predicate: nonnegative
Returns true if `x` is a positive number, including +0.0 and +Infinity.
(This is the same as `float::positive`.)
*/
pure fn nonnegative(x: float) -> bool {
ret x > 0. || (1./x) == infinity();
}
//
// Local Variables:

View File

@ -47,7 +47,7 @@ fn test_nonpositive() {
assert(float::nonpositive(-1.));
assert(float::nonpositive(float::neg_infinity()));
assert(float::nonpositive(1./float::neg_infinity()));
// TODO: assert(!float::nonpositive(float::NaN()));
assert(!float::nonpositive(float::NaN()));
}
#[test]
@ -58,5 +58,5 @@ fn test_nonnegative() {
assert(!float::nonnegative(-1.));
assert(!float::nonnegative(float::neg_infinity()));
assert(!float::nonnegative(1./float::neg_infinity()));
// TODO: assert(!float::nonnegative(float::NaN()));
assert(!float::nonnegative(float::NaN()));
}