5 ms·
My guess is that at least half of the cases where floats are applied could be better served using scaled integers. Real world practicalities limit the the need
by jqpabc123 4y ago
My guess is that at least half of the cases where floats are applied could be better served using scaled integers.
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.
- tialaramex 4y agoFloats are really nice for audio (PCM) which may not be intuitive. 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)
- jqpabc123 4y agoIt's much nicer 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.
- tialaramex 4y agoNot for developers, it's much nicer for the users. There's a phrase "infinite headroom" which might make this a bit sharper. 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.
- jqpabc123 4y agoDon't get me wrong --- there are perfectly valid use cases for floats --- and it sounds like you may have one. But these valid uses are a lot less than what I see in a lot of software.
- ttoinou 4y agoSaid 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 numbers
- pclmulqdq 4y agoThat's not why audio processing software uses floats. Floats are used because they effectively let you decouple scale from precision. With scaled integers, soft sounds only have a few bits of precision, while loud sounds are very precise. With floats, every signal is within 1 bit of precision of each other, regardless of volume. That means you can raise and lower the volume on signals over and over without significant precision loss.
- tialaramex 4y agoWhat part of what I wrote do you think you're disagreeing with ?
- pclmulqdq 4y agoThe part where you said that floats were used because it was easier. It has definite benefits for audio quality to have higher dynamic range in your numerics. Edit: You said it was nicer, not that it was necessarily easier. They are synonyms.
- tialaramex 4y agoI'm sure you believe I said "floats were used because it was easier" but I didn't, which is why if you go look at the comment you replied to that's not what it says.
- tialaramex 4y agoOK, now you've edited your reply to say you believe "nicer" is a synonym for "easier". But, it's just not.
- di4na 4y agoThat sound nice until you start dealing with all kind of algorithm like hyperloglog.
- jqpabc123 4y agoPerfectly valid use cases for floats do exist --- but these are a lot fewer than what I see being done in the real world. Most simple accounting applications involving time and money can be better addressed without them.
- gw99 4y agoI tend to use “HP” style decimal floating point types if accuracy is required. The mantissa is stored as one base 10 digit per 4 bits (BCD encoded). A 64 bit int gives 12 digits of precision and 3 digit mantissa plus flags. This can be scaled to 128 bits if needed easily. 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.
- hnews_account_1 4y agoMoney settling is only a small aspect of finance. In most cases, more precision is required. Not like 15 significant digits or something but more than 2 for sure.
- jqpabc123 4y ago4 decimals can adequately address most interest rate calculations. 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.
- hnews_account_1 4y agoYep. 4 decimals is plenty for every application and as you said, just multiply it by a million if not. I was more addressing the issue that on some online forums people tend to think finance needs specialized 2 decimal software. Only a fraction of finance does. Most others just use the usual IEEE 754 specification that is standard in modern programming languages and OSes for their daily use. Heck, excel uses IEEE 754 for most cases and like 95% of finance runs on excel.
- SideQuark 4y agoDi you try it? Have you written financial software? This is terrible advice. 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.
- Merad 4y ago> For example, most monetary transactions are settled to the nearest penny. 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.
- jqpabc123 4y agoWhen the bill is issued, round the amount due and recorded to the nearest penny (integer) since this is what is being paid. Do this continuously and consistently and you can easily avoid the "mystery" penny problem.
- Merad 4y agoYes, obviously that's an option, but it's one that gives up revenue. At that company most clients were billed monthly, and it was a small-ish company, so the loss wouldn't be huge (I'd guess mid to high 5 figures). However many payment processors do daily billing, meaning you'd be giving up a penny every few days instead of every few months. 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.
- jqpabc123 4y agobut it's one that gives up revenue. 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".
- constantcrying 4y agoThis is a complete fallacy. Just because output and input accuracy are low, that does not imply that intermediate accuracy can be low too. You 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.