[英]how to calculate the float reminder for the two float values
I have two float values, 'a' and 'b'.我有两个浮点值,'a' 和 'b'。
I need to calculate the reminder of these two float values, and it must be a float value.我需要计算这两个float值的提醒,而且必须是float值。
Let让
float a = 1.1;
float b = 0.5;
So the remainder 'r' should be accurate value所以余数'r'应该是准确的值
ie r = a % b即 r = a % b
r = 1.1 % 0.5 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?怎么做?
use fmod()使用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/.如果您使用的是 linux/unix/mac-osx/,请不要忘记 -lm liker 标志。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.