|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
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. Last edited by Ether : 31-01-2013 at 18:47. Reason: added screenshot |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
||||
|
||||
|
Re: remainder function
What's the issue with using the modulus operator instead of the built-in remainder function?
|
|
#5
|
||||
|
||||
|
Re: remainder function
Quote:
|
|
#6
|
|||
|
|||
|
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); |
|
#7
|
||||
|
||||
|
Re: remainder function
Quote:
Code:
double myRemainder(double x, double y){
return x-y*floor(0.5+x/y);
}
|
|
#8
|
|||
|
|||
|
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;
}
|
|
#9
|
||||
|
||||
|
Re: remainder function
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. |
|
#10
|
|||
|
|||
|
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. |
|
#11
|
||||
|
||||
|
Re: remainder function
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:
Quote:
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 |
|
#12
|
|||
|
|||
|
Re: remainder function
Quote:
719 % 360 = 359 |
|
#13
|
||||
|
||||
|
Re: remainder function
Quote:
|
|
#14
|
||||
|
||||
|
Re: remainder function
Quote:
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. |
|
#15
|
|||
|
|||
|
Re: remainder function
OK, so do this instead:
Code:
output = (719 % 360 > 180) ? (-360 + (719 % 360)) : (719 % 360); |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|