Conversation
|
I wonder if it would be beneficial to let |
I contemplated this, although I kept with consistency to return the same type as parameters (double in double out). e.g. https://en.cppreference.com/c/numeric/math/trunc - in ucode, type coercion is trivial, so I suppose having an int wouldn't hurt. Additionally, I could document the types as
I missed that. Suggestion: the maths versions be called fmin/fmax since it uses those from math.h? |
using fmin and fmax which take double values. grows math lib by 48 bytes Signed-off-by: Paul Donald <newtwen+github@gmail.com>
using fmin and fmax grows math lib by 32 bytes Signed-off-by: Paul Donald <newtwen+github@gmail.com>
sign - uses simple logic cast where true is 1 and false is 0. signbit - follows IEEE-754 behaviour (-0.0 returns -1) signnz - returns either -1/1. grows math lib by 80 bytes Signed-off-by: Paul Donald <newtwen+github@gmail.com>
grows math lib by 24 bytes Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Finds the largest integer value not greater than x. grows math lib by 40 bytes Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Computes the smallest integer value not less than x. grows math lib by 24 bytes Signed-off-by: Paul Donald <newtwen+github@gmail.com>
round x down or up to nearest integer value. grows math lib by 24 bytes. Signed-off-by: Paul Donald <newtwen+github@gmail.com>
truncate away the decimal portion of a floating point number to produce an integral number. grows math lib by 24 bytes. Signed-off-by: Paul Donald <newtwen+github@gmail.com>
log10 and log2 grows math lib by 136 bytes. Signed-off-by: Paul Donald <newtwen+github@gmail.com>
-p == JSON rendering -e == string rendering Signed-off-by: Paul Donald <newtwen+github@gmail.com>
boolean check to determine whether value is Infinity. grows math lib by 40 bytes Signed-off-by: Paul Donald <newtwen+github@gmail.com>
arccosine, arcsine, and arctangent functions. grows math lib by 208 bytes Signed-off-by: Paul Donald <newtwen+github@gmail.com>
hyperbolic cosine, hyperbolic sine, and hyperbolic tangent functions. grows math lib by 4296 bytes Signed-off-by: Paul Donald <newtwen+github@gmail.com>
cube root and hypotenuse functions grows math lib by 136 bytes Signed-off-by: Paul Donald <newtwen+github@gmail.com>
|
You could keep the math versions as min() and max() - question is rather whether we accept the redundancy or not. One could think of math.min() and math.max() as a restricted "fast" subset of core.min() and core.max(), but question is how useful this is. The core version is variadic for example while the math version is not, making it somewhat unusable to e.g. find the global minimum or maximum of an array, dimishing the usefulness of their "fast" or "numeric only" property somewhat. About the trunc() et al return value type: boils down what the most common use case and context is. Some thoughts:
Another consideration is that small int results could be stored in tagged pointers, a double result will always require a heap allocation (8 + 8 bytes) |
|
OK - I made those output integers. I added a boolean flag which toggles output type for floor/ceil/round/trunc (which has the benefit of retaining I renamed min/max -> fmin/fmax. I think they're OK to have since their behaviour mirrors that of math.h which aligns with usage expectations, although admittedly finding the min/max in an array does seem to have more utility. |
expm1(x) computes exp(x) - 1 accurately for small x log1p(x) computes log(1 + x) accurately grows math lib by 152 bytes Signed-off-by: Paul Donald <newtwen+github@gmail.com>
links now resolve * as multiply needs space so it is not interpreted as italic formatting. Signed-off-by: Paul Donald <newtwen+github@gmail.com>
default (false) when flag not provided is int64 output set flag to true to output double Signed-off-by: Paul Donald <newtwen+github@gmail.com>
|
Out of curiosity, I benchmarked the main and math variants for a million rounds each: math variant import { fmin, fmax } from 'math';
for (let i = 0; i < 1000000; i++) {
let x = fmin(3, 4);
}
for (let i = 0; i < 1000000; i++) {
let x = fmax(4, 3);
}main variant for (let i = 0; i < 1000000; i++) {
let x = min(3, 4);
}
for (let i = 0; i < 1000000; i++) {
let x = max(4, 3);
}outputs showed a varied but distinct difference: |
|
trunc variants showed a ~10% speed improvement to output int vs double. int import { trunc } from 'math';
for (let i = 0; i < 1000000; i++) {
trunc(2.73);
}double import { trunc } from 'math';
for (let i = 0; i < 1000000; i++) {
trunc(2.73, true);
}output: |
A range of math functions found in math.h are now exposed for use. A few functions could be trivially written in ucode, but their inclusion here makes any resulting ucode more compact, less error prone (helps us abdicate thinking) and results in a more comprehensive math library.
The net growth is roughly +5KB.