using floats

i know this is simple stuff, but for some reason i can’t get it.

i have an integer that i want to turn into a float.

int intValue2 = MBLIVE_TranslateFrom21Bit(&val2);

float decValue = intValue2;

DEBUG_MSG(“%d”, intValue2);

DEBUG_MSG(“%f”, decValue);

for sure intValue2 gives me the number i want. i read online that i can simply assign an int to a float and it’ll become a float, but my second DEBUG_MSG just outputs a blank line. i also tried using (float) before the int and that didn’t work either. any ideas?

thanks,

ultra

There is a simple answer: the simplified (s)printf function of MIOS32 doesn’t support %f

See also http://svnmios.midibox.org/filedetails.php?repname=svn.mios32&path=%2Ftrunk%2Fapps%2Ftutorials%2F003_debug_messages%2FREADME.txt

Workaround: write

DEBUG_MSG(“%d.%03d”, (int)floatValue, (int)(floatValue*1000) % 1000);

Best Regards, Thorsten.

thanks a lot tk, that works quite well. i’m also struggling to understand this:

my float is always a positive int (i guess that’s obvious since i was creating it using an int). for the correct value, the decimal should be shifted 3 places left. i multiply by .001, but i end up with .000, at least using what you gave me to output to mios terminal. is there something else i must do? i would think this would work in most cases, but is there something specific to the compiler regarding floats? too much OOP has spoiled me.

i have to work with ints in the first place and convert them back to .001 of the value because i’m bit shifting some values to 7 bits, sending over midi from ableton live, and reassembling them on the other side. i didn’t see a good way to just send a floating point value (doubt there is one) so i break up the values before and after the decimal and send them separately. so this is the part where i try to reassemble it on the other side.

ultra

It could be important to cast a value to the correct type before doing such operations.

A code snippet would help to understand what is going wrong.

On the other hand:

If your value is an Int anyhow, and you only want to reduce the resolution to get a 7bit value, why not using “intValue / 1000”?

Float operations are expensive (performance wise), but fixed point divisions (and multiplications) of integer values are fast since they are supported by the hardware.

Best Regards, Thorsten.

ok here’s a code snippet. i changed it to /1000 and directly set the first values, even though i’m getting them in a different way.

i have this:

int intValue1 = 1267;

int intValue2 = 474;

float decValue = intValue2;

float totalValue = (decValue / 1000);

DEBUG_MSG(“%d”, intValue1);

DEBUG_MSG(“%d”, intValue2);

DEBUG_MSG(“%d.%03d”, (int)totalValue, ((int)totalValue*1000) % 1000);

and end up with this:

00000021173501 ms | 1267

00000021173501 ms | 474

00000021173501 ms | 0.000

1267 and 474 are values i received from live, which started as 1267.474. in the end, i want my 474 to return to .474 so i can add the 1267 to it and get my original value back. i haven’t done the final adding operation yet because i want to get the .474 working first.

thanks,

ultra

also, to be more complete with information, not using / 1000 gives me this:

00000021437039 ms | 1267

00000021437039 ms | 474

00000021437039 ms | 474.000

so it’s sorta working!

I think you have to ‘cast’ the result of the calculation as otherwise the result will be considered int:

float totalValue = (float)(decValue / 1000);

Cheers

Phil

Yes! But to keep the float resolution, you should write:

float totalValue = (float)decValue / 1000;
[/code]




or better (I don't know if this really makes a difference, but I always add .0 to float constants to force the correct type):
[code]  
float totalValue = (float)decValue / 1000.0;  

Best Regards, Thorsten.

Actually thinking about it, you should cast decValue to float ‘first’. This ensures that the whole calculation uses floating point arithmetic and not integer.

float totalValue = (float)decValue / 1000;

Cheers

Phil

EDIT: TK beat me to it (again!)

yeah i don’t really get it. i tried:

(float)decValue / 1000

(float)decValue / 1000.0

(float)(decValue / 1000)

it all gives me .000

the number i’m working with right now is 84. i tried dividing by 10 to see what happens, and i get 8.0. so for some reason it’s rounding off or just removing the decimal.

The conversion in DEBUG_MSG is wrong.

Instead of:

DEBUG_MSG("%d.%03d", (int)totalValue, ((int)totalValue*1000) % 1000);
[/code]




write:
[code]  
DEBUG\_MSG("%d.%03d", (int)totalValue, (int)(totalValue\*1000.0) % 1000);  

Best Regards, Thorsten.