From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 3B976BBBB for ; Sun, 19 Feb 2006 16:44:16 +0100 (CET) Received: from cenn.mc.mpls.visi.com (cenn.mc.mpls.visi.com [208.42.156.9]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k1JFiFKS025532 for ; Sun, 19 Feb 2006 16:44:15 +0100 Received: from [192.168.42.2] (bhurt.dsl.visi.com [208.42.141.66]) by cenn.mc.mpls.visi.com (Postfix) with ESMTP id 47AA18389; Sun, 19 Feb 2006 09:44:14 -0600 (CST) Date: Sun, 19 Feb 2006 09:44:39 -0600 (CST) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: Joshua Smith Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] What library to use for arbitrary precision decimals In-Reply-To: Message-ID: References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Miltered: at nez-perce with ID 43F8924F.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 rounding:01 integers:01 computations:01 instruments:98 penny:98 1,000:98 wrote:01 arbitrary:01 arbitrary:01 integer:01 integer:01 functions:01 define:01 int:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=US_DOLLARS_3 autolearn=disabled version=3.0.3 On Sun, 19 Feb 2006, Joshua Smith wrote: > There are a couple of ways to handle money transactions without > rounding errors. > > You could use the Nums library, which provides arbitrary precision > calculations and numbers. But even with arbitrary precision numbers, > you still can have the situation where you get 405.0345 which if this > were USD, you would still have to round if you wanted to pay someone > this amount. You will still have the arbitrary precision this way, > however. > > The best way to handle money (in my experience) is to use integers. > Then you can define conversion functions but only have to convert it > to decimal for display purposes. That way, even if you do a million > transactions you won't lose any information. You can also handle > non-decimal based currencies/instruments/transactions that way[1] I agree with this recommendation. The basic idea is that you use fixed point. Say you want to be accurate to one thousandth of a cent (0.001 cents). You simply do all calculations in terms of millicents. So the integer 1 represents one millicent. One penny is represented as the integer 1,000- one thousand millicents. The amount $2,345.67 is represented by the integer 234,567,000. If you use the Int64 module as the basis for your computations, you can hold values up to $184,467,440,737,095.51 in size. This is larger than the world's GDP, so it should be large enough. 32 bits isn't enough- that only allows you to hold values up to $42,949.67. Brian