English 中文(简体)
两个边框之间值的百分比计算方法
原标题:Method for calculating percentage for a value between two bounds

我有以下计算方法:

float value = MY_VALUE;

float percentage = (value - MIN)/(MAX - MIN);
if (percentage < 0)
   percentage = 0;
if (percentage > 1)
   percentage = 1

我只是想知道,在目标C或C中是否有某种方法可以帮助实现这一点,例如,一种在MIN和MAX界限之间环绕“价值”浮点的方法。

最佳回答

没有 No.

换句话说,你也许应该更新你的代码, 这样你就可以防止 可能的除以- 零:

float den = (value - MIN);
float num = (MAX - MIN);
float percentage;

if(den) {
  percentage = num / den;
  if (percentage < 0.0f ) {
    percentage = 0.0f;
  } else if (percentage > 1.0f) {
    percentage = 1.0f;
  } else {
    printf("Percentage: %3.2f",percentage);
  }
} else {
  percentage = 0.0f;
  printf("DIVIDE BY ZERO ERROR!");
}

还有,小心。Oracle正在起诉Google 一个非常相似的代码。

http://developers.slashdot.org/story/12/05/1612228/judget-to-oracle-a-high-school-worte-rangecheck

问题回答

标准C图书馆没有这种方法(我不知道目标C)。

如果您想要计算目标- c 语法中的百分比, 这就是前进的方向 。

(I think Dogbert got it wrong (opposite) on line 6). Here s what I did and it works just as wanted..

- (CGFloat)calculateProgressForValue:(CGFloat)value start:(CGFloat)start end:(CGFloat)end
{    
    CGFloat diff = (value - start);
    CGFloat scope = (end - start);
    CGFloat progress;

    if(diff != 0.0) {
        progress = diff / scope;
    } else {
        progress = 0.0f;
    }

    return progress;
}

你总是可以把限制限制在 0. 0 或 1. 0 上。

- (CGFloat)progressConstrained {

    // example values
    CGFloat start = 80;
    CGFloat end = 160;
    CGFloat a = 100;

    // the constrain
    return MAX(0.0, MIN(1.0, [self calculateProgressForValue:a start:start end:end]))
}

如果您只想确保一个值不高于或低于其他值, 您应该使用 MAX(a, b) 和 MIN(a, b)...

float maxValue = 1.0;
float minValue = 0.0;
float myValue = 2.8;
float myValueConstrained = MAX(minValue, MIN(maxValue, myValue)); // 1.0

MIN(a,b) 需要最小的a和b

MAX(a,b) 最大为a和b


我通常只是喜欢这样:

float myValueConstrained = MAX(0.0, MIN(1.0, myValue));




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签