Jump to content
joli

Bad Result In Sum Of Products

Recommended Posts

    unsigned char xpd=9;
    unsigned char xpm=38;
    unsigned int  xpf=4534;
    unsigned long xr;

    xr = ((xpd*600000)+(xpm*10000)+xpf);  // 5456854 <<< error
    
    xr =  xpd*600000;
    xr += xpm*10000;
    xr += xpf;                            // 5784534 <<< ok

Simulated by sb_7.30

 

Check again and again the math section of sourceboost manual and not see anything that tell me that i can't do it all in one single line.

 

br

Edited by joli

Share this post


Link to post
Share on other sites

Use the -Op compiler option.

 

See manual pages 23/24 on integer promotion.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

Hi

 

Or simply use the "L" specifier on the constants to have the expressions involving "xpd" and "xpm" pormoted to "long".

 

 

Just my 2 cents...

 

Best regards

Jorge

 

PS: All this is plain 'C', not a sourceboost specific thing.

Edited by JorgeF

Share this post


Link to post
Share on other sites

Hi

 

Thanks Reynard

 

I wasn't aware of any limitations on the subject.

I had never had problems with it, but in fact, I can't remember the last time I used it with BoostC.

When it comes to PIC16/18 devices, I tend to maximize "byte" usage and only use other formats when absolutely necessary.

 

 

Best regards

Jorge

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...