lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I've been spending the weekend trying to gain a deeper understanding of noise functions and trying to see what interesting things I can get out of them. After getting through Perlin/Simplex noise and diving into Worley(Cell) Noise, I discovered what appears to be a bug with the LuaJIT BitOp library when dealing with exceedingly large numbers. Namely, i'm experiencing this bug with the BitOp.band function.

Here is the function that I am using to generate sudo random values (copied from the perlin noise site: http://freespace.virgin.net/hugo.elias/models/m_perlin.htm)

noise1d = function(x, r)
    local n = x * r
    n = bit.bxor(bit.lshift(n, 13), n)
    n = n * (n * n * 15731 + 789221) + 1376312589
    n = (1.0 - bit.band(n, 2147483647) / 1073741824.0)
    return n
end

More often than not, this function works as expected and returns the same output given the same input. But, randomly it fails to do so and so far i've seen 3 or 4 variations with the same input. I've narrowed it down to the following 2 lines:

    n = n * (n * n * 15731 + 789221) + 1376312589
    n = (1.0 - bit.band(n, 2147483647) / 1073741824.0)

If I take away the "+ 1376312589" in the first line above, the problem vanishes and the function works as expected (the problem seems to also vanish if I use print(n) in the function call, i had to make a global table and add things to it instead to see track variables then print it after everything was done).

I don't suspect this is a value overflow b/c then I would still be getting the same output given same input every time. I've taken the "+ 1376312589" out completely for now since I still get sudo random values, but the bug is there non the less.

If the entire code is necessary for anyone, i'll happily post I just thought this post was long enough already...

Here are a few input/output tables that I got (sorry if this comes through poorly formated): Columns are: x, r, value of n before the bit.band call, value returned by function

No addition (without the "+ 1376312589"), always returns the same output
1024                57    1.7203215615285e+30        0.42778676841408
1                    57    1.6021743062178e+21        0.42647737916559
2048                57    1.3762572492228e+31        0.4277867898345
2                    57    1.2817394447531e+22        -0.57254056073725
3072                57    4.6448682161269e+31        -0.17030489537865
3                    57    4.3258706259035e+22        -0.92042765114456
4096                57    1.1010057993782e+32        0.42778679262847
4                    57    1.0253915557582e+23        0.052704953588545
2048                57    1.3762572492228e+31        0.4277867898345
0.5                    57    1.8991500972246e+20        -0.22280406951904
4096                57    1.1010057993782e+32        0.42778679262847
1                    57    1.6021743062178e+21        0.42647737916559
6144                57    -4.5588237152993e+31    -0.24167815316468
1.5                    57    5.5027597176147e+21        0.57001950312406
8192                57    -1.6259021345399e+30    0.10560003388673
2                    57    1.2817394447531e+22        -0.57254056073725
3072                57    4.6448682161269e+31        -0.17030489537865
0.33333333333333    57    5.9339789228382e+19        0.011558014899492
6144                57    -4.5588237152993e+31    -0.24167815316468
0.66666666666667    57    4.7471831308992e+20        0.02203331887722
9216                57    1.1141983919572e+25        -0.27148926816881
1                    57    1.6021743062178e+21        0.42647737916559
12288                57    4.7319886470795e+31        -0.58460648916662
1.3333333333333        57    3.7977465032451e+21        0.024652144871652
4096                57    1.1010057993782e+32        0.42778679262847
0.25                57    2.3739376283201e+19        -0.23824135959148
8192                57    -1.6259021345399e+30    0.10560003388673
0.5                    57    1.8991500972246e+20        -0.22280406951904
12288                57    4.7319886470795e+31        -0.58460648916662
0.75                57    6.8784496491036e+20        0.56853779964149
16384                57    -1.3007217076319e+31    0.1056000161916
1                    57    1.6021743062178e+21        0.42647737916559

With addition and the most common output
1024                57    1.7203215615285e+30        0.42778676841408
1                    57    1.6021743062192e+21        0.42647248972207
2048                57    1.3762572492228e+31        0.4277867898345
2                    57    1.2817394447532e+22        -0.57254117168486
3072                57    4.6448682161269e+31        -0.17030489537865
3                    57    4.3258706259036e+22        -0.92042780388147
4096                57    1.1010057993782e+32        0.42778679262847
4                    57    1.0253915557583e+23        0.052704877220094
2048                57    1.3762572492228e+31        0.4277867898345
0.5                    57    1.8991500972383e+20        -0.22284318692982
4096                57    1.1010057993782e+32        0.42778679262847
1                    57    1.6021743062192e+21        0.42647248972207
6144                57    -4.5588237152993e+31    -0.24167815316468
1.5                    57    5.502759717616e+21        0.57001828029752
8192                57    -1.6259021345399e+30    0.10560003388673
2                    57    1.2817394447532e+22        -0.57254117168486
3072                57    4.6448682161269e+31        -0.17030489537865
0.33333333333333    57    5.9339789229759e+19        0.011401546187699
6144                57    -4.5588237152993e+31    -0.24167815316468
0.66666666666667    57    4.747183130913e+20        0.022013760171831
9216                57    1.1141983919572e+25        -0.27148926910013
1                    57    1.6021743062192e+21        0.42647248972207
12288                57    4.7319886470795e+31        -0.58460648916662
1.3333333333333        57    3.7977465032465e+21        0.024649700149894
4096                57    1.1010057993782e+32        0.42778679262847
0.25                57    2.3739376284578e+19        -0.23855429701507
8192                57    -1.6259021345399e+30    0.10560003388673
0.5                    57    1.8991500972383e+20        -0.22284318692982
12288                57    4.7319886470795e+31        -0.58460648916662
0.75                57    6.8784496491174e+20        0.56852802075446
16384                57    -1.3007217076319e+31    0.1056000161916
1                    57    1.6021743062192e+21        0.42647248972207

With addition and an unexpected output, maybe 1 in 10 times of generating the noise
1024                57    1.7203215615285e+30        0.42778676841408
1                    57    1.6021743062192e+21        0.42647248972207
2048                57    1.3762572492228e+31        0.4277867898345
2                    57    1.2817394447532e+22        -0.57254117168486
3072                57    4.6448682161269e+31        -0.17030489537865
3                    57    4.3258706259036e+22        -0.92042780388147
4096                57    1.1010057993782e+32        0.42778679262847
4                    57    1.0253915557583e+23        0.052704877220094
2048                57    1.3762572492228e+31        0.4277867898345
0.5                    57    1.8991500972383e+20        -0.22284318692982
4096                57    1.1010057993782e+32        0.42778679262847
1                    57    1.6021743062192e+21        0.42647248972207
6144                57    -4.5588237152993e+31    -0.24167815316468
1.5                    57    5.502759717616e+21        0.57001828029752
8192                57    -1.6259021345399e+30    0.10560003388673
2                    57    1.2817394447532e+22        -0.57254117168486
3072                57    4.6448682161269e+31        -0.17030489537865
0.33333333333333    57    5.9339789229759e+19        0.011401546187699
6144                57    -4.5588237152993e+31    -0.24167815316468
0.66666666666667    57    4.747183130913e+20        0.022013760171831
9216                57    1.1141983919572e+25        -0.27148926910013
1                    57    1.6021743062192e+21        -0.85531360469759
12288                57    4.7319886470795e+31        0.13360252697021
1.3333333333333        57    3.7977465032465e+21        0.74286116100848
4096                57    1.1010057993782e+32        -0.85400419123471
0.25                57    2.3739376284578e+19        0.47996765654534
8192                57    -1.6259021345399e+30    0.82380905002356
0.5                    57    1.8991500972383e+20        0.49540494661778
12288                57    4.7319886470795e+31        0.13360252697021
0.75                57    6.8784496491174e+20        -0.71325318422168
16384                57    -1.3007217076319e+31    0.82380903232843
1                    57    1.6021743062192e+21        -0.85531360469759

With addition, another unexpected output, probably 1 in 25 or so times of generating the noise
1024                57    1.7203215615285e+30        0.42778676841408
1                    57    1.6021743062192e+21        0.42647248972207
2048                57    1.3762572492228e+31        0.4277867898345
2                    57    1.2817394447532e+22        -0.57254117168486
3072                57    4.6448682161269e+31        -0.17030489537865
3                    57    4.3258706259036e+22        -0.92042780388147
4096                57    1.1010057993782e+32        0.42778679262847
4                    57    1.0253915557583e+23        0.052704877220094
2048                57    1.3762572492228e+31        0.4277867898345
0.5                    57    1.8991500972383e+20        -0.22284318692982
4096                57    1.1010057993782e+32        0.42778679262847
1                    57    1.6021743062192e+21        0.42647248972207
6144                57    -4.5588237152993e+31    -0.24167815316468
1.5                    57    5.502759717616e+21        0.57001828029752
8192                57    -1.6259021345399e+30    0.10560003388673
2                    57    1.2817394447532e+22        -0.57254117168486
3072                57    4.6448682161269e+31        -0.17030489537865
0.33333333333333    57    5.9339789229759e+19        0.011401546187699
6144                57    -4.5588237152993e+31    -0.24167815316468
0.66666666666667    57    4.747183130913e+20        0.022013760171831
9216                57    1.1141983919572e+25        -0.27148926910013
1                    57    1.6021743062192e+21        0.42647248972207
12288                57    4.7319886470795e+31        -0.58460648916662
1.3333333333333        57    3.7977465032465e+21        0.024649700149894
4096                57    1.1010057993782e+32        0.42778679262847
0.25                57    2.3739376284578e+19        -0.23855429701507
8192                57    -1.6259021345399e+30    0.10560003388673
0.5                    57    1.8991500972383e+20        -0.22284318692982
12288                57    4.7319886470795e+31        0.13360252697021
0.75                57    6.8784496491174e+20        -0.71325318422168
16384                57    -1.3007217076319e+31    0.82380903232843
1                    57    1.6021743062192e+21        -0.85531360469759