![]() |
remainder function
1 Attachment(s)
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. |
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 |
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 |
Re: remainder function
What's the issue with using the modulus operator instead of the built-in remainder function?
|
Re: remainder function
Quote:
|
Re: remainder function
It looks like you need to define it using extern "C" for WindRiver to compile it correctly:
Code:
#include <cmath> |
Re: remainder function
Quote:
719 % 360 = 359 |
Re: remainder function
Quote:
|
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. |
Re: remainder function
OK, so do this instead:
Code:
output = (719 % 360 > 180) ? (-360 + (719 % 360)) : (719 % 360); |
Re: remainder function
I can see how the remainder function would be useful in certain cases, except that I don't like the name of the function. It's non-intuitive, and the results would surprise many people who look at the code after the original writer. When I think back to when I learned division in elementary school, the remainder of two positive numbers was always positive (or zero). I'm trying to think of what I would have named that function, to be more in line with its return values.
|
Re: remainder function
The "normal" name for this function is drem(), which I think is a contraction of d-something and remainder.
|
Re: remainder function
Quote:
|
Re: remainder function
"Delta Remainder" sounds cool. At least when I see drem, I would expect slightly different behavior, thus obeying the programming law of Minimize Surprise.
|
Re: remainder function
GNU libc. See the link in my previous post.
|
Re: remainder function
Quote:
|
Re: remainder function
Quote:
Code:
double myRemainder(double x, double y){ |
Re: remainder function
What about:
Code:
double EthersRemainder( double x, double y ){ |
Re: remainder function
Quote:
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){*per IEC 60559 as specified on Page 235 Section 7.12.10.2 of ISO/IEC 9899:TC3 |
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. |
Re: remainder function
2 Attachment(s)
Quote:
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 |
Re: remainder function
In a Java SE project this
Code:
double result;Code:
x: 179.0 rem y: 360 result: 179.0However I cannot use IEEERemainder() in an FRC project they have compiled the jar without it apparently. |
Re: remainder function
Quote:
Quote:
Are there any C++ gurus out there who can look into the same question for C++ ? |
Re: remainder function
Quote:
|
Re: remainder function
Thanks!
Is there a cross reference of where SE stuff that's been moved can be found? |
Re: remainder function
Quote:
|
Re: remainder function
Here's the result from the robot
Code:
double result;Quote:
|
Re: remainder function
Quote:
|
Re: remainder function
Quote:
|
Re: remainder function
From my installation, a grep through the headers folder shows no remainder functions. It's possible I'm missing something, but it looks like it doesn't exist at all in the vxWorks API.
As for the extern "C" option, yes that will make it compile, however the function will most likely not be resolved by the linker, which will cause problems when you try to run it. Wind River's system is designed to not complain about missing symbols until the cRIO's kernel tries to load the program. I'll check if this is the case tomorrow. EDIT: actually, drem() does exist, and is defined to be the same as remainder() (see the glibc manual). However, the definition for it is in <private/trigP.h>, which isn't meant for inclusion by user programs. There don't seem to be any public headers which include it either. |
Re: remainder function
Quote:
Notice the very slight difference between the glibc and Java definitions (handling of divide-by-zero). |
Re: remainder function
The "java" link you posted was to gnu libc...
|
Re: remainder function
Quote:
|
Re: remainder function
Quote:
|
Re: remainder function
Quote:
Code:
x: 179.0 rem y: 360 result: 179.0 |
Re: remainder function
Quote:
For those of you reading this thread for the first time, Math.IEEEremainder(angle,360.0) can be used to convert any angle (in degrees) to the range -180 to 180. This can be very useful when working with the gyro. Let's say you want to find the shortest angular error between your joystick direction angle and the gyro angle. This can be accomplished in one clean line of code as follows: angle_error = Math.IEEEremainder(joystick-gyro,360.0); There's no need for conditional logic. It cleanly handles all input angle ranges for joystick and gyro, including angles greater than 360 degrees and less than -360 degrees. The above assumes that gyro and joystick share the same zero and measurement direction. |
| All times are GMT -5. The time now is 12:29. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi