lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: guile-question: 3/2 -> "1 1/2"


From: David Kastrup
Subject: Re: guile-question: 3/2 -> "1 1/2"
Date: Tue, 11 Aug 2015 08:19:07 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Thomas Morley <address@hidden> writes:

> 2015-08-10 19:02 GMT+02:00 Thomas Morley <address@hidden>:
>> Hi all,
>>
>> I want to put out 3/2 not as 1.5 but "1½" or "1 1/2"
>> Is there a predefined procedure in guile or do I need to define my own?
>>
>> Thanks,
>>   Harm
>
> It's a matter of "exactness".
> And when #e comes into the game the guile-manual is not verbose about it.
> To say the least.
>
> In the end I come up with the below. I decided to return a ist, not a string.
>
> But it feels pretty clumsy, noone with something better?
>
>
> (define (integer-and-fraction nmbr)
>   "Return a list with an integer and a fraction build from an exact number
> Example: 47/7 -> (6 5/7) "
>   (let*  ((i (inexact->exact (truncate nmbr)))
>           (rest (string->number (format #f "~a~a" "#e" (- nmbr i)))))
>           ;; return a string
>           ;(format #f "~a~a"
>           ;  i
>           ;  (if (= rest 0)
>           ;      ""
>           ;      (string-append " " (number->string rest))))
>           ;; return a list
>           (list i rest)
>                 ))

Well, how about something like

(define (integer-and-fraction nmbr)
  (let* ((wh (truncate nmbr)) (rem (- nmbr wh)))
    (if (or (zero? wh) (zero? rem)) (format #f "~s" nmbr)
      (format #f "~s ~s" wh (abs rem)))))

(display (integer-and-fraction (/ 1111 7)))

Of course the disadvantage being that you cannot distinguish between one
and two numbers and cannot read the number back in easily.

-- 
David Kastrup



reply via email to

[Prev in Thread] Current Thread [Next in Thread]