LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-02-2016, 03:58 PM   #1
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Rep: Reputation: Disabled
Strange code skipping while running C++ routine.


My environment is Ubuntu 14.04. I am trying to debug this program and one of the routines exhibits behavior I've never seen.

Code:
public : double norm_ALP_row_LR (double xx, int ll, int mm)
  {
...........................
   // debug
    if ( ll == 30 && mm == 1)
      int bb = 1;  // <=====
    // debug ends
    if (mm%2 != 0)
      res = - res;  // <=====
    return res;
My ll runs from 13 to 30 and mm from 0 to ll. With any ll value the execution does not stop on statements I marked <==== when mm = 0 and mm = 1 but it stops at mm >= 2. More down the road I print result res and it does not change the sign under any circumstances when mm == 1. I check the execution flow with gdb and it gives me the same skip. What is going on? Thank you.

Last edited by AlexBB; 12-02-2016 at 04:11 PM.
 
Old 12-02-2016, 07:35 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by AlexBB View Post
My environment is Ubuntu 14.04. I am trying to debug this program and one of the routines exhibits behavior I've never seen.

Code:
public : double norm_ALP_row_LR (double xx, int ll, int mm)
  {
...........................
   // debug
    if ( ll == 30 && mm == 1)
      int bb = 1;  // <=====
    // debug ends
    if (mm%2 != 0)
      res = - res;  // <=====
    return res;
My ll runs from 13 to 30 and mm from 0 to ll. With any ll value the execution does not stop on statements I marked <==== when mm = 0 and mm = 1 but it stops at mm >= 2. More down the road I print result res and it does not change the sign under any circumstances when mm == 1. I check the execution flow with gdb and it gives me the same skip. What is going on? Thank you.
is that proper syntax?

Code:
res = - res; 
as apposed to this
Code:
res =- res; 
or this
Code:
res = res; 
if this is a command line program


Code:
//check values ?
// not sure if that is how it is written it has beena long time sense I've did c++

cout >> ll;
cout >>mm;

  if ( ll == 30 && mm == 1)
      int bb = 1;  // <=====
    // debug ends
/** check values ?**/
cout >> mm%2 ;

    if (mm%2 != 0)
      res = - res;  // <=====
    return res;

Last edited by BW-userx; 12-02-2016 at 07:44 PM.
 
Old 12-02-2016, 07:49 PM   #3
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
It skips those lines for two particular values of index mm, mm= 0 & mm = 1. It is weird.
 
Old 12-02-2016, 08:11 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by AlexBB View Post
It skips those lines for two particular values of index mm, mm= 0 & mm = 1. It is weird.
what does ll equal?
these both have to equate to true
Code:
( ll == 30 && mm == 1)
explicitly
Code:
if 30 and 1 then bb =1
you're saying if mm = 0 or 1 it bypasses bb = 1?

Code:
if ( something ) {
do this 
do that
}
if (something else) {
do this too
}
return results;

Last edited by BW-userx; 12-02-2016 at 08:15 PM.
 
Old 12-02-2016, 10:04 PM   #5
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
I would probably write res = (-1*res); To avoid any compiler versioning quirks that might have odd interpretations. Order of precedence does matter. Especially when passing a var a value based on itself. But I tend to avoid advanced features so interns and managers have better odds of not being wrong when telling me what my code does. Plus specs change so if it changes to (-2*res) I only have to modify a single char since I already wrote the longform.

The special case of mm=1 is AND'd with ll=30. &&
Did you mean to OR it? ||
 
Old 12-02-2016, 10:11 PM   #6
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
The modulus of 0 might be overflow (0/0) or some other special case and not 0. But it's been a long time since I coded C++. You might try (mm+2)%2 to get the desired outcome (assuming mm is never negative). Although slower do to the extra math.
 
Old 12-03-2016, 10:38 AM   #7
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
That you all for considerations. Answering some questions wholesale, I posted a fragment of a subroutine that computes Normalized Special function, Associated Legendre Polynomials. int ll varies from 13 to 30, because values for indexes from 0 to 12 are computed by a different routine. Correspondingly index mm varies from 0 to ll. I did try (mm+10)%2 and it did not work. For that statement
Code:
if (ll == 30 && mm == 1)
I simply chose an isolated case, not a special one, which is not difficult to catch in gdb debug. It could have been
Code:
if (ll == 25 && mm == 1)
or similar.

BTW, I use g++ compiler.

I will keep experimenting. Thanks.

Last edited by AlexBB; 12-03-2016 at 10:40 AM.
 
Old 12-03-2016, 11:01 AM   #8
AlexBB
Member
 
Registered: Mar 2014
Posts: 464

Original Poster
Rep: Reputation: Disabled
My apologies. I missed a piece of code diverting result before that place in the code for mm=0 & mm==1. It is a recurrent formula with complicated structure. Everything is gine with C++ and g++. Sorry.
 
Old 12-04-2016, 06:44 AM   #9
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
One gotcha I remember is that if you mm=0 in a comparison, it sets the value, not compares it(always true?). So if the comparison is what you want, use == instead of a single =.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange behavior with simple backup routine jbruyet Linux - Software 2 02-15-2013 01:22 PM
how to determine the current sub routine in a perl code ? kshkid Programming 5 03-19-2007 06:47 AM
Strange code in my terminal UltraSoul Solaris / OpenSolaris 5 07-25-2005 12:15 PM
Strange error in C code zaichik Programming 2 02-08-2005 05:21 PM
FC2 test 3 mouse skipping a beat, movie skipping also jang Fedora 1 10-28-2004 07:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration