Forums

(1.3) - (int)(1.3) is 0.2 in IAR C?

Started by jamesmichaelnewton July 28, 2010
#define version 1.3
...
typedef unsigned char byte;
...
volatile byte SPIBuf[10];
...
SPIBuf[5]= (int)(version)+'0';
SPIBuf[6]= (int)((version-(int)version)*10)+'0';
...

And SPIBuf[6] is '2'?

Yet when version was 1.2, SPIBuf[6] was 2. 1.4 gave us 3.

C project in IAR V4.11E/W32 [Baseline] (4.11.5.7) yes, I'm sure that's not the latest IAR, but I can't afford to restart my support contract at the moment.

Am I misusing (int)?

We changed to two separate defines, majorver, minorver but this bothers me...

Beginning Microcontrollers with the MSP430

I do not use floating point. But I love floating point.

I do not write programs in c. But I love c programmers.

I did not know that c compiler can do floating point at compilation time. And I was amazed.

Do you know that 1.3 in floating point is about 1.29999995?

--- In m..., "jamesmichaelnewton" wrote:
>
> #define version 1.3
> ...
> typedef unsigned char byte;
> ...
> volatile byte SPIBuf[10];
> ...
> SPIBuf[5]= (int)(version)+'0';
> SPIBuf[6]= (int)((version-(int)version)*10)+'0';
> ...
>
> And SPIBuf[6] is '2'?
>
> Yet when version was 1.2, SPIBuf[6] was 2. 1.4 gave us 3.
>
> C project in IAR V4.11E/W32 [Baseline] (4.11.5.7) yes, I'm sure that's not the latest IAR, but I can't afford to restart my support contract at the moment.
>
> Am I misusing (int)?
>
> We changed to two separate defines, majorver, minorver but this bothers me...
>

LOL

You Sir, are wise... and kind to young fools...

The lesson you are trying to teach me is that I could have corrected the error by simply adding 0.05 to the value before taking the (int) right?

Just kidding.

I'll avoid doing floating point in the compiler... most of the time.

Many many thanks for pointing that out so nicely.

--
James.

--- In m..., "old_cow_yellow" wrote:
>
> I do not use floating point. But I love floating point.
>
> I do not write programs in c. But I love c programmers.
>
> I did not know that c compiler can do floating point at compilation time. And I was amazed.
>
> Do you know that 1.3 in floating point is about 1.29999995?
>
> --- In m..., "jamesmichaelnewton" wrote:
> >
> > #define version 1.3
> > ...
> > typedef unsigned char byte;
> > ...
> > volatile byte SPIBuf[10];
> > ...
> > SPIBuf[5]= (int)(version)+'0';
> > SPIBuf[6]= (int)((version-(int)version)*10)+'0';
> > ...
> >
> > And SPIBuf[6] is '2'?
> >
> > Yet when version was 1.2, SPIBuf[6] was 2. 1.4 gave us 3.
> >
> > C project in IAR V4.11E/W32 [Baseline] (4.11.5.7) yes, I'm sure that's not the latest IAR, but I can't afford to restart my support contract at the moment.
> >
> > Am I misusing (int)?
> >
> > We changed to two separate defines, majorver, minorver but this bothers me...
>

We skip this problem by doing something like this:

version.h
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_REVISION 3
#define VERSION_BUILD 51

typedef struct
{
uint8 maj;
uint8 min;
uint8 revision;
uint8 build;
} Program_Version;

version.c:
static const Program_Version program_version {
VERSION_MAJOR,
VERSION_MINOR,
VERSION_REVISION,
VERSION_BUILD
};

which also facilitates a nice API for getting the version number which is stored in flash.

And, to drive Old Yellow Cow's point home, I checked this for myself using a 16-bit floating point calculator.
http://www.h-schmidt.net/FloatApplet/IEEE754.html

Stuart

--- In m..., "old_cow_yellow" wrote:
>
> I do not use floating point. But I love floating point.
>
> I do not write programs in c. But I love c programmers.
>
> I did not know that c compiler can do floating point at compilation time. And I was amazed.
>
> Do you know that 1.3 in floating point is about 1.29999995?
>
> --- In m..., "jamesmichaelnewton" wrote:
> >
> > #define version 1.3
> > ...
> > typedef unsigned char byte;
> > ...
> > volatile byte SPIBuf[10];
> > ...
> > SPIBuf[5]= (int)(version)+'0';
> > SPIBuf[6]= (int)((version-(int)version)*10)+'0';
> > ...
> >
> > And SPIBuf[6] is '2'?
> >
> > Yet when version was 1.2, SPIBuf[6] was 2. 1.4 gave us 3.
> >
> > C project in IAR V4.11E/W32 [Baseline] (4.11.5.7) yes, I'm sure that's not the latest IAR, but I can't afford to restart my support contract at the moment.
> >
> > Am I misusing (int)?
> >
> > We changed to two separate defines, majorver, minorver but this bothers me...
>