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!
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!
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
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!
fractions-decimals.omp (6.35 KB)
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
Thanks! This is great.
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
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)
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
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.