Group Admins

  • Profile picture of Karim
  • Profile picture of Jean

OpenMusic

Public Group active 2 days, 22 hours ago

User group for OpenMusic and computer-aided composition. Visit the Forum for discussions.

Fraction to Decimal

Author 4 Subscribed Users |
Profile photo of nagymusic
nagymusic

Hello!
Is there a function that translates a list of fractions into a list of decimals (1/4 to 0.25) and vice versa? I know that one could construct a patch for this, but I thought it might be worth asking just in case.

Thank you!

March 28, 2018 at 02:41 #26046
Profile photo of Karim
Karim

Hi,

Certainly for the first question. Just use the float function.
Concerning the other one you can do for simple floats (not irrationals) – see screenshot
if you have for example pi (3.1415…) or 1/3 = 0.33333…. these will not render exactly as a ratio but will be approximated.

Best
K

Attachments:
  1. Screenshot_2018-03-28_04-06-11

    Screenshot_2018-03-28_04-06-11.png

  2. Screenshot_2018-03-28_04-06-11

    Screenshot_2018-03-28_04-06-11.png

March 28, 2018 at 03:19 #26047
Profile photo of nagymusic
nagymusic

Thanks, Karim. I had a chance to experiment with your suggestions (see the patch attached). The conversion from fractions to decimals works great (1/4 1/8 = 0.25 0.125). While the opposite produces the desired outcome, it’s still not computing the same values (0.25 0.125 = 1/4 3/25). My goal is to be able to go back and forth by keeping the same values, e.g, 0.25 0.125 = 1/4 1/8.

I welcome your thoughts. Thank you once again!

March 28, 2018 at 17:47 #26065
Profile photo of Karim
Karim

Oh sorry
should have explained more. 0.125 have 3 decimals so instead of 100 you should use 1000
and you will get 1/8
If the decimals are finite and not endless (like dividing by 3 or 7 etc…) you just have to calculate the number of decimals and multiply by 10

Best
K

March 28, 2018 at 19:21 #26068
Profile photo of nagymusic
nagymusic

Thanks! This is great.

March 30, 2018 at 16:57 #26084
Profile photo of rubengjertsen
rubengjertsen

Hello,

I have another question about decimals. I have lists containing numbers like this:

(6.848613E-4 1.0E-6)

They do not work well exported to a csound score, so I need to convert everything to plain decimals. Would there be a lisp function doing this? I’m trying this:

(defun r-convert-to-float (float-list)
(loop for f in float-list
collect (fround f 10 ))
;(om-round f 10) ;makes no difference
)

The result with fround is:
(0.0 0.0)
And om-round makes no difference. Is there a way to get (0.0006848613 0.000001)?

Best
Ruben

May 4, 2018 at 23:00 #26469
Profile photo of anders
anders

Hi Ruben. Floating point numbers is a big issue, they’re not ‘exact’ (as integers or ratios), but have a certain, limited precision.

But you can choose a useful printed representation in your output, perhaps using the ~F format directive? E.g:

(format "~,8f" 5.098098e-4)

-> “0.00050981”

Or if you can use ratios in your output:

(let* ((*read-default-float-format* 'real)
       (num (read-from-string (format nil "~A" 5.098098e-4))))
  num)
  • This reply was modified 7 months, 2 weeks ago by Profile photo of anders anders.
May 6, 2018 at 10:00 #26470
Profile photo of rubengjertsen
rubengjertsen

Thank you Anders,

That solves the float issue. I now almost have a function to convert a score-line, but it needs to leave integers and strings alone:

(defun r-scoreline-to-strings-with-float-formatting (float-list)
(apply #’concatenate ‘string
;List of score strings:
(flat
(loop for f in float-list
collect (let*(
(process-string
(if
(integerp f)
(write-to-string f) ;create string
(if
(stringp f)
(write-to-string f ) ;?? already a string
(format nil “~,8f” f) ;format float numbers
)))
);end of let declarations
(list process-string ” “)
)
)
)
)
)

This line contains some different types of information which would be in a score:
(i1 f 400 6.848613E-4 “/Folder/hello.wav” 1.0E-6)

It’s almost working, but I need to get rid of the \ in from of each “:
“i1 f 400 0.00068486 \”/Folder/hello.wav\” 0.00000100 ”

Best
Ruben

May 6, 2018 at 14:03 #26472
Profile photo of anders
anders

but I need to get rid of the \ in from of each “

If you use ~S to format the string, it will be output without escape characters, ie. change (write-to-string f ) with (format nil “~S” f).

Just a warning about your floats: depending on your use-case, relying on values beyond 6-8 decimals or more (ie. 0.00000100) is likely to suffer from round-off errors, esp. when moving data between applications. In general it’s wise to try to normalize things.

  • This reply was modified 7 months, 2 weeks ago by Profile photo of anders anders.
May 6, 2018 at 14:42 #26473

You must be logged in to reply to this topic.

Log in now