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

Glynn Clements glynn at gclements.plus.com
Tue Feb 26 07:44:12 EST 2008


Ivan Shmakov wrote:

> >>>>> Maciej Sieczka <tutey at o2.pl> writes:
> 
> [...]
> 
>  >> I could change it to use strtol(), which sets errno to ERANGE on
>  >> overflow.
> 
> 	It may be better to check `*tailptr', like:

There's no point. The string which is being passed to atoi() is lex's
yytext variable, which is guaranteed to consist solely of decimal
digits:

	I		[0-9]+
	
	...
	
	{I}		{
				yylval.ival = atoi(yytext);
				return INTEGER;
			}

strtol() only stops when it encounters an invalid character; it won't
stop just because it has read more than 9 digits. So, the endptr
argument will always point to the terminating NUL.

The issue is that atoi() doesn't provide any indication that it
clamped the result to INT_MAX (or INT_MIN), while strtol() sets errno.

>  > Would that make r.mapcalc accept integers bigger than int32?
> 
> 	The sizes of the C types generally depend on the platform, but
> 	for x86-based platforms I'm familiar with, sizeof (long) is 32.
> 
> 	It may make sense to use `long long' and strtoll () where
> 	available.

You can't store anything larger than an "int" in a CELL map, so there
isn't much point in supporting 64-bit integers within r.mapcalc. If
you need more digits for intermediate values, you may as well just use
a double and convert the final result with the int() function.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list