Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Financial datatype for Lisp?

44 views
Skip to first unread message

GP lisper

unread,
Aug 15, 2005, 6:55:23 AM8/15/05
to
Long ago, I used BCD datatypes for adding up nickels and computing
sales tax. Using floats to add nickels doesn't really work out, maybe
working in rationals might however. Working in pennies via integers
might also be done. But is there a simple and convenient method of
working with dollars and cents?


--
TIA

Harald Hanche-Olsen

unread,
Aug 15, 2005, 7:17:10 AM8/15/05
to
+ GP lisper <spam...@CloudDancer.com>:

One simple (simple minded?) method would be to convert dollars and
cents to cents on reading, then printing with a suitable format.

CL-USER> (round 1234.56 0.01)
123456
8.6188316e-5
CL-USER> (format t "~10,2,-2F" *)
1234.56

You may wish to (setf *read-default-float-format* 'double-float) in
order to avoid trouble on reading big amounts.

--
* Harald Hanche-Olsen <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
than thinking does: but it deprives us of whatever chance there is
of getting closer to the truth. -- C.P. Snow

Jason Kantz

unread,
Aug 15, 2005, 9:37:05 AM8/15/05
to
Are you looking for something like fixed point math?

(defmacro fix<-int (x)
`(ash ,x 8))

(defmacro fix<-float (x)
`(floor (* ,x 256)))

(defmacro int<-fix (x)
`(ash ,x -8))

(defmacro float<-fix (x)
`(coerce (/ ,x 256) 'float))

(defmacro fix* (x y)
`(ash (* ,x ,y) -8))

(defmacro fix/ (x y)
`(/ (ash ,x 8) ,y))

--
Jason Kantz
(reverse "moc.ztnak@nosaj")

Marco Antoniotti

unread,
Aug 15, 2005, 11:57:04 AM8/15/05
to
TRT is to implement a Decimal Number datatype, e.g. following the specs at

http://www2.hursley.ibm.com/decimal/

Of course we'll have a #D reader macro to denote decimal numbers.

Cheers
--
Marco

GP lisper

unread,
Aug 15, 2005, 5:02:38 PM8/15/05
to
> GP lisper wrote:
>> Long ago, I used BCD datatypes for adding up nickels and computing
>> sales tax. Using floats to add nickels doesn't really work out, maybe
>> working in rationals might however. Working in pennies via integers
>> might also be done. But is there a simple and convenient method of
>> working with dollars and cents?
>>

On Mon, 15 Aug 2005 11:57:04 -0400, <mar...@cs.nyu.edu> wrote:
>
> TRT is to implement a Decimal Number datatype, e.g. following the specs at
> http://www2.hursley.ibm.com/decimal/
>
> Of course we'll have a #D reader macro to denote decimal numbers.

TRT? What kind of schedule is there?

Is the IBM "reference implementation" usable in your opinon? Maybe I
can throw a FFI wrapper around it (immediate need here)? ...or ECL...


--
On a cloudy day,
You hear the cons cells whisper:
"We are lost and gone." -- Oliver

Harald Hanche-Olsen

unread,
Aug 15, 2005, 6:55:14 PM8/15/05
to
+ GP lisper <spam...@CloudDancer.com>:

| On Mon, 15 Aug 2005 11:57:04 -0400, <mar...@cs.nyu.edu> wrote:
| >

| > TRT is to implement a Decimal Number datatype [...]


|
| TRT? What kind of schedule is there?

The Right Thing, presumably.
There is only one schedule for The Right Thing:
WIR (When It's Ready).

GP lisper

unread,
Aug 15, 2005, 7:37:20 PM8/15/05
to
On 16 Aug 2005 00:55:14 +0200, <han...@math.ntnu.no> wrote:
>
>
> + GP lisper <spam...@CloudDancer.com>:
>
>| On Mon, 15 Aug 2005 11:57:04 -0400, <mar...@cs.nyu.edu> wrote:
>| >
>| > TRT is to implement a Decimal Number datatype [...]
>|
>| TRT? What kind of schedule is there?
>
> The Right Thing, presumably.
> There is only one schedule for The Right Thing:
> WIR (When It's Ready).
>
True, but you left out:

"Of course we'll have a #D reader macro to denote decimal numbers."

Marco Antoniotti

unread,
Aug 16, 2005, 10:27:57 AM8/16/05
to

GP lisper wrote:
>>GP lisper wrote:
>>
>>>Long ago, I used BCD datatypes for adding up nickels and computing
>>>sales tax. Using floats to add nickels doesn't really work out, maybe
>>>working in rationals might however. Working in pennies via integers
>>>might also be done. But is there a simple and convenient method of
>>>working with dollars and cents?
>>>
>
>
> On Mon, 15 Aug 2005 11:57:04 -0400, <mar...@cs.nyu.edu> wrote:
>
>>TRT is to implement a Decimal Number datatype, e.g. following the specs at
>>http://www2.hursley.ibm.com/decimal/
>>
>>Of course we'll have a #D reader macro to denote decimal numbers.
>
>
> TRT? What kind of schedule is there?

??? TRT is "The Right Thing". Common Lisp is all about TRT :)


>
> Is the IBM "reference implementation" usable in your opinon? Maybe I
> can throw a FFI wrapper around it (immediate need here)? ...or ECL...
>

It seems to works from C. Wrapping it up may be feasable, I have not
thought about it. But I really like the idea of having #D3.14 denote
decimal numbers.

Apart from that, the IBM pages are instructive. Essentially the library
is provided as a "push" to have decimal arithmetic in HW (which, if I am
not mistaken, IBM mainframes already have)

Cheers
--
Marco


Jason Kantz

unread,
Aug 16, 2005, 5:03:58 PM8/16/05
to
I like idea of #D too. I've made a cliki page for this topic ...

http://www.cliki.net/DECIMAL-NUMBER

in case anyone wants to pick this one up and run with it.

GP lisper

unread,
Aug 16, 2005, 5:00:29 PM8/16/05
to
On Tue, 16 Aug 2005 10:27:57 -0400, <mar...@cs.nyu.edu> wrote:
> GP lisper wrote:
>> On Mon, 15 Aug 2005 11:57:04 -0400, <mar...@cs.nyu.edu> wrote:
>>>GP lisper wrote:
>>>
>>>>Long ago, I used BCD datatypes for adding up nickels and computing
>>>>sales tax. Using floats to add nickels doesn't really work
>>
>>>TRT is to implement a Decimal Number datatype, e.g. following the specs at
>>>http://www2.hursley.ibm.com/decimal/
>>>
> ??? TRT is "The Right Thing". Common Lisp is all about TRT :)

Sometimes. ROUND utilizes bankers rounding, so there was some
awareness of decimal needs in the past. I wonder what Kent Pitman has
to say about 'decimal numbers' during the standardization efforts.
Maybe the ivory tower snobbishness about money left this hole in CL.


>>>Of course we'll have a #D reader macro to denote decimal numbers.
>>

>> Is the IBM "reference implementation" usable in your opinon? Maybe I
>> can throw a FFI wrapper around it (immediate need here)? ...or ECL...
>>
> It seems to works from C. Wrapping it up may be feasable, I have not
> thought about it. But I really like the idea of having #D3.14 denote
> decimal numbers.

Sounds great to me, more important than unicode in fact. But unicode
is an easier problem. Adding a new numeric type into Common Lisp
doesn't seem easy, libraries (etc) just look like an ugly hack. Seems
like something a commerical lisp will need to offer, or maybe one of
the new CL efforts will pick it up to attract users.


> Apart from that, the IBM pages are instructive. Essentially the library
> is provided as a "push" to have decimal arithmetic in HW (which, if I am
> not mistaken, IBM mainframes already have)

BCD math was in the 8080/Z80 microprocessors (see DAA opcode), I
remember it in a few CP/M compilers. So it might still be around in
x86, and maybe it works (always a risk with INTEL).

George Neuner

unread,
Aug 18, 2005, 4:08:28 AM8/18/05
to
On Tue, 16 Aug 2005 14:00:29 -0700, GP lisper
<spam...@CloudDancer.com> wrote:


>BCD math was in the 8080/Z80 microprocessors (see DAA opcode), I
>remember it in a few CP/M compilers. So it might still be around in
>x86, and maybe it works (always a risk with INTEL).


IA-32 processors have ALU support for arithmetic on unpacked (byte per
digit) BCD, and add/subtract on packed BCD. it also has FPU support
for converting floating point from/to an 80-bit, 18-digit packed BCD
format.

IA-64 dropped ALU support for BCD. AFAICT, the FPU support remains.

George
--
for email reply remove "/" from address

GP lisper

unread,
Aug 24, 2005, 2:13:34 AM8/24/05
to


--
Program A uses CLOS, Program B is implemented with structs, leading
to a fourfold increase in execution speed. --J. B. Heimatseiten

0 new messages