how to calculate the float reminder for the two float values

10,303

Solution 1

In C , C++ and Objective-C that would be fmod.

Solution 2

use fmod()

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

Don't tforget the -lm liker flag if you are on linux/unix/mac-osx/.

for more info

$man fmod

Solution 3

Try out

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

Hope this helps.

Solution 4

did you declare it?

float r;

you have to do that before you could do any calculations

so

float r;

float a = 1.1;
float b = 0.5;

r = a % b;
Share:
10,303
Ranga
Author by

Ranga

Updated on June 14, 2022

Comments

  • Ranga
    Ranga almost 2 years

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

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

    Let

    float a = 1.1;
    float b = 0.5;
    

    So the remainder 'r' should be accurate value

    i.e. r = a % b

    r = 1.1 % 0.5

      0.5) 1.1 (2
           1.0
         ______
    
           0.1
    
      r = 0.1
    

    But it causes to an error invalid operand for float values.

    How to do it?