Floats and square roots

plougot

2012-06-13 01:16:46

Hi everyone !

After some researchs, i didn't find anything about floats and square roots in bome midi translator.
I found kind of a workaround for the lack of float (basically, *1000 at the beginning and /1000 at the end) but for sqrt, i'm a bit stuck.
i think i could use the Heron algorythm, which would give me some nice results, but since i don't have floats, ... And since it's not linear, i cannot just apply my silly workaround for floats....
Of course, i don't want to send a float in the end, but not having floats in the process gives me a lot of wrong results...

Any ideas ?

Thx

David

plougot

2012-06-14 16:39:36

It's ok, i managed to do it by myself.
If anyone is interested, i used the Heron method. Basically, i did the square root myself ^^

daisyemma

2012-06-15 05:19:45

Thank you so much.I was also still looking for floats and square roots in bome midi translator.
plougot wrote:It's ok, i managed to do it by myself.
If anyone is interested, i used the Heron method. Basically, i did the square root myself ^^

plougot

2012-06-15 15:59:38

Here's the code for doing a square root with the Heron Method :

Code: Select all

Label "ini"
oo = YOUR_X^2
xx=1
Label "1st row"
uu=oo/xx
uu=uu+xx
uu=uu/2
xx=uu
Label "2nd row"
uu=oo/xx
uu=uu+xx
uu=uu/2
xx=uu
Label "3rd row"
uu=oo/xx
uu=uu+xx
uu=uu/2
xx=uu
...
at the end, xx = sqrt(oo)

Each row is exactly the same. Repeat this until you have something like 10 rows or so, and you'll be sure to have a precise answer. Check in your log window. Depending on your numbers, you might need less rows.

If you want something more precise ("floats"), just multiply you X² by 100 for each float you want in your final result. For example, if i multiply my x² by 10 000, my x will be multiplied by 100, therefore i'll have two "floats".
DO NOT OVERDO IT, or you'll risk to go through the "ceiling", and have negative values which will ruin everything.

Also, do not multiply your X² with 10, 1000, ... or any odd number of zeros, or your result will be completely wrong.
example :
sqrt(9) = 3
sqrt(90) = 9.4868
sqrt(900) = 30

Et voilà !

PS : you can do a loop, so that you don't have to repeat the code manually, but i felt it was easier to explain it that way.