modulus operator with double operands?

This is a discussion on modulus operator with double operands? within the C Programming forums, part of the General Programming Boards category; Can the modulus operator work on double (data type) operands? If not, can anyone refer me to some website that ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    16

    modulus operator with double operands?

    Can the modulus operator work on double (data type) operands?
    If not, can anyone refer me to some website that gives c code which does the modulus operation on double operands?
    You may also suggest at least a pseudocode on doing this.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    This isn't really a place to 'make demands'. Try it and see if it works yourself.

    > You may also suggest at least a pseudocode on doing this.
    Okay, since you asked,
    Code:
    A MODULUS B

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    I don't think that's a demand.
    I'm asking if anyone knows whether % works with double operands 'coz it's causing compile-time errors in my program.
    Also, I wouldn't ask that question if I haven't tried it.
    If you can't answer my question, then don't.
    You don't have to post stupid pseudocodes.
    Last edited by chickenandfries; 03-28-2008 at 08:02 AM.

  4. #4
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by chickenandfries View Post
    I don't think that's a demand.
    I'm asking if anyone knows whether % works with double operands 'coz it's causing compile-time errors in my program.
    Also, I wouldn't ask that question if I haven't tried it.
    If you can't answer my question, just don't.
    You don't have to post stupid pseudocodes.
    So let me get this straight . . .

    You've put the code into a compiler and it DOESN'T work. I guess I'm unsure where the confusion is.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    Compilation of the program failed.
    The compiler indicated that modulus operation is not allowed with int and double as operands.

  6. #6
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by chickenandfries View Post
    Compilation of the program failed.
    The compiler indicated that modulus operation is not allowed with int and double as operands.
    This code will work:
    Code:
    int main(void)
    {
    	int a = 55;
    	int b = 11;
    	int c;
    	
    	c = a % b;
    	
    	printf("Answer is %d.\n", (int)c);
    	fflush(stdout);
    	
    	return 0;
    }
    This code will not:
    Code:
    int main(void)
    {
    	double a = 55;
    	double b = 11;
    	double c;
    	
    	c = a % b;
    	
    	printf("Answer is %d.\n", (int) c);
    	fflush(stdout);
    	
    	return 0;
    }
    Since it throws an error on c = a % b, I think you have your answer. I would not doubt, however, that there is a standard library function to do this. Google search for a modulus function that takes in doubles, or write your own.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This is correct, you can not use % on non-integers.

    There is a library function called fmod(): http://www.hmug.org/man/3/fmod.php

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    fmod() worked!
    Thanks matsp!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 04:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 09:38 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21