3 ms·
He really should have used fixed-point arithmetic.
by greiskul 14y ago
He really should have used fixed-point arithmetic.
- thaumaturgy 14y agoWhy? If I found out that the library that I was using didn't have good fixed-point support (i.e., "doesn't do what I expect"), and if I didn't need the values to be infinitely precise, I'd just use a multiplier. It's always worked well for me in the past. e.g., 7 * 215 = 1505.
- tehwalrus 14y agoLuckily, I've never had to write any database code that deals with money. If I had, though, I would have insisted on integers where a penny is 1 like this - it just doesn't make any sense to store an exponent for a bunch of values that are all the same order of magnitude anyway. (I assume that this is how "fixed point" algebra works, although what I assume computers do with floats and what they actually do ain't exactly ever been similar.)
- jmartinpetersen 14y agoThis would, however, totally miss the point that a penny actually isn't the smallest meaningful unit of money used in systems tracking actual money. Assuming that the order of magnitude is fixed, this is of course doable, but according to my experience the order of magnitude does not converge.
- tehwalrus 14y agoAh, but it would work fine on a website where you're selling things to humans (whose bank accounts can't store ha-pennies). I don't think anyone would be foolish enough to let the physicists near financial markets - there would be heavy losses on both sides.
- nitrogen 14y ago(I assume that this is how "fixed point" algebra works, although what I assume computers do with floats and what they actually do ain't exactly ever been similar.) You might enjoy reading about IEEE-754, then: https://en.wikipedia.org/wiki/IEEE_754-1985 https://en.wikipedia.org/wiki/IEEE_754-1985
- mistercow 14y agoDon't do that. All of the major database players have built in fixed point decimal types with user specified precision. You'd be reinventing the wheel. As far as doing mathematical operations on the values, if you're using double precision floats, you have 15 digits of precision before you run into problems. In most situations, you're going to be dealing with values considerably less than 10 trillion dollars, so that will be totally sufficient. That's good news if you're using a language like JavaScript or Lua, where there is no built in integer type.
- tehwalrus 14y agoI've found (having written too much physics code for my own good) that floats give you 0 digits of precision - I ask for 4.0 and it gives me 3.999999999998 .. ;) The solution is, of course, to use rational numbers ( http://gmplib.org/ http://gmplib.org/ ) if you have time to debug why GMP doesn't compile on your processor.
- mistercow 14y agoYep, that's called "fixed point arithmetic". The multiplier is called a "scaling factor". You typically have to "rescale" after each multiply operation by dividing the result by the scaling factor, but you've done that implicitly by not scaling integer values like 7.
- anonymous 14y agoHe really should have used plain integers and calculated in cents.
- geon 14y agoThat is the definition of fixed point. (Although, if you do it for anything else than currency, you probably would do it in base 2 instead of 10. And yes, I know every base is base 10.)