www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Need a std::numeric_limits<T>::lowest() equivalent

reply Xavier Bigand <flamaros.xavier gmail.com> writes:
Hi,

I am creating an AABB struct that should be initialized in an invalid 
state. Here is my actual code :

 struct AABB(type, int dimensions)
 {
 	static assert(dimensions == 2 || dimensions == 3);

 	VectorType	min = Vector!(type, 3)(type.max, type.max, type.max);
 	VectorType	max = Vector!(type, 3)(-type.max, -type.max, -type.max);

 	bool	isValid() const
 	{
 		return max >= min;
 	}

 	private alias VectorType = Vector!(type, dimensions);

 	unittest
 	{
 		AABB2i	aabbInt;
 		AABB2f	aabbFloat;

 		assert(aabbInt.isValid == false);
 		assert(aabbFloat.isValid == false);
 	}
 }
Using -type.max is close to what I want but for integer int.min is different than -int.max. In c++11 there is std::numeric_limits<T>::lowest() that works perfectly. Can an equivalent property added to integer and floating types? PS: I prefer to use border values to be sure that the AABB stay invalid with a partial initialization, else using -1 and 1 give the same result ;-)
Jan 20 2017
next sibling parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Friday, 20 January 2017 at 23:45:38 UTC, Xavier Bigand wrote:
 Using -type.max is close to what I want but for integer int.min 
 is different than -int.max.

 In c++11 there is std::numeric_limits<T>::lowest() that works 
 perfectly.

 Can an equivalent property added to integer and floating types?
In D, these are builtin properties doced here: http://dlang.org/spec/property.html Is that what you where looking for?
Jan 20 2017
parent reply Xavier Bigand <flamaros.xavier gmail.com> writes:
Le 21/01/2017 à 00:50, Nordlöw a écrit :
 On Friday, 20 January 2017 at 23:45:38 UTC, Xavier Bigand wrote:
 Using -type.max is close to what I want but for integer int.min is
 different than -int.max.

 In c++11 there is std::numeric_limits<T>::lowest() that works perfectly.

 Can an equivalent property added to integer and floating types?
In D, these are builtin properties doced here: http://dlang.org/spec/property.html Is that what you where looking for?
Yes I saw that, but it seems few are missing std::numeric_limits<T>::lowest() is describe as "A finite value x such that there is no other finite value y * where y < x." I find something equivalent in the documentation.
Jan 20 2017
parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Saturday, 21 January 2017 at 00:03:11 UTC, Xavier Bigand wrote:
 std::numeric_limits<T>::lowest() is describe as "A finite value 
 x such that there is no other finite value y
        *  where y < x."
According to what I presume that definition means ("no other finite value" means "no other finite value representable in T"), that's just IntegerT.min or -FloatingT.max respectively. Shared numeric logic between integer and floating types is generally a bad idea in my experience, what's a compelling use-case for "lowest"?
Jan 21 2017
parent reply Xavier Bigand <flamaros.xavier gmail.com> writes:
Le 21/01/2017 à 10:54, John Colvin a écrit :
 On Saturday, 21 January 2017 at 00:03:11 UTC, Xavier Bigand wrote:
 std::numeric_limits<T>::lowest() is describe as "A finite value x such
 that there is no other finite value y
        *  where y < x."
According to what I presume that definition means ("no other finite value" means "no other finite value representable in T"), that's just IntegerT.min or -FloatingT.max respectively. Shared numeric logic between integer and floating types is generally a bad idea in my experience, what's a compelling use-case for "lowest"?
I don't see any other use case than for initialized maths struct to an invalid state, and because it is generally in template that working with integers and floats it is easier to have same properties (when it have the same meaning).
Jan 21 2017
parent Dominikus Dittes Scherkl <Dominikus.Scherkl continental-corporation.com> writes:
On Saturday, 21 January 2017 at 15:55:35 UTC, Xavier Bigand wrote:
 I don't see any other use case than for initialized maths 
 struct to an invalid state, and because it is generally in 
 template that working with integers and floats it is easier to 
 have same properties (when it have the same meaning).
I use /// consider T.max to be NaN of unsigned types /// and T.min to be NaN of signed types property bool isNaN(T)(const(T) x) pure safe nogc nothrow if(isIntegral!T) { static if(isSigned!T) return x == T.min; else // unsigned return x == T.max; } /// add a property to numeric types that can be used as return value if a result is out of bounds template invalid(T) if(isNumeric!T) { static if(isFloatingPoint!T) property enum invalid = T.init; else static if(isSigned!T) property enum invalid = T.min; // 0x80..00 else // unsigned property enum invalid = T.max; // 0xFF..FF } /// returns the save (not invalid) minimum value for the given type template smin(T) if(isNumeric!T) { static if(isFloatingPoint!T) property enum smin = -T.max; // T.min is the smallest representable positive value!! else static if(isSigned!T) property enum smin = T(T.min+1); else // unsigned property enum smin = T.min; // 0 } /// returns the save (not invalid) maximum value for the given type template smax(T) if(isNumeric!T) { static if(isUnsigned!T) property enum smax = T(T.max-1u); else property enum smax = T.max; }
Jan 23 2017
prev sibling parent Nornimices <rattipxwnta hotmail.com> writes:
I am not sure about it.
Feb 17 2017