+ 1

Why float cannot be applied on modulus operator?

4th Jun 2018, 8:42 AM
Bunny
Bunny - avatar
9 Antworten
+ 2
because if you for example divide 2.5 by 2 you get 1.25 and not 1, so there is no left over part and it doesn‘t really make sense to define % in that context
4th Jun 2018, 9:00 AM
Max
Max - avatar
+ 2
Because C was designed to provide an efficient high-level programming environment to the PDP-11 and it didn't have a modulus instruction for floating point numbers.
4th Jun 2018, 12:19 PM
Vlad Serbu
Vlad Serbu - avatar
+ 1
@Max module for float has sense... In your example 2.5 % 2 is 0.5.. Much languages make work they module operator working with float numbers Anyway you cannot use floats with % operator because, standard tell that you cannot... Simple... But not all is losed... Exist a function for this, fmod and work like you expect
4th Jun 2018, 9:21 AM
KrOW
KrOW - avatar
0
KrOW the only ideals in a field are the 0 ideal and the field itself, so your arbitrary definition had no justification
4th Jun 2018, 9:27 AM
Max
Max - avatar
0
Max Its better make difference between module math (allowed in interger field only) and module computing (where float are allowed also)... We here saying of module computing
4th Jun 2018, 9:35 AM
KrOW
KrOW - avatar
0
plz explain in a more simple way
4th Jun 2018, 10:31 AM
Bunny
Bunny - avatar
0
You don't need it for floats, because they can represent a number with decimals integer cant do that, they just cut of the part. This part, which is cut off (rest), can be obtained by the modulus operator example: For floats or doubles: 3/2 = 1.5 (exact result, no rest) 13/5 = 2.6 (exact) For integer: 3/2 = 1 (rest 1, because 2*1 + 1 =3) 13/5 = 2 (rest 3, because 5*2 + 3 = 13) remember how you started learning to divide in primary school
4th Jun 2018, 10:44 AM
Matthias
Matthias - avatar
0
Matthias thnx
4th Jun 2018, 3:26 PM
Bunny
Bunny - avatar