Opened 9 years ago

#9013 new Feature Requests

Create enum to make sign result more readable.

Reported by: Joaquim Duran Comas <jduran.gm@…> Owned by: John Maddock
Milestone: To Be Determined Component: math
Version: Boost 1.54.0 Severity: Cosmetic
Keywords: math, sign, enum type Cc:

Description

The function 'sign' return 1, 0 or -1 depending on the signal of the z, but comparing the result with -1, 0 or 1 constants could make the source code bad readable specially for people that doesn't know what the 'sign' function return.

if (math::sign(value) == -1){
....
}

I propose to create an enumerate type to test the result of the function with a constant, to create a more readable code. The compatibility with the previous implementation of the function is not broken.

Suggested implementation of code:

enum Sign
{
	SIGN_NEGATIVE = -1,
	SIGN_ZERO = 0,
	SIGN_POSITIVE = 1
}

template <class T>
inline Sign sign BOOST_NO_MACRO_EXPAND(const T& z)
{
   return (z == 0) ? SIGN_ZERO : (boost::math::signbit)(z) ? SIGN_NEGATIVE : SIGN_POSITIVE;
}

The previous example comparison could be write as:

if (math::sign(value) == math::SIGN_NEGATIVE){
....
}

Change History (0)

Note: See TracTickets for help on using tickets.