Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LISP - digits after decimal point

does anyone know how to specify the numer of digits after the decimal point for a float in Lisp?

Say if I print this command at the REPL:

CL-USER 3 > (format t "~,15f" (float (/ 1 7)))

I get:

0.142857150000000 

But the number is rounded at the 8th digit after the decimal point, I need to see a lot of digits after the decimal point in order to see if the number is cyclic and to calculate the period. (Actually I'm starting to try and solve Project Euler's problem 26).

I need to get something like this:

CL-USER 3 > (format t "~,15f" (float (/ 1 7)))
0.142857142857142857142857142857142857.... 

Thank you,

Luca

like image 904
Luca Avatar asked Aug 16 '12 09:08

Luca


2 Answers

Common Lisp does not have floats with arbitrary exactness in its standard.

Common Lisp defines four float types in the standard: SHORT-FLOAT, SINGLE-FLOAT, DOUBLE-FLOAT, LONG-FLOAT.

You can coerce a ratio to a float using the function COERCE (example in LispWorks):

CL-USER 1 > (coerce (/ 1 7) 'double-float)
0.14285714285714285D0

or as a LONG-FLOAT in CLISP

[1]> (coerce (/ 1 7) 'long-float)
0.14285714285714285714L0

To compute with longer float numbers you need extensions to Common Lisp. GNU CLISP has a non-portable extension and can set the number of (binary) digits:

(SETF (EXT:LONG-FLOAT-DIGITS) n)

Example:

[3]> (SETF (EXT:LONG-FLOAT-DIGITS) 1000)    
1000
[4]> (coerce (/ 1 7) 'long-float)
0.142857142857142857142857142857142857142857142857142857
142857142857142857142857142857142857142857142857142857
142857142857142857142857142857142857142857142857142857
142857142857142857142857142857142857142857142857142857
142857142857142857142857142857142857142857142857142857
142857142857142857142857142857142857143L0
like image 193
Rainer Joswig Avatar answered Sep 21 '22 10:09

Rainer Joswig


In addition to Rainer's excellent answer, I think you want to check out the function RATIONALIZE:

(rationalize (float 1/7))
1/7
like image 31
sds Avatar answered Sep 22 '22 10:09

sds