Converting from integer to fractional with code?


#1

Hello :slight_smile:

I am trying to convert an integer value to a fractional value. When I patch the below with factory objects it works as supposed, but when I try to do it with code it doesnt really work:

The top calculation with a integer div 16 followed by a fractional div 32 gives the right value.. But when I try to code it.. Making all the calculations in one object, dividing by 512(>>9) and using and integer input and and fractional output it does not work. I just get no output at all. It should output 7.81, like the top calculation... So I thinking I need to convert it in some way. I also looked inside the conv/to f to see what is going on, but it seems like it is the inlets/outlets doing the conversion, not by code.

Any idea how to convert the integer value to fractional?

Thanks, Jaffa


#2

in frac32 the dial displays 64.0 for 1<<27;
so i would say,
int -> frac:

 outlet_frac32 = inlet_int << (27 - 6);   // 64 = 2^6

frac to int

 outlet_int32 = inlet_frac32 >> (27 - 6);

#3

Thanks I just tried this out, with green inlet and blue out:
outlet_f = inlet_i << (27 - 6); // 64 = 2^6

And that didnt work. It gave me a result of -96. Guess I need to try some different ways of doing it.


#4

Maybe a sign bit issue...

outlet_f = ((int32_t) inlet_i) << (27 - 6);

#5

Still -96 wit that math.


#6

That seems to work for me.
I've coded that :

Help -> Library -> community -> tiar -> conv -> i_to_f


#7

Dont work here.. Anyway, maybe it is another type of conversion I need to do. I googled a bit but dont find much. Mostly about converting from integer to floats in c++ not so much about integer to fractional.

But thank for the effort :wink:

Ohh if you want to check the exact math I am trying to do then feed the input with integer value of 4000. And then when converted from I to F and divided by 512 it should be 7,8125.

Ahhh I think I have an idea now... It has to be done in 2 steps, like when patching with factory objects...


#8

What's the value of the integer ?

frac32 is limited in range. q27 with 4 bits and a sign bit (total 32bits).
So, above 64*16=1024, the values will go modulo.


#9

See above I just edited my post.


#10

BUt Yeah... I think it has to be done in 2 step... FIRST divided by 16 when integer and THEN divide by 32 in fractional.. Like when patched the factory object.... I guess the integer number has to be within the range of 1024. So first divide the integer into something meaningul below 1024 and then divide again... I think that will work.


#11

I tried this... I get the right number at the first output, the outlet_i 4000/16 = 250

But when I do the next step 250/32 it just doesnt work.