简体   繁体   中英

C unsigned long long miscalculation

I don't even know how to name this problem, so first I will present some code:

void hsvm2dcd72f(const hsvm_t *src, dcd72f_t *result, unsigned long long range)
{
    int h, s, v;
    int kh, ks, kv;
    int i;
    int indexes[72];
    int values[72];
    unsigned long long sum = 0;
    unsigned long long temp = 0;

    for (i = 0; i < 72; i++)
    {
        result->values[i] = 0;
        values[i] = 0;
    }

    for (i = 0; i < src->length; i++)
    {

        h = (int) src->content[i].h;
        s = (int) src->content[i].s;
        v = (int) src->content[i].v;

        // kwantyzacja H
        if ((h >= 316) || (h < 20))
            kh = 0;
        else if ((h >= 20) && (h < 40))
            kh = 1;
        else if ((h >= 40) && (h < 75))
            kh = 2;
        else if ((h >= 75) && (h < 155))
            kh = 3;
        else if ((h >= 155) && (h < 190))
            kh = 4;
        else if ((h >= 190) && (h < 270))
            kh = 5;
        else if ((h >= 270) && (h < 295))
            kh = 6;
        else if ((h >= 295) && (h < 316))
            kh = 7;

        // kwantyzacja S
        if ((s >= 0) && (s <= 20))
            ks = 0;
        if ((s > 20) && (s <= 70))
            ks = 1;
        if ((s > 70) && (s <= 100))
            ks = 2;

        //kwantyzacja V
        if ((v >= 0) && (v <= 20))
            kv = 0;
        if ((v > 20) && (v <= 70))
            kv = 1;
        if ((v > 70) && (v <= 100))
            kv = 2;

        values[9 * kh + 3 * ks + kv]++;
    }

    quicksorti((int*) &indexes, (int*) &values, 72);

    //norm
    for (i = 72 - 8; i < 72; i++)
        sum += (unsigned long long) values[indexes[i]];

    unsigned long long temp1 = 65535;
    temp = (unsigned long long) range;
    if(temp == temp1)
        printf("ok");
    for (i = 72 - 8; i < 72; i++)
    {
        result->values[indexes[i]] = values[indexes[i]]*temp1 / sum;
    }
}

struct dcd72f // result struct
{
    unsigned long long values[72]; /**< dcd descriptor values. */
};
typedef struct dcd72f dcd72f_t;

It shows "ok" after the comparison so temp1 and temp are equal (that's something I was aiming for), but according to variable I am getting different results in result->values[indexes[i]] , with temp and temp1 . The temp1 results are correct ones but I need temp to be flexible.

I am using GCC with eclipse on Windows. Any idea how to solve this?

Edit: printf("%llu\\n", temp); and printf("%llu\\n", temp1); both return 65535...

You have never compare two floats in a condition. Real numbers in a range are infinite, but a float-double are not. It has infinite blanks and they give you an approximation to the value. The comparator == is to binary level. This must be your problem.

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