[plt-scheme] Is plt-scheme appropriate for applications that usecurrency?
In my opinion, Scheme provides a very good numerical system. May be this can help you:
(module currency->string mzscheme
(read-decimal-as-inexact #f)
(define (currency->string x)
(let-values
(((integer-part fraction)
(quotient/remainder (* 100 x) 100)))
(string-append
(number->string integer-part)
"."
(number->string fraction))))
(provide currency->string))
(module use-currency->string mzscheme
(require currency->string)
(display (let ((p (open-input-string "10.22"))) (currency->string (read p)))))
(require use-currency->string) ; displays 10.22
With respect to currencies make sure to use arithmetic functions that produce exact values when given exact arguments. E.g: +, -, *,
/ and expt are ok, but sqrt and log are not (in this context) You may want to read the introduction on numbers in R5RS and to take
notice of the distinction between exact and inexact numbers.
Best wishes, Jos Koot
----- Original Message -----
From: Scott Hickey
To: plt-scheme at list.cs.brown.edu
Sent: Monday, May 07, 2007 9:04 PM
Subject: [plt-scheme] Is plt-scheme appropriate for applications that usecurrency?
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.
Scott Hickey
_________________________________________________
For list-related administrative tasks:
http://list.cs.brown.edu/mailman/listinfo/plt-scheme
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070507/8790c878/attachment.html>