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!

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:Screenshot_20180328_040611.png
Screenshot_20180328_040611.png

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!

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

rubengjertsen

Hello,
I have another question about decimals. I have lists containing numbers like this:
(6.848613E4 1.0E6)
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 rconverttofloat (floatlist)
(loop for f in floatlist
collect (fround f 10 ))
;(omround f 10) ;makes no difference
)
The result with fround is:
(0.0 0.0)
And omround makes no difference. Is there a way to get (0.0006848613 0.000001)?
Best
Ruben

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.098098e4)
> “0.00050981”
Or if you can use ratios in your output:
(let* ((*readdefaultfloatformat* 'real)
(num (readfromstring (format nil "~A" 5.098098e4))))
num)

This reply was modified 10 months, 2 weeks ago by anders.

rubengjertsen

Thank you Anders,
That solves the float issue. I now almost have a function to convert a scoreline, but it needs to leave integers and strings alone:
(defun rscorelinetostringswithfloatformatting (floatlist)
(apply #’concatenate ‘string
;List of score strings:
(flat
(loop for f in floatlist
collect (let*(
(processstring
(if
(integerp f)
(writetostring f) ;create string
(if
(stringp f)
(writetostring f ) ;?? already a string
(format nil “~,8f” f) ;format float numbers
)))
);end of let declarations
(list processstring ” “)
)
)
)
)
)
This line contains some different types of information which would be in a score:
(i1 f 400 6.848613E4 “/Folder/hello.wav” 1.0E6)
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

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 (writetostring f ) with (format nil “~S” f).
Just a warning about your floats: depending on your usecase, relying on values beyond 68 decimals or more (ie. 0.00000100) is likely to suffer from roundoff errors, esp. when moving data between applications. In general it’s wise to try to normalize things.

This reply was modified 10 months, 2 weeks ago by anders.
