C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 30th, 2012, 10:38 AM
AstroTux AstroTux is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2008
Posts: 593 AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level)AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level)AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level)AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level)AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level)AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 5 Days 21 m 29 sec
Reputation Power: 42
Logic not trapping

Hi,

This code is short and sweet. It is complete as posted. The problem is this: when the value of hdg_ins exceeds M_PI*2.0 (360 degrees) or goes negative (less than 0) the bounds check fails and doesn't execute.

1) Why
2) How to fix it?

Code:
{
	hdg_error = hdg_error + ((7.0/25920000.0)*M_PI);
	if(hdg_error >= (M_PI*2.0)) hdg_error = hdg_error - (M_PI*2.0);
	if(hdg_error < 0.0) hdg_error = hdg_error + M_PI*2;

	inertial_hdg = inertial_hdg + (rateyaw * cos(-roll)) + (ratepitch * sin(-roll));
	hdg_ins = (inertial_hdg * 0.025) + hdg_error;

	if(hdg_ins >= (M_PI*2.0)) hdg_ins = hdg_ins - (M_PI*2.0);
	if(hdg_ins < 0.0) hdg_ins = hdg_ins + (M_PI*2.0);
}


Many thanks in advance.

Best regards,
AstroTux.

Reply With Quote
  #2  
Old August 30th, 2012, 01:41 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 21 m 53 sec
Reputation Power: 1774
I think you would need to post a test case we can copy/paste/run for ourselves.

Compiling a bit of code and stepping through with gdb shows nothing unexpected here.
Code:
Breakpoint 1, main () at foo.c:18
18	    if(hdg_ins >= (M_PI*2.0)) hdg_ins = hdg_ins - (M_PI*2.0);
(gdb) list
13	    if(hdg_error < 0.0) hdg_error = hdg_error + M_PI*2;
14	
15	    inertial_hdg = inertial_hdg + (rateyaw * cos(-roll)) + (ratepitch * sin(-roll));
16	    hdg_ins = (inertial_hdg * 0.025) + hdg_error;
17	
18	    if(hdg_ins >= (M_PI*2.0)) hdg_ins = hdg_ins - (M_PI*2.0);
19	    if(hdg_ins < 0.0) hdg_ins = hdg_ins + (M_PI*2.0);
20	    }
21	    
22	    return 0;
(gdb) set hdg_ins = 7
(gdb) step
19	    if(hdg_ins < 0.0) hdg_ins = hdg_ins + (M_PI*2.0);
(gdb) step
22	    return 0;
(gdb) print hdg_ins
$5 = 0.71681469282041377

(gdb) run
Breakpoint 1, main () at foo.c:18
18	    if(hdg_ins >= (M_PI*2.0)) hdg_ins = hdg_ins - (M_PI*2.0);
(gdb) set hdg_ins = -7
(gdb) step
19	    if(hdg_ins < 0.0) hdg_ins = hdg_ins + (M_PI*2.0);
(gdb) step
22	    return 0;
(gdb) print hdg_ins
$6 = -0.71681469282041377
(gdb) 

I set positive and negative out of range values, and it all seems fine.

The only thing I can think of is if hdg_ins is a float, and you're getting some weird rounding very close to the range limits, which results in the adjustments still being out of range.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old August 30th, 2012, 02:15 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,139 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 8 m 12 sec
Reputation Power: 1974
It would also help if you gave us the test input values that you're using. Plus the variable declarations as salem alluded to.

Reply With Quote
  #4  
Old August 30th, 2012, 03:03 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,383 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 13 h 44 m 29 sec
Reputation Power: 383
use fmod, not if if if

Code:
#define PI2 (2*M_PI)
{
  hdg_error += (7.0/25920000.0)*M_PI;
  hdg_error = fmod(hdg_error+PI2,PI2);
  inertial_hdg = inertial_hdg + (rateyaw * cos(-roll)) + (ratepitch * sin(-roll));
  hdg_ins = (inertial_hdg * 0.025) + hdg_error;
  hdg_ins = fmod(hdg_ins+PI2,PI2);
}
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #5  
Old September 7th, 2012, 10:01 AM
AstroTux AstroTux is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Feb 2008
Posts: 593 AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level)AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level)AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level)AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level)AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level)AstroTux User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 5 Days 21 m 29 sec
Reputation Power: 42
Hi,

It was the float vs. integer thing.

e.g.

Code:
double result = 1/6;


1/6 treated like an integer. Destination type is double. How can I change this default behavior?

If I have:

Code:
double result = 1.0/6.0;


I get the correct result.

Best regards,
AstroTux.

Reply With Quote
  #6  
Old September 7th, 2012, 12:19 PM
EEmaestro EEmaestro is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 58 EEmaestro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 17 h 52 m 2 sec
Reputation Power: 2
Try this code & compare results.
Code:
     #include <stdio.h>

     int
     main (void)
     {
        double result;


        result = 1/6;
        printf ("\nresult = %f",result);

        result = 1./6.;
        printf ("\nresult = %f",result);

        result = (float) 1/6;
        printf ("\nresult = %f",result);

        result = (double) 1/6;
        printf ("\nresult = %f",result);

       return 0;
     }

Last edited by EEmaestro : September 7th, 2012 at 12:23 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Logic not trapping

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap