Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,427 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,901 people online right now. Registration is fast and FREE... Join Now!




Concerning the modulus % sign

 
Reply to this topicStart new topic

Concerning the modulus % sign

TheMagnitude
19 Jan, 2008 - 03:17 PM
Post #1

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
CODE

#include <math.h>
#define PI 3.14159265

float angleDist(float a,float b) // in radians, finds distance between 2 angles
{
    return fabs((((  (a-b) % 2*PI)  +3*PI) % 2*PI)-PI);
}


I get a compile error:

invalid operands of types `float' and `int' to binary `operator%'

Does the modulus sign only work with int?
User is offlineProfile CardPM
+Quote Post

Max_Payne
RE: Concerning The Modulus % Sign
19 Jan, 2008 - 05:22 PM
Post #2

D.I.C Head
**

Joined: 19 Oct, 2007
Posts: 51


My Contributions
The error is because the % operator only works on integers.


You can use fmod,
Syntax:
CODE

  #include <cmath>
  double fmod( double x, double y );


The fmod() function which takes two doubles and returns the remainder of x/y.

ej:
fmod(13.0, 2.25) is 1.75
fmod(13.0, 2.50) is 0.5

This post has been edited by Max_Payne: 19 Jan, 2008 - 05:26 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month