4 Replies Latest reply on Feb 14, 2019 8:00 AM by temac_1467596

    double to uint16_t

    temac_1467596

      I know this is a casting problem but I can't get it working using GCC.

       

      I have defined

      #define oPwrF  3

       

      I have a variable that's part of an array

      double offsetFactor[4];

       

      and I assign a value of -90.0 to it as follows

      offsetFactor[oPwrF] = -90;

       

      but when I tried to use it in a calculation, it was giving a value of zero so I separated it out and tried to change it into a uint16_t...

      tempInt16 = offsetFactor[oPwrF];

       

      where tempInt16 is declared as

      int16_t tempInt16 = 0;

       

      When I run all this, the value of tempInt is 0

       

      The original non-working line was...

      transHeader.pwrFactorLatest =  ((xchngArray[m4_rawPwrFactor] + offsetFactor[oPwrF]) * -1);

       

      where xchngArray[] is an int16_t and

      and transHeader.pwrFactorLatest is an int16_t

       

      I tied casting the offsetFactor[oPwrF] as (uint16_T)offsetFactor[oPwrF] but that doesn't work in any of the above examples, I always get zero.

       

      if I write...

      transHeader.pwrFactorLatest =  ((xchngArray[m4_rawPwrFactor] - 90) * -1);

       

      I get my expected results but the user is able to change the value -90 so this isn't a real solution.

       

      When I looked up the definition of uint16_t, it open _stdint.h and there's a red X at line 12 next to #include <machine/_default_types.h> and when I hover over that X is says..

      machine/_default_types.h file not found

       

      Any ideas what's wromg?

        • 1. Re: double to uint16_t
          temac_1467596

          I have realized that I had doubles used in various places in my code and they were working and now they all produce bad results, zero or 'nan'

          1 of 1 people found this helpful
          • 2. Re: double to uint16_t
            GeonaM_26

            Please use volatile qualifier to prevent compiler optimizations.

            1 of 1 people found this helpful
            • 3. Re: double to uint16_t
              JoMe_264151

              Set the optimization level to "none" which will prevent the compiler from optimizing variables into registers.

              You find that option under Project->Build settings->ARM...->Compiler->Optimization..

               

              Bob

              1 of 1 people found this helpful
              • 4. Re: double to uint16_t
                temac_1467596

                Guys,

                 

                Thanks for all the suggestions, in the end it was really simple.  I was saving the values to flash and then reading them back.

                 

                The issue was that I messed up and tried to read the values from a different address than I saved them to so the values being read in were read from flash full of 0xFF’s  That’s what’s causing the 'nan' issue because the values were not numbers.

                 

                If fact the compiler handles the following exactly as I expected, even though it's a mixture of 3 or 4 different data types.

                 

                transHeader.pwrFactorLatest =  ((xchngArray[m4_rawPwrFactor] + offsetFactor[oPwrF]) * -1);

                 

                Thanks for all the suggestions.

                 

                Sorry for crying wolf!

                 

                Ted

                1 of 1 people found this helpful