Does Delphi have any inbuilt math functions that operate on arrays / lists / etc?  For example, System.Math has no Median function, so I just wrote one that happens to work with arrays of Int64. But really, a generic Median(...) function would be awesome.  (Syntax would probably have to be different given we don't have generic standalone functions yet, but you get the idea.)  Ditto other common methods that work on more than one or two values at a time.  If I've missed any inbuilt ones... where are they?

Comments

  1. David Millington Most numeric types have no use outside legacy support, and many have no use outside non-numeric cases (ie. they're used as IDs, enums, ordinals, bitwise masks, not really "numbers" in the mathematical sense), so the need for generic is very limited.

    In terms of floats, only Double is really relevant, and for integers, only 32 & 64bit have "numeric use".

    So such maths methods matter on only 3 types, but even just for those three types, you have interpretation issues which means there is not "one" code that is valid for all three...

    For instance median on float and median on integers cannot use the same code, because a midpoint is not always possible for integers, or a generic median could not have a generic result type.. which opens another can of worm as a generic implementation would thus have to use the highest precision numeric types of all the numeric types it may ever be specialized upon...

    I guess that only Min & Max could have a generic implementation, as even a simple Summation would be subject to interpretation: Integers will overflow but have no precision issues, Doubles likely won't overflow but will face precision issues. Single summation will face horrible precision issues and will require use of an higher precision internal type.

    So all in all, I gather a generic numeric library might be an interesting academic exercise, but would be quite useless for real-world numeric purposes :-)

    ReplyDelete
  2. Eric Grange All good points!

    I wish it was possible to have conditional compilation based on the type in a generic. (I think currently you can have code which is optimized out, eg an if statement based on the type, but the other branches still have to be valid code.) Perhaps I'm seeking for solutions which aren't really necessary though.

    All up I just want to (a) have more in the RTL and (b) write less duplicate code :)

    ReplyDelete
  3. There are numerous libs around for various types of math. I do miss an efficient high precision number format though - especially when doing large data sets.

    ReplyDelete

Post a Comment