[GRASS-dev] r.mapcal rand() strangeness
Maciej Sieczka
tutey at o2.pl
Sun Feb 24 04:18:55 EST 2008
Glynn Clements pisze:
> Maciej Sieczka wrote:
>> $ r.mapcalc 'map=rand(-2147483648,2147483647)'
>>
>> $ r.stats -1 map | sort -n | uniq
>> 100%
>> -2147483647
>> -2147483646
>>
>> Why only 2 values from 4294967295 possible? Region is big enough to
>> accomodate more than 2 values from the range:
> res[i] = (lo == hi) ? lo : lo + x % (hi - lo);
>
> The expression (hi - lo) is overflowing the range of a signed integer.
>
> Also, x will be restricted to 0 <= x < 2^31, possibly less if the
> system doesn't have lrand48().
>
> The wrapping can be fixed by adding casts, i.e.:
>
> res[i] = (lo == hi) ? lo : lo + (unsigned) x % (unsigned) (hi - lo);
>
> The 31-bit limitation can be fixed by replacing lrand48() with
> mrand48(), which covers the full (signed) 32-bit range.
>
> The attached patch appears to work.
Will you apply it?
> One final point: -2^31 (= 0x80000000 = -2147483648) is the null value
> for the CELL type, so you'll never see that value in a map.
BTW, what are the NULL value for double and float?
Maciek
More information about the grass-dev
mailing list