Need help understanding this C function for Perlin noise vector generation

45 Views Asked by At

I found this C code on wikipedia. I am new to C. In this C code, why is the value of random not 0 if this (3.14159265 / ~(~0u >> 1)) is always 0 according to this test I wrote (second piece of code). Wouldn't multiplecation by 0 be zero?

typedef struct {
    float x, y;
} vector2;

/* Create pseudorandom direction vector
 */
vector2 randomGradient(int ix, int iy) {
    // No precomputed gradients mean this works for any number of grid coordinates
    const unsigned w = 8 * sizeof(unsigned);
    const unsigned s = w / 2; // rotation width
    unsigned a = ix, b = iy;
    a *= 3284157443; b ^= a << s | a >> w-s;
    b *= 1911520717; a ^= b << s | b >> w-s;
    a *= 2048419325;
    float random = a * (3.14159265 / ~(~0u >> 1)); // in [0, 2*Pi]
    vector2 v;
    v.x = cos(random); v.y = sin(random);
    return v;
}

The test

int main(){
     float w = (3.14159265 / ~(~0u >> 1));
     printf("%f\n", w);
}

I have tried the test. Sorry for grammar errors. English is not my best launguage.

Link to page: https://en.wikipedia.org/wiki/Perlin_noise

0

There are 0 best solutions below