modulo(%) operation with double

This is a discussion on modulo(%) operation with double within the C Programming forums, part of the General Programming Boards category; I would like to do an operation like: double a, b, c; a = (b % c); But Gcc doesn't ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    1

    modulo(%) operation with double

    I would like to do an operation like:

    double a, b, c;

    a = (b % c);


    But Gcc doesn't allow me to do it, because it says % operation can't be used with double. (Invalid operands to binary %).

    How can I do the modulo operation without using % or using the % and double types.

    Thanks,

    Joćo Pedro

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    With modf() from math.h of course.
    modf - C++ Reference

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    The reason for this is that there is no remainder when performing division with a type capable of representing the actual answer.
    E.g.
    11 / 4 => 2, remainder 3 (where 3 = 11 % 4)
    but 11.0 / 4.0 = 2.75, no remainder ever!

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    39
    The reason for this is that there is no remainder when performing division with a type capable of representing the actual answer.
    E.g.
    11 / 4 => 2, remainder 3 (where 3 = 11 % 4)
    but 11.0 / 4.0 = 2.75, no remainder ever!

    You can use % with C# or Java with real values...

  5. #5
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Why doesn't C just do an automatic conversion from double to int in this case?

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    because you would lose your decimal values
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

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, 03:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

Tags for this Thread


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