Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no division remainder operation for floats/doubles in C and C++? [duplicate]

Tags:

java

c++

c

c#

division

Possible Duplicate:
Why does modulus division (%) only work with integers?

This code doesn't work in C and C++ but works in C# and Java:

float x = 3.4f % 1.1f;
double x = 3.4 % 1.1;

Also, division remainder is defined for reals in Python.

What is the reason this operation is not defined for floats and doubles in C and C++?

like image 998
Andrey Agibalov Avatar asked Jul 14 '12 20:07

Andrey Agibalov


People also ask

Which operator Cannot be used with float operands in C?

% operator cannot be used with floating point numbers in C & C++.

Can you see modulo division operator with float and int?

15) Can you use C Modulo Division operator % with float and int? Explanation: Modulo Division operator % in C language can be used only with integer variables or constants.

Does modulo work with floats?

The modulo operator, like the other arithmetic operators, can be used with the numeric types int and float .

How do you find the remainder of a float?

The modulus is basically finding the remainder. For this, we can use the remainder() function in C. The remainder() function is used to compute the floating point remainder of numerator/denominator.


1 Answers

The C committee explained its position of why there is no remainder operator for floating types in the Rationale document:

(6.5.5 Multiplicative operators) The C89 Committee rejected extending the % operator to work on floating types as such usage would duplicate the facility provided by fmod (see §7.12.10.1).

like image 87
ouah Avatar answered Oct 11 '22 14:10

ouah