Thread overview
Need a std::numeric_limits<T>::lowest() equivalent
Jan 20, 2017
Xavier Bigand
Jan 20, 2017
Nordlöw
Jan 21, 2017
Xavier Bigand
Jan 21, 2017
John Colvin
Jan 21, 2017
Xavier Bigand
Feb 17, 2017
Nornimices
January 21, 2017
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 ;-)
January 20, 2017
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?
January 21, 2017
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.

January 21, 2017
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"?
January 21, 2017
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).

January 23, 2017
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;
}

February 17, 2017
I am not sure about it.