ID:1395821
 
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
There are a few basic math functions that for whatever reason are not included in the DM language. While they're easy enough to implement in softcode, that does represent a fairly large performance hit that doesn't seem to really be necessary.

The math functions I'd like to propose be added to the DM language are the following:

tan(x):
tangeant; much faster to call it natively than to call "sin(x) / cos(x)" in softcode
proc/tan(var/V)
. = sin(V) / cos(V)


sign(x):
sign(x) would return the sign of X, either -1, 0, or 1. In the case of -1.#INF or 1.#INF, would return -1/1 respectively, but for any .#INV value, would return .#INV.
proc/sign(x)
. = x ? x / abs(x) : 0

fix(x):
fix(x) is a bit like round(x), but in the event of a negative number, rounds towards 0 instead of away from it. e.g. fix(1.2) = 1, fix(-3.4) = -3. round(-3.4) = -4, according to the DM help file.
proc/fix(x)
. = x ? round(abs(x)) * sign(x) : 0
Bump
Of those the only one I'd be seriously interested in adding to the bytecode is tan(), if only because it feels like it's missing. I do like a good sign() function, but I'd do it this way instead to avoid a pointless division:

proc/sign(x)
. = (x>0) ? 1 : ((x<0) ? -1 : 0)

I also would never name an integer-part proc fix(), which is really, really nonstandard terminology. I'd call it int(). The functions ip() and fp() (integer and fraction parts) are relatively common. However this would be easy to do with soft code.

proc/int(x)
. = (x>=0) ? round(x) : -round(-x)