locked
Using floats with % and ^ operators RRS feed

  • Question

  • Why does MC++ .NET 2005 return and error when I try to compile and I use a float type varaible with the % and ^ operators?

    Example:

    float temp, result;

    temp = 2.5;

    result = temp % 1;

    result = 2^temp;

     

    With both of these lines I get an error saying that the type float is not allowed with those operators. I have found a work around for the first but I can not mathmatically figure out one for the second problem. Is there some sort of patch to fix this?

    Tuesday, September 5, 2006 6:45 PM

Answers

  • Yes, these operators only work on integral types.  The floating point alternative for '%' is fmod().  There is no alternative for the XOR operator, it just doesn't make sense; you can't do anything reasonable with the fractional part.  Best you can possibly do is cast/trunc/round the float to an integer...
    Tuesday, September 5, 2006 6:59 PM

All replies

  • Yes, these operators only work on integral types.  The floating point alternative for '%' is fmod().  There is no alternative for the XOR operator, it just doesn't make sense; you can't do anything reasonable with the fractional part.  Best you can possibly do is cast/trunc/round the float to an integer...
    Tuesday, September 5, 2006 6:59 PM
  • Let's see if we're speaking about the same things:

    % is the remainder operator (so it does not make much sense to use it with a float, remainder is an integer operation)

    ^ is the bitwise XOR operator (and again it does not make much sense to use it with floats)

    So what exactly do you want % and ^ to do for float values ?

     

    Tuesday, September 5, 2006 7:05 PM
  • Maybe a cheap and dirty random number generator?
    Tuesday, September 5, 2006 7:27 PM
  • I am using the % operator to modulus a number by 1 to get the decimal part of the number. This part was for converting the decimal part of the value of gold into silver or silver into copper. So 2.5 silver pieces would be 2 silver pieces and 5 copper pieces when 10 copper pieces = 1 silver piece.

    The ^ is also the exponent operator. 2^3 = 2 * 2 * 2 = 8. I am using this as part of the formula for taking the strength score of a character and determining what the carrying capacity for that caracter is. The exact formula I am using is 100 * 2 ^ ((Strength - 10) / 5).

    Both of these things are for a much larger program that eventually be a toolkit for Dungeons and Dragons using the d20 system.

    Tuesday, September 5, 2006 7:40 PM
  • This is C++ not FORTRAN. In C++ ^ is not the exponential operator it is the binary XOR operator. You need to use to use one of the pow functions.
    Tuesday, September 5, 2006 7:50 PM
  • Either they have changed that from previous versions or I'm mixing it up with other language like VB and Java. But thanks for the help, it's working now with the pow function.
    Tuesday, September 5, 2006 7:54 PM
  • No C/C++ compiler gives you a floating point modulus operator ala '%'.  Use modf and its variants instead.

    int main()
    {
    float value = 2.5F;
    float whole;
    float fraction = modff( value, &whole );
    // whole is 2.0.  fraction is 0.5.
    return 0;
    }

    ^ is not an exponentiation operator in C/C++ for any datatype.  For floating point exponentiation use the pow function and its variants.

    I would pick up a book on C (Kernigan/Richie is a classic) and review.

    Brian

    Tuesday, September 5, 2006 7:58 PM
  • It would be useful for a 2-variable swap operation

    a ^= b

    b ^= a

    a ^= b

    Sunday, December 2, 2018 4:13 PM
  • Even for integers this has no place in any code other than the obfuscated code contest.
    Sunday, December 2, 2018 7:09 PM
  • It would be useful for a 2-variable swap operation

    a ^= b

    b ^= a

    a ^= b

    Just in case you hadn't noticed, you posted your reply to a thread that is
    more than 12 years old. 

    You may have decided that it was worth commenting on even though it is 
    such an old thread. But if you just missed that fact then this is simply 
    a reminder that it often pays to check the dates on posts/threads before 
    spending time composing and posting a reply. 

    Reactivating very old threads, especially those already marked as "Answered",
    is usually not a good practice. Many things will have changed since the
    thread was started many years ago. Starting a new thread to discuss the
    same or similar issues is best, and of course any new thread can include
    links or references to older threads if deemed useful.

    - Wayne

    Sunday, December 2, 2018 8:19 PM