develooper Front page | perl.perl5.porters | Postings from November 2000

Re: looks_like_number (was Re: SvOOK_on)

Thread Previous | Thread Next
Nicholas Clark
November 28, 2000 03:52
Re: looks_like_number (was Re: SvOOK_on)
Message ID:
On Tue, Nov 28, 2000 at 09:26:27AM +0000, Nick Ing-Simmons wrote:
> Nicholas Clark <> writes:
> >
> >I can't think any situation where (int)atof() would give you an answer
> >better than atol() for a number matching /[-+]?[0-9]+/
> It does if number does not fit in an integer. 

Aargh. atol's behaviour is undefined if number is out of range. Which means
that the man page isn't quite helpful for writing portable programs if
it says behaviour is as strtol. strtol and strtoll are defined as the man
page says:

     The strtol() function returns the result of the conversion, unless the
     value would underflow or overflow.  If an underflow occurs, strtol() re-
     turns LONG_MIN. If an overflow occurs, strtol() returns LONG_MAX. In both
     cases, errno is set to ERANGE.

Can we rely on that? That's the casting behaviour we want, isn't it?

I'm concerned that a string of something like (LONG_LONG_MAX-2) would get
parsed and rounded by atof() to a value > (NV) LONG_LONG_MAX
[can I trust floating point "==" ?] and will slip through as an NV when it
should be possible to convert it to an IV.

Nicholas Clark

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About