Skip to main content
Associate III
January 20, 2024
Question

Convert Integer to float

  • January 20, 2024
  • 2 replies
  • 5594 views

Hello everyone, can someone tell me why casting doesn't work. I want to convert int values of an array to float.

for (int i = 0; i<= adcBufferSize; i++)

{

adc_buffer_float[i] = (float)adc_buffer [i];

}

    2 replies

    Tesla DeLorean
    Guru
    January 20, 2024

    Show an example data set that's "Not Working"

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    SaxAuthor
    Associate III
    January 20, 2024

     

    Sax_1-1705741499741.png

     

    Tesla DeLorean
    Guru
    January 20, 2024

    Ok that seems to illustrate the input, it doesn't show the "not working" nor does it show or explain how the output is wrong or incorrect. 

    What's the expectation and how does that differ from reality?

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Associate II
    March 24, 2024

    I have the same problem. I read analog values with my ADC and values are type of uint16_t but when i convert my array of values that i get from ADC (Because of doublebuffering) to Float numbers get weird. Is it because of endians, maby overflows i dont know. I have it all on my work PC but i can get a screenshot tomorow.
    I have tried
    for(int n = 0;n < BuffSize;n++){
    FLOATARRAY[n] = (float) INTARRAY[n] * 1.0f; 
    }

     

    Tesla DeLorean
    Guru
    March 24, 2024

    You'll need to show the definitions, and datasets which are failing, to have any idea as to why the presented code fragment is failing.

    I doubt it's got anything to do with endian ordering or overflow, as a float should have no trouble representing a number in the 0-65535 range. Where you'll get issues is 8 or more digits.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Associate II
    March 25, 2024

    I discoverd my problems nest. Its not in typecasting it is in declaration. I have few arrays named 

    uint16_t ADC_BUFFER_RAW [ADC_BUFFER_LENGHT];

    float ADC_BUFFER [ADC_BUFFER_LENGHT];

    float FFT_BUFFER [FFT_BUFFER_LENGHT];

    float MAGNITUDE_BUFFER[FFT_BUFFER_LENGHT/2];

    If i define FFT_BUFFER_LENGHT to be more than size of 31,

    it corupts my values found in ADC_BUFFER. I have no idea why.

     

    My .map file shows that registers are not overlaping:


    .bss.fft_instance
    0x00000000200002e4 0x18 ./Core/Src/main.o
    0x00000000200002e4 fft_instance
    .bss.ADC_BUFFER_RAW
    0x00000000200002fc 0x80 ./Core/Src/main.o
    0x00000000200002fc ADC_BUFFER_RAW
    .bss.ADC_BUFFER
    0x000000002000037c 0x100 ./Core/Src/main.o
    0x000000002000037c ADC_BUFFER
    .bss.FFT_BUFFER
    0x000000002000047c 0x40 ./Core/Src/main.o
    0x000000002000047c FFT_BUFFER
    .bss.MAGNITUDE_BUFFER
    0x00000000200004bc 0x20 ./Core/Src/main.o
    0x00000000200004bc MAGNITUDE_BUFFER
    .bss.ADC_CONVERSION_COMPLETE
    0x00000000200004dc 0x1 ./Core/Src/main.o

    (TEXT is slightly diferent as HTML does not support original text)