Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   remainder function (http://www.chiefdelphi.com/forums/showthread.php?t=112483)

Ether 01-31-2013 06:38 PM

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.



sur 01-31-2013 10:48 PM

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

Ether 02-01-2013 12:41 AM

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




nightpool 02-01-2013 09:18 AM

Re: remainder function
 
What's the issue with using the modulus operator instead of the built-in remainder function?

Ether 02-01-2013 09:24 AM

Re: remainder function
 
Quote:

Originally Posted by nightpool (Post 1225780)
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?



sur 02-01-2013 11:54 AM

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);


Racer26 02-01-2013 12:35 PM

Re: remainder function
 
Quote:

Originally Posted by Ether (Post 1225784)
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

nightpool 02-01-2013 12:57 PM

Re: remainder function
 
Quote:

Originally Posted by 1075guy (Post 1225893)
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.

Ether 02-01-2013 01:01 PM

Re: remainder function
 
Quote:

Originally Posted by 1075guy (Post 1225893)
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.




Racer26 02-01-2013 01:21 PM

Re: remainder function
 
OK, so do this instead:

Code:

output = (719 % 360 > 180) ? (-360 + (719 % 360)) : (719 % 360);

Cal578 02-01-2013 01:38 PM

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.

nightpool 02-01-2013 01:40 PM

Re: remainder function
 
The "normal" name for this function is drem(), which I think is a contraction of d-something and remainder.

Ether 02-01-2013 01:45 PM

Re: remainder function
 
Quote:

Originally Posted by nightpool (Post 1225926)
The "normal" name for this function is drem()

"Normal" according to whom? Link please.



Cal578 02-01-2013 01:47 PM

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.

nightpool 02-01-2013 01:55 PM

Re: remainder function
 
GNU libc. See the link in my previous post.


All times are GMT -5. The time now is 09:29 AM.

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