[GRASS-dev] r.mapcal rand() strangeness

Maciej Sieczka tutey at o2.pl
Sun Feb 24 08:01:16 EST 2008


Maciej Sieczka pisze:
> Glynn Clements pisze:
>> Maciej Sieczka wrote:

>>> $ r.mapcalc 'map=rand(-2147483648,2147483647)'

>>     res[i] = (lo == hi) ? lo : lo + x % (hi - lo);
>>
>> The expression (hi - lo) is overflowing the range of a signed integer.

Are all GRASS modules limited to Int32, ie. -2147483648 to 214748364? I 
can see r.mapcalc silently forces 214748364 for anything bigger...

>> 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?

I forgot to write I tested it and confirm it seems OK.

>> 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