简体   繁体   中英

how to calculate the float reminder for the two float values

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

ie 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?

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

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

Try out

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

Hope this helps.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM