How to calculate a float for two float values

I have two float values, 'a' and 'b'.

I need to calculate a reminder of these two float values, and it should be a float value.

Let be

float a = 1.1; float b = 0.5; 

Thus, the remainder 'r' must be an exact quantity

i.e. r = a% b

r = 1.1% 0.5

  0.5) 1.1 (2 1.0 ______ 0.1 r = 0.1 

But this leads to an invalid operand error for float values.

How to do it?

+6
source share
4 answers

In C, C ++ and Objective-C this will be fmod .

+13
source

use fmod ()

 #include <math.h> double x,y,z; x = 1.1; y = 0.5; z = fmod(x,y) 

Remember to leave the -lm liker flag if you are using linux / unix / mac-osx /.

for more information

 $man fmod 
+5
source

Try

 float x = (float)(1.1 % 0.5); NSLog(@"%f",x); 

Hope this helps.

+2
source

Have you stated this?

 float r; 

you need to do this before you can do any calculations

So

 float r; float a = 1.1; float b = 0.5; r = a % b; 
+1
source

Source: https://habr.com/ru/post/912481/


All Articles