-3

Consider following program in C++:

#include <iostream>
int main()
{
    float f = 9.9f, m = 3.3f;
    float c = f % m;  
    std::cout << c;
    return 0;
}

Shows an error: Illegal use of floating point

When I write same program in Java like:

class Test
{
    public static void main(String args[])
    {
        float f = 9.9f, m = 3.3f;
        float c = f % m;
        System.out.println(c);
    }
}

Gives me the output: 3.2999997

and also in C#:

using System;
class Test
{
    public static void Main()
    {
        float f = 9.9f, m = 3.3f;
        float c = f % m;
        Console.WriteLine(c);
    }
}

gives output: 3.3

Why the same programming logic gives three different outputs in different language? Why we can't use modulo % with floating point numbers in C/C++ but can use in Java and C#?

Sorry if it is a fool question...I am a student...thanks.

xyz
  • 1,325
  • 5
  • 26
  • 50
  • Because the mathematical usage of `%` is only for integers. Use `fmod` instead. – Maroun Dec 28 '14 at 07:55
  • @MarounMaroun but why it works for float in java and C#? – xyz Dec 28 '14 at 07:56
  • Kernighan and Ritchie didn't think it belongs into the compiler. This is indeed better placed in a math library. – laune Dec 28 '14 at 07:57
  • 2
    @MarounMaroun NO, it isn't duplicate? can you explain its behavior in Java and C#?. – xyz Dec 28 '14 at 07:59
  • 3
    Because c/c++ is not java or c#, they are syntactically similar, but also different. – Ryan Mann Dec 28 '14 at 08:11
  • @Ryios explain in answer. – xyz Dec 28 '14 at 08:14
  • 2
    Open the duplicated read the accepted answer. You will have the answer why it is in the c++ like this, why in c#, java is like this. BECAUSE they made this function. – mybirthname Dec 28 '14 at 08:28
  • duplibcate of http://stackoverflow.com/questions/25340496/why-does-modulus-operator-work-for-char-but-not-for-floating-types? – mafso Dec 28 '14 at 09:25

2 Answers2

1

Probably C and C++ don't have it because there is more than one way to do it: rounding towards zero (fmod) and rounding towards the nearest integer (remainder). You'd have to know (or to look up) which versions are chosen by the other languages you are citing.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • But that is true for integer values as well. – Rudy Velthuis Dec 30 '14 at 01:01
  • @RudyVelthuis, no, I don't think so. In all mathematical context I know of integer division is uniquely defined and never amounts to rounding. – Jens Gustedt Dec 30 '14 at 12:19
  • No rounding, but the modulo can either be toward 0 or toward negtive infinity. So, yes, this is true for integers as well. Just take a look at the table at the right of [this page](http://en.wikipedia.org/wiki/Modulo_operation#Modulo_operation_expression). It is either the sign of the divisor, the dividend, always positive or implementation defined, depending on the language (even in early C, it was implementation defined). – Rudy Velthuis Dec 31 '14 at 18:01
  • Especially: << Informally speaking the quotient is "rounded towards zero", and the remainder therefore has the same sign as the dividend. Knuth described floored division where the quotient is defined by the floor function q=floor(a/n) >> – Rudy Velthuis Dec 31 '14 at 18:14
-1

I found the easiest solution from here.

The modulo operator (remainder operator) returns that part of a division that is discarded (the remainder) in case of a non floating point operation.

So when you have floating point modulo doesn't make sense since nothing is discarded.

e.g.

int:
155 % 100 = 55

so, for integer division, 155 / 100 = 1 reminder = 55

and for floating point division: 155 / 100 = 1.55 remainder=0.0

so, as there is no reminder while dividing floating points, % operator does not work for floating point arithmetic.

So, in earlier language like C, % operator for floating point was not supported.

In java and C#, the notion of remainder is expanded and while using % operator, floats are considered as a number prices up to some x decimal points.

for ex, Java will take 9.900000 % 3.300000 = 3.2999997 and C# will take 9.9 % 3.3 = 3.3. So, here 9.900000 and all floats becomes prices no.s while using %. and so they can give remainder. in actual, they are 9.9xxxxxxxxxxxxxxxxxxx... (not precise).

xyz
  • 1,325
  • 5
  • 26
  • 50
  • 1
    Note that it is called the *remainder operator* in C++. Not the *modulo operator*. – juanchopanza Dec 28 '14 at 09:30
  • @juanchopanza i'm talking about general mathematics...not C++. – xyz Dec 28 '14 at 09:31
  • The last part about "decimal points" is quite unclear. Takeaway message: float is precise, but binary precise to `x` bits. Rounding to `y` decimal digits isn't precise. – MSalters Dec 29 '14 at 02:10
  • @MSalters i can't understand...If you know than please edit...I'll approve it – xyz Jan 01 '15 at 11:32