double to uint16_t

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
TeMa_1467596
Level 5
Level 5
5 sign-ins 5 likes given First like received

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?

0 Likes
1 Solution

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

View solution in original post

4 Replies
TeMa_1467596
Level 5
Level 5
5 sign-ins 5 likes given First like received

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'

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

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

GeonaP_26
Moderator
Moderator
Moderator
250 solutions authored 100 solutions authored 50 solutions authored

Please use volatile qualifier to prevent compiler optimizations.