From: GP lisper
Subject: Financial datatype for Lisp?
Date: 
Message-ID: <1124103604.b7aa3610c3e8d8251a585c19ae76c3cd@teranews>
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

From: Jason Kantz
Subject: Re: Financial datatype for Lisp?
Date: 
Message-ID: <1124113025.333941.119330@g47g2000cwa.googlegroups.com>
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 ··········@nosaj")
From: Marco Antoniotti
Subject: Re: Financial datatype for Lisp?
Date: 
Message-ID: <kX2Me.33$DJ5.68418@typhoon.nyu.edu>
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 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?
> 
> 
From: GP lisper
Subject: Re: Financial datatype for Lisp?
Date: 
Message-ID: <1124140335.ec9c5c1de70429921b41b3de239ef18a@teranews>
> 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, <·······@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
From: Harald Hanche-Olsen
Subject: Re: Financial datatype for Lisp?
Date: 
Message-ID: <pcou0hqize5.fsf@shuttle.math.ntnu.no>
+ GP lisper <········@CloudDancer.com>:

| On Mon, 15 Aug 2005 11:57:04 -0400, <·······@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).

-- 
* 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
From: GP lisper
Subject: Re: Financial datatype for Lisp?
Date: 
Message-ID: <1124149683.4fbf0b083a72f7d655851cd918eba589@teranews>
On 16 Aug 2005 00:55:14 +0200, <······@math.ntnu.no> wrote:
>
>
> + GP lisper <········@CloudDancer.com>:
>
>| On Mon, 15 Aug 2005 11:57:04 -0400, <·······@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."


-- 
On a cloudy day,
You hear the cons cells whisper:
"We are lost and gone."  -- Oliver
From: Marco Antoniotti
Subject: Re: Financial datatype for Lisp?
Date: 
Message-ID: <OJmMe.37$DJ5.68596@typhoon.nyu.edu>
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, <·······@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
From: GP lisper
Subject: Re: Financial datatype for Lisp?
Date: 
Message-ID: <1124226726.2619ffce7ebdf59154566cea0ed28ae5@teranews>
On Tue, 16 Aug 2005 10:27:57 -0400, <·······@cs.nyu.edu> wrote:
> GP lisper wrote:
>> On Mon, 15 Aug 2005 11:57:04 -0400, <·······@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).


-- 
On a cloudy day,
You hear the cons cells whisper:
"We are lost and gone."  -- Oliver
From: George Neuner
Subject: Re: Financial datatype for Lisp?
Date: 
Message-ID: <ane8g15po586u7jq66gl6cnvaqqq6jk3j0@4ax.com>
On Tue, 16 Aug 2005 14:00:29 -0700, GP lisper
<········@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
From: GP lisper
Subject: Re: Financial datatype for Lisp?
Date: 
Message-ID: <1124864646.95fb889d88a177e458bd272640084f23@teranews>
On Tue, 16 Aug 2005 14:00:29 -0700, <········@CloudDancer.com> wrote:
>
>
> On Tue, 16 Aug 2005 10:27:57 -0400, <·······@cs.nyu.edu> wrote:
>> GP lisper wrote:
>>> On Mon, 15 Aug 2005 11:57:04 -0400, <·······@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).
>
>


-- 
Program A uses CLOS, Program B is implemented with structs, leading
to a fourfold increase in execution speed.  --J. B. Heimatseiten
From: Jason Kantz
Subject: Re: Financial datatype for Lisp?
Date: 
Message-ID: <1124226238.185254.12030@g49g2000cwa.googlegroups.com>
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.
From: Harald Hanche-Olsen
Subject: Re: Financial datatype for Lisp?
Date: 
Message-ID: <pco7jenlaa1.fsf@shuttle.math.ntnu.no>
+ GP lisper <········@CloudDancer.com>:

| 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?

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