<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sat, Mar 5, 2016 at 10:37 AM, Frank Staals <span dir="ltr"><<a href="mailto:frank@fstaals.net" target="_blank">frank@fstaals.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Why not just use Data.Fixed from base?<br>
<br>
> import Data.Fixed<br>
> read "4.02" :: Fixed E2<br>
> 4.02<br></blockquote><div><br></div><div>Thanks, I had overlooked that one. That is much more satisfyingly accurate for read/show reversibility.</div><div><br></div><div>It also has the advantage of encoding the precision in the type. Oddly, it doesn't allow all precisions but just gives you a fixed set of them. Also, It doesn't provide any operations for converting between the precisions, rounding, or divvying out values in proportions without losing pieces of it. And it forces you to stick with Integer as the underlying representation, whereas Decimal lets you choose which sort of Integral to use.</div><div><br></div><div>It's interesting that Data.Fixed uses a type class to encode the precision. I would have thought using a recursive type like Data.Number.Fixed would make the most sense (each type being defined to have a precision of a factor of 10 more than the preceding one), but now I wonder if there might be some loss of efficiency during conversion due to that.</div><div><br></div><div>Overall I would go for the Decimal library for currency manipulation. It was clearly designed explicitly for that purpose. The only disadvantages are due to storing the precision in the value: potential inefficiency, and ease of accidentally doing arithmetic on values of two different precisions without explicit coercion. The advantages include being able to choose the underlying integral type, show/read reversibility, and the library operations for divvying up amounts into proportions while guaranteeing that the sum matches the original value.</div><div><br></div><div>Not that one couldn't write operations to do the divvying, using Data.Fixed. Its constructor is exposed, so one can easily add new operations. Likewise, coercion operations could be added.</div></div></div></div>