繁体   English   中英

如何计算两个浮点值的浮点提醒

[英]how to calculate the float reminder for the two float values

我有两个浮点值,'a' 和 'b'。

我需要计算这两个float值的提醒,而且必须是float值。

float a = 1.1;
float b = 0.5;

所以余数'r'应该是准确的值

即 r = a % b

r = 1.1% 0.5

  0.5) 1.1 (2
       1.0
     ______

       0.1

  r = 0.1

但它会导致浮点值的错误操作数无效。

怎么做?

在 C、C++ 和 Objective-C 中将是fmod

使用fmod()

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

如果您使用的是 linux/unix/mac-osx/,请不要忘记 -lm liker 标志。

了解更多信息

$man fmod

试用

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

希望这可以帮助。

你声明了吗?

float r;

在进行任何计算之前你必须这样做

所以

float r;

float a = 1.1;
float b = 0.5;

r = a % b;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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