[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: converting decimal numbers into rational in scheme functions
From: |
David Kastrup |
Subject: |
Re: converting decimal numbers into rational in scheme functions |
Date: |
Wed, 18 Sep 2013 01:45:32 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) |
Janek Warchoł <address@hidden> writes:
> Hi,
>
> i have a function that takes a moment as the argument:
>
> foo =
> #(define-music-function (parser location mom)
> (ly:moment?)
> #{
> \override Score.SpacingSpanner.base-shortest-duration = #mom
> #})
>
> \new Staff {
> \foo #(ly:make-moment 1 50)
> c'4 d' e' f'
> }
>
> Now, i would like to be able to supply a decimal number and have it
> converted to a rational number (i.e. a fraction, which can be easily
> converted to a moment), so that i could call the function like this
>
> \foo #0.02 % 1/50 = 0.02
>
> Is there any smart way to do this (a built-in function? I've searched
> for one but haven't found any), or do i have to write a
> decimal->rational converter? Would anyone want to help with this?
How did you search? It's right in the GUILE manual where one would
expect, with rational numbers.
File: guile-1.8.info, Node: Reals and Rationals, Next: Complex Numbers,
Prev: Integers, Up: Numbers
5.5.2.3 Real and Rational Numbers
.................................
[...]
-- Scheme Procedure: rationalize x eps
-- C Function: scm_rationalize (x, eps)
Returns the _simplest_ rational number differing from X by no more
than EPS.
As required by R5RS, `rationalize' only returns an exact result
when both its arguments are exact. Thus, you might need to use
`inexact->exact' on the arguments.
(rationalize (inexact->exact 1.2) 1/100)
=> 6/5
--
David Kastrup