设置价值限制,iPhone

发布于 2024-08-13 02:09:40 字数 217 浏览 2 评论 0原文

我有以下代码:

float valueCalculated = (val1 / val2) * 100;

我想要做的是将 valueCalculated 的最大值限制为 100。

我相信我可以使用某种 if 语句来做到这一点,但这意味着更多的代码行。编辑//事实并非如此,请参阅下面的答案。

谢谢,

斯图

I have the following code:

float valueCalculated = (val1 / val2) * 100;

What I want to be able to do is to cap the maximum value of valueCalculated to 100.

I believe I could do this using some sort of if statement, but this would mean many more lines of code. Edit// This is not the case, see the answers below.

Thanks,

Stu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

筱果果 2024-08-20 02:09:41

你的意思是像这样“更多行代码”?

if (valueCalculated > 100) { valueCalculated = 100; }

You mean "many more lines of code" like this?

if (valueCalculated > 100) { valueCalculated = 100; }
心凉怎暖 2024-08-20 02:09:41
float valueCalculated = MIN((val1 / val2) * 100, 100);
float valueCalculated = MIN((val1 / val2) * 100, 100);
沧桑㈠ 2024-08-20 02:09:41
float valueCalculated = (float temp = (val1 / val2) * 100) > 100 ? 100 : temp;

但我想一个函数会更好,你可以使用函数或简单的宏来完成:

#define MAX(m, toset) toset = (toset > m ? m : toset)

MAX(100, valueCalculated);

我想我更好地理解你的问题:

valueCalculated = MAX(100, (val1 / val2) * 100);

希望

#define MAX(max, val) (((val) > (max)) ? (max) : (val))

它有帮助,

float valueCalculated = (float temp = (val1 / val2) * 100) > 100 ? 100 : temp;

but I guess a function would be better, you can do with function or a simple macro:

#define MAX(m, toset) toset = (toset > m ? m : toset)

MAX(100, valueCalculated);

I guess I understood better your question:

valueCalculated = MAX(100, (val1 / val2) * 100);

where

#define MAX(max, val) (((val) > (max)) ? (max) : (val))

hope it helps,
Joe

森林迷了鹿 2024-08-20 02:09:41

如果我理解这个问题,那么

float valueCalculated = fminf((val1 / val2) * 100, 100);

……应该按照你的要求去做。

但是,您是否担心负值,即 val1 或 val2 可能为负数,并且您是否要将 valueCalculated 的可能值限制为 0-100?如果是这样,那么您可能想要...

float valueCalculated = fmaxf(fminf((val1 / val2) * 100, 100), 0);

If I understand the question then...

float valueCalculated = fminf((val1 / val2) * 100, 100);

...should do what your asking.

However, are you worried about negative values, i.e. could val1 or val2 be negative and do you want to limit possible values for valueCalculated to 0-100?. If so then you might want...

float valueCalculated = fmaxf(fminf((val1 / val2) * 100, 100), 0);
吻风 2024-08-20 02:09:41

这个怎么样:

float valueCalculated = ((val1 / val2) * 100 < 100) ? (val1 / val2) * 100 : 100

What about this:

float valueCalculated = ((val1 / val2) * 100 < 100) ? (val1 / val2) * 100 : 100
执笔绘流年 2024-08-20 02:09:41

我不是 iphone 开发人员,但 modulo 不是您要找的吗?

float valueCalculated = ((val1 / val2) * 100) % 101;

I'm not an iphone dev, but isn't modulo what you're looking for?

float valueCalculated = ((val1 / val2) * 100) % 101;

?

淡淡的优雅 2024-08-20 02:09:41

如果您只想通过“加权”将计算值限制在 0 到 100 之间,则可以将该值除以可能的最大值,然后乘以 100。这假设该值不是负数。

If you just want to limit a calculated value to 0 to 100 by "weighting" it, you would divide that value by the maximum that it could be and then multiply by 100. This assumes the value isn't negative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文