[plt-scheme] Is plt-scheme appropriate for applications that use currency?

From: John Clements (clements at brinckerhoff.org)
Date: Mon May 7 15:17:55 EDT 2007

On May 7, 2007, at 12:04 PM, Scott Hickey wrote:

> I have been taking a closer look at Scheme and trying to understand  
> the numeric types. In nearly every language I've examined, the  
> default numeric representation for numbers with a decimal place is  
> binary floating point. Thus, asserts like *like* (actually numbers  
> differ by language) 1.1 + 0.1 == 1.2 fail. This obviously doesn't  
> work well in any application involving currency.
>
> In languages like Java and Ruby, I can get around this by  
> explicitly declaring numbers to be of type BigDecimal. In Groovy  
> (http://groovy.codehaus.org) and Rexx (http://www.rexxla.org/),  
> this math support is there by default for numeric literals.
>
> Can someone clarify what happens in plt-scheme or Scheme in  
> general? TIA.

PLT Scheme (and most other flavors of Scheme) support a wide variety  
of numeric representations, including rationals.  So, for instance:

computer:/tmp clements$ mzscheme
Welcome to MzScheme v369.8 [3m], Copyright (c) 2004-2007 PLT Scheme Inc.
 > (+ 110/100 010/100)
6/5
 >

With that said, however, it seems that you probably don't really want  
the full power of rationals, either.  In fact, for most currency  
manipulations involving a fixed smallest unit, you almost certainly  
want to use simple integers.  That is, if your smallest unit is (say)  
a tenth of a penny, then you would simply represent a dollar as  
"1000".  Naturally, when you print these integers out you would dress  
them up with syntactic eye-candy like a decimal place, a dollar sign,  
and possibly some commas.

No?

Hope this helps,

John Clements

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2223 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20070507/72ea8fae/attachment.p7s>

Posted on the users mailing list.