Substraction error

General discussion on mikroC.
Post Reply
Author
Message
Pesticida
Posts: 88
Joined: 26 Sep 2008 08:02

Substraction error

#1 Post by Pesticida » 24 Oct 2010 19:11

Hello,
I just test a simple code.
But it's giving a wrong result !

Code: Select all


signed int err;
signed int pitchOffset;
signed int pitch;


void main() {
pitchOffset = -36;
pitch = -37;

  do
  {

     err=pitch - pitchOffset;



       }while (1);
       }
the result is err=65535 why?

Thanks
is very urgent!

regards Pesti

KaranSoin
Posts: 130
Joined: 07 May 2010 22:27
Location: Melbourne, Australia

Re: Substraction error

#2 Post by KaranSoin » 24 Oct 2010 22:18

how are you checking the result ? LCD display, debugger ? The result is actually correct (-1 is represented as 65535 in 2's compliment), but is somehow being represented as unsigned int instead of signed int. However you are trying to display the value, one of the functions in there accepts an unsigned int value and is treating the -1 as 65535


Regards

Pesticida
Posts: 88
Joined: 26 Sep 2008 08:02

Re: Substraction error

#3 Post by Pesticida » 25 Oct 2010 07:38

Hello,

I Display this over Uart with InttoStr Function.

I test the same code with the trial version of mikroc pro and the result is ok!

Thanks
Regards Pesti

KaranSoin
Posts: 130
Joined: 07 May 2010 22:27
Location: Melbourne, Australia

Re: Substraction error

#4 Post by KaranSoin » 25 Oct 2010 08:56

Thats quite strange, I cant see anything obviously wrong with this. Post ur code if possible, may be it will reveal something.


cheers

Pesticida
Posts: 88
Joined: 26 Sep 2008 08:02

Re: Substraction error

#5 Post by Pesticida » 25 Oct 2010 12:33

Code: Select all

float pitch_est=0;
float Offset_Pitch = 0;
float wy = 0.0;
float yy = 0.0;
float esumy ;
float erry  ;
float I = 0.01 ;
float Kp = 0.2 ;
float DY = 1 ;
float ealty = 0.0;
float y=0;

wy = pitch_est;
yy = Offset_Pitch ;

....
/loop every 10 mS
erry = yy - wy;  // aktuelle Regelabweichung bestimmen

if ((y <220) && (y > 0))  
{
      // (Anti-Windup)
esumy = esumy + erry ;      
}
y = RC_Throttle + (Kp * erry) + (I * 0.02 * esumy) + (DY * ((erry - ealty)) / 0.02) ; // Reglergleichung
ealty = erry  ;               
if (y > 220) y = 220 ;             
if (y < 1) y=0;
}

when wy and yy are negative i got always the wrong result ,for ex. wy = -35 and yy = -35 the result should be 0 but i got 35.

Pesticida
Posts: 88
Joined: 26 Sep 2008 08:02

Re: Substraction error

#6 Post by Pesticida » 25 Oct 2010 19:53

Nobody can Help me ? :-(

zan
Posts: 434
Joined: 15 May 2007 19:35

Re: Substraction error

#7 Post by zan » 25 Oct 2010 20:03

I'm not using mikroC, but maybe your problem is IntToStr. I think you need to use FloatToStr.

Pesticida
Posts: 88
Joined: 26 Sep 2008 08:02

Re: Substraction error

#8 Post by Pesticida » 25 Oct 2010 20:12

Hello,

The Problem ist that i got the wrong value on the Debugger too!

Maybe is a MIkroc Bug?!

On the Mikroc Pro Demo i got the right value.

KaranSoin
Posts: 130
Joined: 07 May 2010 22:27
Location: Melbourne, Australia

Re: Substraction error

#9 Post by KaranSoin » 25 Oct 2010 21:02

quite possible its a MikroC bug, upgrade to a newer if you dont have it already.



Regards

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: Substraction error

#10 Post by tihomir.losic » 26 Oct 2010 13:54

Hello,

please, look at screen shot... I didn't reproduce your issue.
For test is used mikroC PRO for PIC v4.15, and PIC16F887:
oduzimanje.png
oduzimanje.png (117.78 KiB) Viewed 3684 times
Best regards,

Losic Tihomir
mikroElektronika [Support team]

Pesticida
Posts: 88
Joined: 26 Sep 2008 08:02

Re: Substraction error

#11 Post by Pesticida » 26 Oct 2010 16:42

Yes I know on mikro c pro is working but on mikro c 8.2 i got always the wrong Result.
mikroc8_2.jpg
mikroc8_2.jpg (126.74 KiB) Viewed 3681 times

KaranSoin
Posts: 130
Joined: 07 May 2010 22:27
Location: Melbourne, Australia

Re: Substraction error

#12 Post by KaranSoin » 26 Oct 2010 18:13

The Result is actually correct. -1, in MICROCHIP 32 bit float format, is represented as 7F80 0000 which is equates to 2139095040 in decimal. If you single click on the value, a small button will appear, which opens up a "Edit Value" dialog box. There is a good chance that you have "Dec" representation selected instead of "Float". I am using version 3.2 so may be a slightly different picture.


Regards
Attachments
format_selection.PNG
format_selection.PNG (53.5 KiB) Viewed 3678 times

Post Reply

Return to “mikroC General”