haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Min/Max constants for numeric values

Open RealyUniqueName opened this issue 8 years ago • 9 comments

Haxe lacks min/max constants for numeric (Int, Float, UInt) types. There is an implementation in as3hx Float Int Those constants could be added to Math class or to corresponding base types (like Int.MAX, Float.MIN etc).

RealyUniqueName avatar Feb 24 '17 13:02 RealyUniqueName

I can make PR ;)

SlavaRa avatar Feb 24 '17 13:02 SlavaRa

++

BregoXX avatar Feb 24 '17 14:02 BregoXX

Any luck with this?

trsh avatar Nov 02 '18 08:11 trsh

This would totally help me too. If it's difficult to do with built-in types like Int is it at least possible to offer it on additional targets? I'm often needing it on cpp, where it should be easy (using std::numeric_limits).

thomasjwebb avatar Mar 19 '19 16:03 thomasjwebb

use https://github.com/skial/min-max

kevinresol avatar Mar 19 '19 16:03 kevinresol

That only provides for two types (Float and Int).

thomasjwebb avatar Mar 19 '19 16:03 thomasjwebb

I thought about this a little yesterday and I think it would be better if the interface was more like how it is in c++, i.e., using a type parameter. This would allow one to write generic algorithms that could work on different integer types, rather than littering your code with conditionals or having to use macros:

function squareWave<T>(phase:T):T
{
    if (phase > (NumericLimits<T>.max() + NumericLimits<T>.min() / 2)) {
        return NumericLimits<T>.max();
    } else {
        return NumericLimits<T>.min();
    }
}

edit: the above code would work on all numeric types, not just integer

thomasjwebb avatar Mar 20 '19 15:03 thomasjwebb

Maybe increase priority on this issue or schedule for a release? This seems to be a very useful feature.

nwawrzyniak avatar Aug 20 '21 18:08 nwawrzyniak

+1

onehundredfeet avatar Aug 11 '22 19:08 onehundredfeet

Just ran into this, and it would definitely be nice to have built in

AdamHarte avatar Jan 05 '23 21:01 AdamHarte