4 ms·
Why not javascript specifically? Using integers or floats is wrong in any language. You want a decimal representation library when dealing with money.
by Waterluvian 4y ago
Why not javascript specifically? Using integers or floats is wrong in any language. You want a decimal representation library when dealing with money.
- dijit 4y ago> Why not javascript specifically? Javascript has one number type; float.
- onion2k 4y agoIt's fair to say most JS code is only handling floats especially if you're just relying on JS's automatic casting but JS does have BigInt as well, and it has a whole bunch of numerical typed arrays that only accept specifically typed numbers like Uint8 or Int32 etc. You can do math in JS quite safely if you delve into the less well-known types a bit.
- eurasiantiger 4y agoNot true. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- dijit 4y ago1) Wow, I didn't know that existed. 2) Wow, those downsides are insane. It's not constant time, thus can't be used in cryptography...? Has huge performance impacts and can't be casted to a "number"- how is this implemented? it can't be native INTs on the CPU?
- eurasiantiger 4y agoAny cryptography is surely using Uint8Array.
- SgtBastard 4y ago>Any cryptography is surely using Uint8Array. Is this to optimise memory operations over a large plaintext/cipher text or is there some other advantage to it that is less obvious (e.g. more resistant to timing attacks?)
- eurasiantiger 4y agoNo, it’s because Number is internally floating point and cannot represent all integers.
- pclmulqdq 4y ago"Integer number of cents" is a fine way to deal with money unless you want sub-cent units. Otherwise, you can divide it up smaller: a few stock exchanges use [price in dollars] * 10^8 as their price unit, and store a 64 bit int.
- t_tsonev 4y agoExactly, and BigInt fits this role perfectly.
- twelve40 4y ago> "Integer number of cents" is a fine way to deal with money unless you want sub-cent units I use this approach to minimize the amount of time I have to waste on trivial money operations such as storage and api's, but this approach (number of cents) falls apart quickly the moment you need to do something as simple as calculate tax or a commission. So, still likely no getting around proper float math even if you may not really be all that interested in sub-cent units.
- Waterluvian 4y agoYou really want to use a decimal library for any serious financial math. And if you’re doing the math on tiny units of processing you’re going to want the precision and accuracy. You don’t need to work in decimal cents. Many work in thousandths of a cent or smaller. But you want an infinite upper and lower bound. Even 64 bit integers only has so much upper range depending on how far you subdivide a penny. You also want the precision guarantees when you take those numbers and do more than just add or subtract them.
- pclmulqdq 4y agoWhy do you need an infinite upper bound for money? I agree that you need to do saturating arithmetic, but 2^64/10^8 dollars is about 10^10 dollars, which is the around the GDP of the US. A number that big is not going to show up in your billing system if you do everything correctly. With all numerical calculations, you need to be careful about losing precision on mathematical operations, so it's a good idea to think about things like this even if you use a decimal number system with a very wide dynamic range - it won't be very precise all the time.
- throwaway294566 4y agoNo. You want a parametrized "Money" type, one that can do calculations in varying numbers of decimals, and configurable rounding. There are legal requirements that make this necessary. One (from memory, not exact) calculation I've had to implement for a customer: Multiply prices by whole numbers. Prices are given in cents subdivided to 5 digit precision, result should have 10cent precision, rounded down. Then, for the tax calculation, get taxes from some tables, 3 different taxes for each line item. Apply the first two taxes, Euros with 10 digit subdivisions, then round "according to trade custom" (which is the usual "up from .5 cents, down from below .5"). Then add the third tax, Cents with 5 digit subdivisions, round down. Then add all the line items and their taxes, separately as well as together. And if there is a Skonto, take care to properly unravel the tax stuff again. I've had to do it in plain Typescript, it is possible, but it really sucks. Those days when you yearn for COBOL... ;)