Go to Post No offense, but telling the Killer Bees they should do something is like telling Micheal Jordan how to dunk a basketball (or Lebron James for you young'ens who don't know who Michael Jordan is)... - Don Wright [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 31-01-2013, 18:38
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,077
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
remainder function


Does the C++ compiler used for FRC support the REMAINDER operator "x REM y" per IEC 60559

as specified on Page 235 Section 7.12.10.2 of ISO/IEC 9899:TC3 ?



If someone would be willing to test that with y=360

and x values of 179, 180, 181, 359, 721, -1, -179, -180, -359, -361, -721

and post results it would be much appreciated.


Attached Thumbnails
Click image for larger version

Name:	remainder.png
Views:	63
Size:	63.8 KB
ID:	13738  

Last edited by Ether : 31-01-2013 at 18:47. Reason: added screenshot
Reply With Quote
  #2   Spotlight this post!  
Unread 31-01-2013, 22:48
sur sur is offline
Registered User
AKA: Sujit Rao
FRC #3324 (Metrobots)
Team Role: Alumni
 
Join Date: May 2012
Rookie Year: 2011
Location: Ohio
Posts: 12
sur is an unknown quantity at this point
Re: remainder function

It seems to be defined if you use the C compiler, but not the C++ one. You could use fmod, which has slightly different behavior.

You can test it out here: http://codepad.org/86MlmNXU
Reply With Quote
  #3   Spotlight this post!  
Unread 01-02-2013, 00:41
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,077
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: remainder function



fmod(x,y) is quite different from remainder(x,y)

The 3 columns below are x, remainder(x,360), and fmod(360):

Code:
 180.000     -180.000      180.000
 181.000     -179.000      181.000
-181.000      179.000     -181.000
 359.000       -1.000      359.000
-359.000        1.000     -359.000
 719.000       -1.000      359.000
-719.000        1.000     -359.000


Reply With Quote
  #4   Spotlight this post!  
Unread 01-02-2013, 09:18
nightpool's Avatar
nightpool nightpool is offline
robotRectifier
AKA: Evan
no team (formerly of CORE 2062)
Team Role: Alumni
 
Join Date: Oct 2011
Rookie Year: 2011
Location: Waukesha, WI
Posts: 81
nightpool is on a distinguished road
Re: remainder function

What's the issue with using the modulus operator instead of the built-in remainder function?
__________________
Proud alum of CORE 2062.
www.core2062.com
Reply With Quote
  #5   Spotlight this post!  
Unread 01-02-2013, 09:24
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,077
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: remainder function

Quote:
Originally Posted by nightpool View Post
What's the issue with using the modulus operator instead of the built-in remainder function?
Are you saying there is a remainder function in the C++ compiler used for FRC?


Reply With Quote
  #6   Spotlight this post!  
Unread 01-02-2013, 11:54
sur sur is offline
Registered User
AKA: Sujit Rao
FRC #3324 (Metrobots)
Team Role: Alumni
 
Join Date: May 2012
Rookie Year: 2011
Location: Ohio
Posts: 12
sur is an unknown quantity at this point
Re: remainder function

It looks like you need to define it using extern "C" for WindRiver to compile it correctly:

Code:
#include <cmath>
using namespace std;

extern "C" double remainder(double, double);
Reply With Quote
  #7   Spotlight this post!  
Unread 01-02-2013, 14:16
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,077
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: remainder function

Quote:
Originally Posted by sur View Post
It looks like you need to define it using extern "C" for WindRiver to compile it correctly:

Code:
#include <cmath>
using namespace std;

extern "C" double remainder(double, double);
Would you mind running that and see if it produces the same output as this:

Code:
double myRemainder(double x, double y){
return x-y*floor(0.5+x/y);
}


Reply With Quote
  #8   Spotlight this post!  
Unread 01-02-2013, 15:12
Racer26 Racer26 is offline
Registered User
no team
Team Role: Alumni
 
Join Date: Apr 2003
Rookie Year: 2003
Location: Beaverton, ON
Posts: 2,229
Racer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond repute
Re: remainder function

What about:

Code:
double EthersRemainder( double x, double y ){

int wholenumber;

wholenumber = (int)(x/y);

double result;

result = (x - (wholenumber * y));
if(result > 0.5*y) {
result -= y;
}

return result;

}
Reply With Quote
  #9   Spotlight this post!  
Unread 01-02-2013, 15:41
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,077
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: remainder function

Quote:
Originally Posted by 1075guy View Post
What about:
EthersRemainder
That's not my remainder.

Did you compile and run it ? It doesn't work correctly.

I'm sure that there's a way, with enough conditional logic, to torture fmod() or % into into a remainder* function, but what I am really asking in this thread is

a) does the C++ compiler provided with FRC have the remainder* function, and

b) does it behave like this:


*This* is my remainder:

Code:
double myRemainder(double x, double y){
  return x-y*floor(0.5+x/y);
}

*per IEC 60559 as specified on Page 235 Section 7.12.10.2 of ISO/IEC 9899:TC3


Last edited by Ether : 01-02-2013 at 16:11.
Reply With Quote
  #10   Spotlight this post!  
Unread 01-02-2013, 17:23
Racer26 Racer26 is offline
Registered User
no team
Team Role: Alumni
 
Join Date: Apr 2003
Rookie Year: 2003
Location: Beaverton, ON
Posts: 2,229
Racer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond repute
Re: remainder function

If you have a function that works, then use it?

I don't understand your qualm?

I have not ever encountered a programming scenario where modulus division didn't solve my desire for a remainder, so I'm a bit lost.

If there doesn't exist an operator that does what you want, then you need to either a) fabricate a solution from the operators you have access to, or b) find a library that does that for you.
Reply With Quote
  #11   Spotlight this post!  
Unread 01-02-2013, 18:16
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,077
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: remainder function

Quote:
Originally Posted by 1075guy View Post
If you have a function that works, then use it?
I do use it. But I don't want to be telling other people to use my custom function if there's already a perfectly good remainder function provided with FRC C++. I don't have access to a working C++ installation, otherwise I would test it myself. I am seeking help from knowledgeable C++ users on this forum.

Quote:
I don't understand your qualm?
Honestly, that's because I don't have a qualm. I am perfectly calm.

Quote:
I have not ever encountered a programming scenario where modulus division didn't solve my desire for a remainder, so I'm a bit lost.
I have encounted at least one such scenario, and that's why I posted this thread.

For example, the IEC60559 remainder function can take any angle and normalize it to the range -180..+180 with a single C instruction:

normalizedAngle = remainder(angle,360);

I'd like to know if the remainder function is supported in FRC C++ so I can recommend it to students who are trying to do this type of conversion.

Neither the fmod() function nor the % operator can replicate the functionality of the remainder() function without using a bunch of conditional logic

If there are any Java programmers following this thread, Java appears to have the IEC 60559 remainder function; in Java it is called IEEERemainder. See attachments. If someone would be willing to test that (compare it to the implementation I posted ealier in this thread) I'd be grateful.



qualm
/kwä(l)m/
Noun

1) An uneasy feeling of doubt, worry, or fear; a misgiving.
2) A momentary faint or sick feeling.

Synonyms
nausea - queasiness
Attached Thumbnails
Click image for larger version

Name:	Java_IEEEremainder.png
Views:	19
Size:	6.2 KB
ID:	13749  Click image for larger version

Name:	20.11.14.png
Views:	19
Size:	9.8 KB
ID:	13750  
Reply With Quote
  #12   Spotlight this post!  
Unread 01-02-2013, 12:35
Racer26 Racer26 is offline
Registered User
no team
Team Role: Alumni
 
Join Date: Apr 2003
Rookie Year: 2003
Location: Beaverton, ON
Posts: 2,229
Racer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond repute
Re: remainder function

Quote:
Originally Posted by Ether View Post
Are you saying there is a remainder function in the C++ compiler used for FRC?


There's definitely a modulus division operator, which amounts to the same thing.

719 % 360 = 359
Reply With Quote
  #13   Spotlight this post!  
Unread 01-02-2013, 12:57
nightpool's Avatar
nightpool nightpool is offline
robotRectifier
AKA: Evan
no team (formerly of CORE 2062)
Team Role: Alumni
 
Join Date: Oct 2011
Rookie Year: 2011
Location: Waukesha, WI
Posts: 81
nightpool is on a distinguished road
Re: remainder function

Quote:
Originally Posted by 1075guy View Post
There's definitely a modulus division operator, which amounts to the same thing.

719 % 360 = 359
Right, but it rounds the same as fmod() (see http://www.gnu.org/software/libc/man...Functions.html). I don't have enough experience to say when this would be more or less useful in practice, but if you DO need it extern "C" seems to be the way to go about it.
__________________
Proud alum of CORE 2062.
www.core2062.com
Reply With Quote
  #14   Spotlight this post!  
Unread 01-02-2013, 13:01
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,077
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: remainder function

Quote:
Originally Posted by 1075guy View Post
There's definitely a modulus division operator, which amounts to the same thing.

719 % 360 = 359
The modulus division operator is *not* the same thing as the remainder function.

remainder(719,360) is -1, not 359.

Look at the definition of the remainder function which I attached to the original post in this thread.



Reply With Quote
  #15   Spotlight this post!  
Unread 01-02-2013, 13:21
Racer26 Racer26 is offline
Registered User
no team
Team Role: Alumni
 
Join Date: Apr 2003
Rookie Year: 2003
Location: Beaverton, ON
Posts: 2,229
Racer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond reputeRacer26 has a reputation beyond repute
Re: remainder function

OK, so do this instead:

Code:
output = (719 % 360 > 180) ? (-360 + (719 % 360)) : (719 % 360);
Reply With Quote
Reply


Thread Tools
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
Forum Jump


All times are GMT -5. The time now is 01:13.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi