by jqpabc123 · 4y ago · 0 comments
Real world practicalities limit the the need for extended precision in a lot of (if not most) cases.
For example, most monetary transactions are settled to the nearest penny. Most time clocks are only accurate to the nearest second. Most milling machines are only accurate to a single digit fraction of a millimeter.
Applying extended decimals to these may work --- but in engineering terms, this is really "false accuracy" that is unnecessary and can be counter productive.
It's much nicer during mixing, editing and other production steps to work with floats and so essentially all modern software does that, even though the A2D conversion is integer and the end results will be integer data (e.g. CD audio is Linear PCM 16-bits at 44.1kHz)
That means you can raise and lower the volume on signals over and over without significant precision loss.
Edit: You said it was nicer, not that it was necessarily easier. They are synonyms.
Said every developer who has ever applied floats where scaled integers would be just as effective.
I attribute this to pocket calculators which have shaped most people's thinking.
I've even had developers try to tell me it can't be done any other way --- until I show them.
All that said, if floats are needed to interface with other software, you may not have much choice in the matter.
With a conventional analogue recording medium, the medium has physical properties which impact the precision with which it can record audio. For example maybe the reel-to-reel tape recorders in a studio can do about 12-bit precision. At the bottom, the precision is cut off by noise, mechanical noise in the equipment and eventually even quantum mechanical noise in our universe which is unavoidable. At the top it's cut off by practical engineering limits - the magnetic field strengths are limited. A good analogue engineer ensures that there's "headroom" where if the recording is a little louder than expected it doesn't bump into that upper limit, when Aretha hits that note 6dB louder than anticipated, you had 10dB of headroom so it's captured OK. But to do this you're sacrificing that precision at the other end of the scale, listeners can't hear her turning the lyrics sheet in the background of the song even though you'd have heard that if you were in the room.
In digital integer space, we can have lots of precision, 14-bit happened early, 16-bit was soon common place, and today 24-bit is widely available (although you need to squint to see 24 actual bits, don't think about it too hard). But, the integers are brick walls at the edges of that range. If you set things up to use all your (say) 16-bits and it comes out hotter than expected, your data is trashed because it saturates † producing a harsh clipping. So you need to carefully set the headroom to give you as much space at the top as you need, but without wasting too much. Stressful.
With floats you get infinite headroom. Conventionally PCM audio is represented as -1.0 to 1.0, but in float space if you're "too hot" by a factor of literally 1000, it's no problem at all, floating point numbers can go from -1000.0 to 1000.0 just fine, and when you're ready to actually listen to it, just multiply them by 0.001 (this is called "gain" in audio).
† Or, if the programmer doesn't understand what they're doing, it wraps and sounds like complete nonsense. This is common on 1980s home computer audio setups.
But these valid uses are a lot less than what I see in a lot of software.
Said every developer who has ever applied floats where scaled integers would be just as effective.
Floats are great to prototype quickly and get a good result. Once the algorithm is found, you can spend more time fine tuning with improvements such as others kinds of ways to represent fractional numbersYou gave the example of a milling machine, of course in most cases you wouldn't expect it to be accurate to more then a 10th of milliliter. So you could believe that e.g. 100th of a millimeter should be more then enough as your scaled integer (meaning an integer with a value of "100" represents one millimeter). The maximum value of a 32bit integer would represent around 43km. Who would ever want to use more?
But what if now you are trying to calculate the 3D distance between two points which are about a meter (= 1000mm = (int)100000) apart in one direction. To calculate this you need a third power. 100000^3 is pretty large, much larger than a 32 bit int can hold...oops
With scaled integers you have to constantly think about whether each intermediate result is still within your range. It is a terrible, headache inducing format for many applications. The magic of floats is that you do not need to worry about so many things. Yes, they have their pitfalls, but they are used for a very good reason and "you don't need the precision" simply is false in many cases.
Settled, yes, but all kinds of financial operations require higher precision. IME with payment processing the backend calculations and account balances were done to a thousandth of a cent. It was a common point of confusion because when clients were billed for their processing fees they usually ended up with a fraction of a cent balance that couldn’t be billed and stayed on their account. Every couple of months those fractions of a cent would add up to a full cent and their detailed billing breakdown would include a 1 cent charge in the “other” category that would drive them crazy. But of course financial calculations usually don’t use floating point anyway.
Do this continuously and consistently and you can easily avoid the "mystery" penny problem.
I also overstated the customer impact... perhaps 1% of customers would inspect every detail of their statements and they tended to be annoyed by it. The overwhelming majority didn't notice or care.
Done consistently and properly, it is just as likely to *gain* revenue.
Over the course of time and probability, it should be revenue neutral.
I suggest looking into "banker's rounding".
I spent several years maintaining a C library that handled these for some embedded instrumentation. This included most key mathematical functions as well. Most interesting thing I ever had to look after.
One 3 line function can handle applying an interest rate to monetary amounts held as scaled integers. Instead of taking the time to write this one function, the typical developer will build the entire software around floats which causes endless issues.
Even the most naïve calculation, such as computing total amount paid at 8% compounded monthly over a 30 year mortgage, ends up wrong by over $100,000 compared to the correct answer. This is a pretty common mortgage calculation.
Here's the expression to evaluate: 1000000 * (1 + (8/100)/12) ^ (12*30). Do it, rounding to d digits of accuracy at each basic expression, and here are the errors you get:
2 -> 25 million
3 -> 1.3 million
4 -> $131,170
5 -> $13,040
6 -> $1304
7 -> $130
8 -> $13
9 -> $1.3
10 -> $0.13
11 -> $0.013
....
Thus, using anything less than a double, over large portfolios, is a terrible, terrible idea, even for such a simple calculation.Anything actually useful, such as summing such calculations over some portfolio, or handling sum over variable interest rate calculations, or, well, pretty much anything, completely pukes at 4 decimals.
This is why people that don't develop floating point with a solid understanding of how to compute and check things like condition numbers for calculations should not give advice on how much precision to use for things.
In fact, I'm hard pressed to find any reasonable financial computation that is not crap at 4 decimals.
This is perhaps why your "guess .. that at least half of the cases where floats are applied could be better served using scaled integers" is not a very good guess.
The proper answer is do not pick either unless one really understands the numerics, so if someone chooses "scaled numerics" over floating point because they find floating point complicated, then they are most certainly made an uninformed choice and likely an error.
Choosing floating point calculations over scaled numerics is much less likely to bite someone doing numerical math, since it can scale properly and dynamically to help hide their ignorance of numerical issues.
High finance uses float / double in general and doesn’t bother with the nuances of decimal representation or IEEE 754 unless they’re working too close to the silicon rather than doing model work in higher abstractions.
* software that allows only 2 decimal representation must account for these discrepancies by default even if it’s only off by a few cents
Assume Principal (P) is provided in pennies. In pseudo code:
P := P * 100 //temporarily scale out 2 extra digits for accuracy (4 decimals total)
for I:=1 to 360 do
P := P + ((P * 8)/1200)
return (P+50) div 100 //round back to nearest penny
By my calculations, this returns 1093572966 pennies or $10,935,729.66How did I do?
Thanks for playing.
Most simple accounting applications involving time and money can be better addressed without them.