Be a Supporter!

not sure what its called

  • 263 Views
  • 11 Replies
New Topic Respond to this Topic
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
not sure what its called 2005-06-23 15:57:15 Reply

i know in AS2 there is an operator % which returns the remainder of a division for eg.
3.5%1 = 0.5, 4%3 = 1,

what is this in c++;

i tried using it but get the error of

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

im trying to use it to remove the decimal points of a number (double) by doing

u2 -= u2%1;

where u2 is the double


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to not sure what its called 2005-06-23 16:01:22 Reply

sorry about this, i just had a look through the math header and found the floor function

id still like to know bout it tho please.


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Cinjection
Cinjection
  • Member since: Apr. 6, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to not sure what its called 2005-06-23 16:02:46 Reply

It's called a 'modual' function and it looks like % that. Here's some code that will use mods (for short) to see if a number is prime.


//Programmed by Oleksi Derkatch/Cinjection
//June 17 2005
//Fuck i should be studing for my next exam ;P

#include <iostream>

using namespace std;

int main(){

int factors[100];
int curfactor = -1;
int number;

cout<<"Enter a number and this program will tell you if ";
cout<<"it is a prime number."<<endl;
cin>>number;
cin.ignore();

for (int c = 1; c <= number; c++){
if (number % c == 0){
curfactor += 1;
factors[curfactor] = c;
}
}

if (curfactor >= 2){
cout<<number<<" is not a prime number."<<endl;
cout<<"It's factors are:"<<endl;
for (c = 0; c <= curfactor - 1; c++){
if ((factors[c] != 1) && (factors[c] != number)){
cout<<factors[c]<<endl;
}
}
}
else{
cout<<number<<" is a prime number."<<endl;
}

cin.get();
return 0;

}

Sar-Casm
Sar-Casm
  • Member since: Dec. 13, 2002
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to not sure what its called 2005-06-24 07:36:39 Reply

At 6/23/05 04:02 PM, Cinjection wrote: It's called a 'modual' function and it looks like % that.

Actually, it is 'modulo' and it is an operator. It usually returns the remainder of division. E.g.:

5 % 2 = 1
10 % 4 = 2

Sar-Casm
Sar-Casm
  • Member since: Dec. 13, 2002
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to not sure what its called 2005-06-24 07:38:01 Reply

At 6/24/05 07:36 AM, JohnCarmack wrote:
At 6/23/05 04:02 PM, Cinjection wrote: It's called a 'modual' function and it looks like % that.
Actually, it is 'modulo' and it is an operator. It usually returns the remainder of division. E.g.:

5 % 2 = 1
10 % 4 = 2

Oops. Owned myself. :-[

Cinjection
Cinjection
  • Member since: Apr. 6, 2004
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to not sure what its called 2005-06-24 09:17:27 Reply

At 6/24/05 07:36 AM, JohnCarmack wrote: Actually, it is 'modulo' and it is an operator.

Damn, That's right. My Bad. Stupid exams ruining my mind! :p

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to not sure what its called 2005-06-25 07:37:14 Reply

so it can only be used with integers?


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
PONGpaddle
PONGpaddle
  • Member since: Sep. 23, 2003
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to not sure what its called 2005-06-25 07:53:35 Reply

At 6/25/05 07:37 AM, dELta_Luca wrote: so it can only be used with integers?

I'm pretty sure it can be used with all numerical data types, like double, long, float, etc. I'm not sure if you can do:

float number = x % y;

Or you have to do:

float number = (float)(x % y);

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to not sure what its called 2005-06-25 08:01:33 Reply

well i jsu tried a simple

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
double a = 10;
double b = 12;
double c = b%a;
cout << c;
getch();
return 0;
}

and i get compiling error:
"invalid operands of types `double' and `double' to binary `operator%' "


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
PONGpaddle
PONGpaddle
  • Member since: Sep. 23, 2003
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to not sure what its called 2005-06-25 09:11:40 Reply

Try this:

#include <iostream>

using namespace std;

int main()
{
double a = 10;
double b = 12;
double c = (double)((int)b%(int)a);
cout << c;
cin.get();
return 0;
}

PONGpaddle
PONGpaddle
  • Member since: Sep. 23, 2003
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to not sure what its called 2005-06-25 09:13:52 Reply

By the way, modulus needs only the number to the left to it to be an integer. This would also work:

#include <iostream>

using namespace std;

int main()
{
double a = 10;
int b = 12;
double c = (double)(b%(int)a);
cout << c;
cin.get();
return 0;
}

PONGpaddle
PONGpaddle
  • Member since: Sep. 23, 2003
  • Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to not sure what its called 2005-06-25 09:15:23 Reply

Sorry, triple post.

Scrap my last post, I was talking shit.