From: Ray Dillinger
Subject: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <48c4142f$0$17220$742ec2ed@news.sonic.net>
Does anyone know of a lisp dialect with exact decimal fractions 
that aren't bignums?

By which I mean, you get exactly correct answers for exact 
operations on decimal fractions as long as the answers don't have 
to exceed some known (settable?) threshold of precision, and you 
*don't* get a consumes-all-memory error when answers (to operations 
such as repeated multiplication by an interest factor) do exceed 
that threshold of precision?

Yes, I'm thinking of the semantics some of you may remember from 
COBOL.  It's a translation target.  Having the numbers behave 
like COBOL numbers is crucial to the correctness of the translated
code.  If it doesn't exist, then I'll have to roll my own. 

                                Bear

From: Tim Bradshaw
Subject: Re: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <7c81b185-bd97-42a8-8657-5f83174edae7@d1g2000hsg.googlegroups.com>
On Sep 7, 6:47 pm, Ray Dillinger <····@sonic.net> wrote:

> By which I mean, you get exactly correct answers for exact
> operations on decimal fractions as long as the answers don't have
> to exceed some known (settable?) threshold of precision, and you
> *don't* get a consumes-all-memory error when answers (to operations
> such as repeated multiplication by an interest factor) do exceed
> that threshold of precision?

CLISP has settable float precision, I think.  But I suspect it is bits
rather than decimal digits.
From: GP lisper
Subject: Re: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <slrngc8ei7.4t1.spambait@phoenix.clouddancer.com>
On Sun, 07 Sep 2008 10:47:31 -0700, <····@sonic.net> wrote:
>
> like COBOL numbers is crucial to the correctness of the translated
> code.  If it doesn't exist, then I'll have to roll my own. 


http://speleotrove.com/decimal/

CMUCL was extended for double double about a year ago, that might be a
roadmap for the above.

-- 
One of the strokes of genius from McCarthy
was making lists the center of the language - kt
** Posted from http://www.teranews.com **
From: Scott Burson
Subject: Re: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <518a61cd-f037-4d97-9381-3a386238a5ef@i24g2000prf.googlegroups.com>
On Sep 7, 10:47 am, Ray Dillinger <····@sonic.net> wrote:
> Does anyone know of a lisp dialect with exact decimal fractions
> that aren't bignums?
>
> If it doesn't exist, then I'll have to roll my own.

Here's what I would do:

(defun round-to-digits (num digits)
  (let ((frac (expt 10 (- digits))))
    (* (round num frac) frac)))

Watch what happens when I use this on _rational_ numbers:

* 22345/66789   ; just some number
545/1629
* (float *)
.33456108    ; just so you can see what the value is
* (round-to-digits 545/1629 2)
33/100
* (round-to-digits 545/1629 3)
67/200       ; = .335
* (round-to-digits 545/1629 4)
1673/5000    ; = .3346

So by keeping the numbers in rational form, and applying ROUND-TO-
DIGITS as needed, you can have whatever precision you want.

How this compares performance-wise with other approaches I don't know,
but it might not be too bad, in a well-implemented CL.

-- Scott
From: Scott Burson
Subject: Re: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <6248c1ec-b73a-422f-a2c7-3190533724dc@s9g2000prg.googlegroups.com>
On Sep 7, 2:50 pm, Scott Burson <········@gmail.com> wrote:
>
> Here's what I would do:
>
> (defun round-to-digits (num digits)
>   (let ((frac (expt 10 (- digits))))
>     (* (round num frac) frac)))
>
> Watch what happens when I use this on _rational_ numbers:

Just to clarify, it's not the fact that you supply a rational argument
for NUM that causes you to get a rational result; it's the fact that
the EXPT call returns a ratio.  So you can call ROUND-TO-DIGITS on a
float, as well, and you'll still get a rational result.

If performance is an issue, you might want to cache the EXPT results
in a hash table or something.

-- Scott
From: Ray Dillinger
Subject: Re: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <48c4a330$0$17208$742ec2ed@news.sonic.net>
Scott Burson wrote:

> On Sep 7, 10:47 am, Ray Dillinger <····@sonic.net> wrote:
>> Does anyone know of a lisp dialect with exact decimal fractions
>> that aren't bignums?

>> If it doesn't exist, then I'll have to roll my own.

> Here's what I would do:

> (defun round-to-digits (num digits)
>   (let ((frac (expt 10 (- digits))))
>     (* (round num frac) frac)))

<clip>

> * (round-to-digits 545/1629 4)
> 1673/5000    ; = .3346
> 
> So by keeping the numbers in rational form, and applying ROUND-TO-
> DIGITS as needed, you can have whatever precision you want.

Yeah, that's looking a lot like what I'm going to have to do here.
Thanks for a nice concise expression of it. 

Of course, that's  not the whole of it...  I'm going to have to roll 
a bunch of basic math functions around it that keep track of roundoffs. 
That'll be tedious, but it's just syntax. 
 
                                Bear
From: leppie
Subject: Re: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <d159967c-81d6-4079-95ca-3105cb36c613@t54g2000hsg.googlegroups.com>
On Sep 7, 11:50 pm, Scott Burson <········@gmail.com> wrote:
> On Sep 7, 10:47 am, Ray Dillinger <····@sonic.net> wrote:
>
> > Does anyone know of a lisp dialect with exact decimal fractions
> > that aren't bignums?
>
> > If it doesn't exist, then I'll have to roll my own.
>
> Here's what I would do:
>
> (defun round-to-digits (num digits)
>   (let ((frac (expt 10 (- digits))))
>     (* (round num frac) frac)))
>
> Watch what happens when I use this on _rational_ numbers:
>
> * 22345/66789   ; just some number
> 545/1629
> * (float *)
> .33456108    ; just so you can see what the value is
> * (round-to-digits 545/1629 2)
> 33/100
> * (round-to-digits 545/1629 3)
> 67/200       ; = .335
> * (round-to-digits 545/1629 4)
> 1673/5000    ; = .3346
>
> So by keeping the numbers in rational form, and applying ROUND-TO-
> DIGITS as needed, you can have whatever precision you want.
>
> How this compares performance-wise with other approaches I don't know,
> but it might not be too bad, in a well-implemented CL.
>
> -- Scott

Why not use the builtin rationalize procedure?

Cheers

leppie
From: Ray Dillinger
Subject: Re: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <48c52969$0$17202$742ec2ed@news.sonic.net>
leppie wrote:

>> On Sep 7, 10:47 am, Ray Dillinger <····@sonic.net> wrote:
>>
>> > Does anyone know of a lisp dialect with exact decimal fractions
>> > that aren't bignums?
>>
>> > If it doesn't exist, then I'll have to roll my own.

> Why not use the builtin rationalize procedure?

Powers of ten aren't special to it.  I need a function to which 
powers of ten are special. 

                                Bear
From: Pascal J. Bourguignon
Subject: Re: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <7c3akak76z.fsf@pbourguignon.anevia.com>
Ray Dillinger <····@sonic.net> writes:

> leppie wrote:
>
>>> On Sep 7, 10:47 am, Ray Dillinger <····@sonic.net> wrote:
>>>
>>> > Does anyone know of a lisp dialect with exact decimal fractions
>>> > that aren't bignums?
>>>
>>> > If it doesn't exist, then I'll have to roll my own.
>
>> Why not use the builtin rationalize procedure?
>
> Powers of ten aren't special to it.  I need a function to which 
> powers of ten are special. 

Well I guess you use it for money, mainly.
There are several packages to deal with money.

Have a look at mine, in there:
http://darcs.informatimago.com/lisp/common-lisp/invoice.lisp
(I should make it an independant package).

;;;---------------------------------------------------------------------
;;; Monetary Amounts & Currency Syntax
;;;---------------------------------------------------------------------
;;
;; Since floating point arithmetic is not adapted to accounting,
;; we will use integers for monetary amounts, and
;; percentages will be expressed as rationnals: 16 % = 16/100
;;
;; 123.45 � = 12345 � = #978m123.45
;;
;; In addition monetary amounts are tagged with a currency, and
;; arithmetic operations are type-restricted.
;;
;; The reader syntax is: # [currency-code] m|M [+|-] digit+ [ . digit* ]
;; The currency code must be a numeric code of a currency found 
;; in (com.informatimago.common-lisp.iso4217:get-currencies), 
;; otherwise a read-time error is issued.
;; When the currency-code is not present, the currency designated 
;; by *DEFAULT-CURRENCY* is used.
;; The number of digits after the decimal point must not be superior 
;; to the minor unit attribute of the currency.
;; The value is converted to an integer number of minor unit and
;; the read result is an AMOUNT structure gathering the currency 
;; and the value.
;;
;; The operations defined on AMOUNT values are:
;; 
;; c: amount* --> boolean
;; with c in { <, <=, >, >=, =, /= }.
;;
;; +: amount* --> amount
;; -: amount* --> amount
;; *: amount X real* --> amount (commutative and associative)
;; /: amount X real* --> amount (not commutative and not associative)
;;
;; For now, all these operations work only when the currency of all amount
;; involved is the same.
;;
;; These Common-Lisp operators are shadowed, and functions are defined for 
;; them, that extend the normal numeric functions for amounts.
;;
;; The AMOUNT structure has a printer that prints different format
;; depending on the *PRINT-READABLY*. It uses the reader syntax defined 
;; above when *PRINT-READABLY* is true, or a "~V$ ~3A" format printing 
;; the value followed by the alphabetic code of the currency.

-- 
__Pascal Bourguignon__
From: George Neuner
Subject: Re: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <43b8c4pbgo93ou0jch14il66uc8bfnmbdb@4ax.com>
On Sun, 07 Sep 2008 10:47:31 -0700, Ray Dillinger <····@sonic.net>
wrote:

>
>Does anyone know of a lisp dialect with exact decimal fractions 
>that aren't bignums?
>
>By which I mean, you get exactly correct answers for exact 
>operations on decimal fractions as long as the answers don't have 
>to exceed some known (settable?) threshold of precision, and you 
>*don't* get a consumes-all-memory error when answers (to operations 
>such as repeated multiplication by an interest factor) do exceed 
>that threshold of precision?
>
>Yes, I'm thinking of the semantics some of you may remember from 
>COBOL.  It's a translation target.  Having the numbers behave 
>like COBOL numbers is crucial to the correctness of the translated
>code.  If it doesn't exist, then I'll have to roll my own. 
>
>                                Bear

I recall a version of XLisp (a Scheme despite the name) that had fixed
point fractions.  Might have been 16-bit though - it was that long
ago.  I no longer have a copy of it.

Have you checked for fixed point libraries?  There are some C libs
available if there are none in Lisp.

George
From: Kaz Kylheku
Subject: Re: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <20080908121128.154@gmail.com>
["Followup-To:" header set to comp.lang.lisp.]
On 2008-09-07, Ray Dillinger <····@sonic.net> wrote:
>
> Does anyone know of a lisp dialect with exact decimal fractions 
> that aren't bignums?

I.e. decimal floating-point.

> Yes, I'm thinking of the semantics some of you may remember from 
> COBOL. 

I remember it from any pocket calculator. :)
From: Brian
Subject: Re: Lisps with decimal fractions that aren't bignums?
Date: 
Message-ID: <a479aadb-0a29-441d-a7c0-c55ee454373c@m45g2000hsb.googlegroups.com>
On Sep 8, 2:16 pm, Kaz Kylheku <········@gmail.com> wrote:
> ["Followup-To:" header set to comp.lang.lisp.]
> On 2008-09-07, Ray Dillinger <····@sonic.net> wrote:
>
> > Does anyone know of a lisp dialect with exact decimal fractions
> > that aren't bignums?
>
> I.e. decimal floating-point.
Now if you could find a Common Lisp implementation in which FLOAT-
RADIX of various floating point numbers returned 10...