News for hackers. Stuff that matters.
Re:Stage 3 Proposal: Array.prototype.at by dlojudice on Wednesday June 02, 2021 @09:32AM (#27368474)
JS urgently needs support for decimals [1] and thus be able to enter sectors such as scientific and financial.

V8 + dynamic language + better math support = perfect environment for many applications out there

[1] https://github.com/tc39/proposal-decimal

Re:Stage 3 Proposal: Array.prototype.at by cogman10 on Wednesday June 02, 2021 @12:10PM (#27370390)
Couldn't that be done with WASM today? It looks like a few people have managed to build GMP targeting WASM.

You might miss out on some SIMD operations, but you'd be able to do 90% of what you'd want with scientific calcs. (admittedly, it'd be nice if this were part of the language).

Re:Stage 3 Proposal: Array.prototype.at by diamond_hands on Wednesday June 02, 2021 @02:42PM (#27372379)
There's still a ton of overhead for WASM and it's not first class. Using WASM is a lot like using C code in python. It's still nice to have features as first class citizens :)
Re:Stage 3 Proposal: Array.prototype.at by schwartzworld on Wednesday June 02, 2021 @06:19PM (#27374746)
Isn't most python just stringing together c and Fortran libraries that expose a python interface?
Re:Stage 3 Proposal: Array.prototype.at by Macha on Thursday June 03, 2021 @12:15PM (#27383534)
It amazes me at Python web engineers and Python ML engineers who go along completely unaware of the existence of the other group in any large numbers. (I say this as someone who went to my local pycon 5-6 years ago and was surprised that ML talks were like 30% of the topics)
Re:Stage 3 Proposal: Array.prototype.at by runarberg on Wednesday June 02, 2021 @12:29PM (#27370672)
Honest (possibly stupid) question: Do people working on statistical and probability application never need a numeric type optimized for probability values (i.e. values between 0 and 1 that can get really close to either; for example the really almost certain value of p = 1 - 10^-13)? In the same way that financial application needs a decimal type?

I’ve only worked on probability applications at a surface level for a tiny bit and never really needed it, but I kept wondering whether there were such a numeric types, and if not, what people did if they needed it.

Re:Stage 3 Proposal: Array.prototype.at by joppy on Wednesday June 02, 2021 @12:47PM (#27370895)
Log probabilities are used a bit for this, and most numeric packages (numpy for instance) have special functions for computing log(1+x) which are more accurate when x is tiny.
Re:Stage 3 Proposal: Array.prototype.at by 6gvONxR4sf7o on Thursday June 03, 2021 @12:56PM (#27384129)
This would be really nice, but the only solution I know here involves working with numbers that represent a function of p rather than p itself. And what you do depends on what you're computing. In general, you'll find lots of useful math functions that have specialized routines around zero and one, like numpy's log1p function, logaddexp, expm1, and company.

Log odds ratios are also good representations that have this kind of accuracy between zero and one. They're good when you need them. (e.g. 100:1 odds are log(100/1) and 1:100 odds are log(1/100))

Re:Stage 3 Proposal: Array.prototype.at by runarberg on Thursday June 03, 2021 @01:29PM (#27384596)
I see. So basically you just know a bunch of log rules and then map your distribution into a log-distribution. And I guess this becomes second nature to a seasoned statistician. So a dedicated numeric type for probability value is nothing more than a nice-to-have.
Re:Stage 3 Proposal: Array.prototype.at by 6gvONxR4sf7o on Thursday June 03, 2021 @01:46PM (#27384868)
Kinda, but to make it more explicit: with floating point numbers you don't even need the explicit log distribution because it's kinda baked in already, with the number split between an absolute and an exponential part. So floats capture an exponential scale of precision naturally, allowing you to write 1e-200 and 1 and 1e200 just as easily, with a common relative precision around each. You use the log scale as a mental model and then just keep floating point issues in mind while you work.

The handy functions are more about making sure that cancellations don't happen by providing a few primitive operations that cover lots of the uses. Like the handy log1p which computes log(1+x). If you did this with the log function, you'll compute log(1+1e-200)=log(1)=0. If you use log1p, you get log1p(1e-200)=1e-200. You avoid the loss of info that comes with adding something close to zero to something that isn't close to zero, which is the real trick. And then if you need something close to one, you probable just use the converse: instead of using p=1-eps, you just work with eps itself.

Re:Stage 3 Proposal: Array.prototype.at by Aardwolf on Wednesday June 02, 2021 @12:37PM (#27370778)
For financial makes sense, but science should be independent of numerical base?
Re:Stage 3 Proposal: Array.prototype.at by ihuman on Wednesday June 02, 2021 @01:12PM (#27371268)
I think the issue isn't the base, but that Javascript stores all numbers as 64-bit floats.
Re:Stage 3 Proposal: Array.prototype.at by dragonwriter on Wednesday June 02, 2021 @02:12PM (#27372042)
The decimal proposal is a vague enough that it is unclear if it would improve on that; if it was arbitrary-precision decimals, that would be an improvement. If it is limited precision but better than Number (seems unlikely), it would be an improvement. If it is limited precision but not better range/precision than Number, just decimal rather than binary floating point, its probably not an improvement. The proposal is vague enough that any of those would fit.
Re:Stage 3 Proposal: Array.prototype.at by dfabulich on Wednesday June 02, 2021 @01:33PM (#27371576)
JS has BigInt now. You can kinda fake BigDecimal with it, but none of the built-in operators will work.
Re:Stage 3 Proposal: Array.prototype.at by runarberg on Wednesday June 02, 2021 @03:22PM (#27372867)
How do you fake BigDecimal with BigInt? My immediate intuition would have a 3-tuple (a, b, c) which would construct the decimal with a representing the numbers before the decimal separator, b the number of zeroes immediately after the decimal separator, and c the numbers after the zeroes. e.g. 1.000054 would be represented as `[1n, 4n, 54n]` and 3.14159 as `[3n, 0n, 14159n]`.

Is there a better way?

Re:Stage 3 Proposal: Array.prototype.at by lbussell on Wednesday June 02, 2021 @03:59PM (#27373349)
What about a 2-tuple with two numbers, one being the BigInt and one being the exponent. So 1.000054 would be (1000054, -6) = 1000054*10^(-6).

Yours is probably more space efficient though.

Re:Stage 3 Proposal: Array.prototype.at by nine_k on Thursday June 03, 2021 @11:57AM (#27383283)
This is basically floating-point.

IEEE 754, BTW, allows for 10-based floats, but IDK if anybody uses it.

Re:Stage 3 Proposal: Array.prototype.at by wizzwizz4 on Wednesday June 02, 2021 @07:15PM (#27375196)
Yours is more space-efficient for real-world numbers.
Re:Stage 3 Proposal: Array.prototype.at by xhrpost on Thursday June 03, 2021 @12:29PM (#27383737)
Very good point. I'm just now fully understanding why at my last fin-tech job we stored all monetary amounts in pennies. I thought at first it was just ideal to keep everything as integers in the database but I see now that due to our Node backend, something as simple as 0.20 + 0.10 could pose a problem.
Re:Stage 3 Proposal: Array.prototype.at by spankalee on Thursday June 03, 2021 @06:42PM (#27388384)
That's not due to Node, that's due to IEEE 754 standard floating point operations.
Re:Stage 3 Proposal: Array.prototype.at by erikpukinskis on Thursday June 03, 2021 @09:39PM (#27389532)
What would the benefits of a language native implementation be, over something like decimal.js?

Is it just a matter of there being a "blessed" implementation for folks to standardize around?

Re:Stage 3 Proposal: Array.prototype.at by ranguna on Friday June 04, 2021 @02:57AM (#27391308)
Universal support for third party integrations such as connecting with a database that allows for arbitrary precision decimals, communicating with other services without forcing a use of a random lib (because there are many that do this kind of thing).

Support for this I believe is "low level enough" to ve implemented natively, it's not something like protobuf.

Re:Stage 3 Proposal: Array.prototype.at by z3t4 on Thursday June 03, 2021 @12:08PM (#27383446)
We also have a unix timestamp issue coming up.