473,883 Members | 1,523 Online

Fibonacci number

How to generate fibonacci mubers in C ?
Nov 14 '05
62 5470
la************@ ugsplm.com wrote:
In comp.std.c CBFalconer <cb********@yah oo.com> wrote:

That makes no sense to me. If -2 is handled with modulo
arithmetic, so should a string representing ULONG_MAX + 2.

Why? Consider the following:

/* assuming ULONG_MAX == 4294967295 */
unsigned long ul1 = -2UL; // perfectly good, ul1 = 4294967294
unsigned long ul2 = 4294967297UL; // error, overflow

The first is actually -(2UL), which has been specifically defined
to be interpreted with modulo arithmetic. However the string "-2"
represents a value outside the range of an unsigned long. The
purpose of error detection in strtoul() is to detect the supply of
an invalid value by the user.

I would suggest that the proper interpretation of that string is
to set endptr to point after the 2 (which may be to a '\0'),
return ULONG_MAX, and set errno to ERANGE.

To achieve the modulo interpretation, the programmer should have
to write:

unsigned long u;

u = -2;
or
u = strtol("-2", NULL, 10);

which last will have exactly the same values to use in the actual
assignment.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
Nov 14 '05 #21
CBFalconer wrote:
... the string "-2"
represents a value outside the range of an unsigned long. The
purpose of error detection in strtoul() is to detect the supply of
an invalid value by the user.

I would suggest that the proper interpretation of that string is
to set endptr to point after the 2 (which may be to a '\0'),
return ULONG_MAX, and set errno to ERANGE.

Basically, the argument is for a change of the standard to disallow the
optional - sign. I agree, and Doug implied earlier that such an
interpretation would make sense if we start defining the strtoul
function from scratch. Since that would be a quiet change for working
programs that don't check ERRNO, and it is easy to avoid in the program
by testing, I don't expect it to fly.

Another example: application programs which accept 012 as a user input
and convert to 10, not the intended 12.

I chalk both problems up to baggage that C carries around because it is
has a legacy. The practical alternatives are to add your own checks or
to use non-Standard-C robust input routines.

Nov 14 '05 #22

CBFalconer wrote:
... the string "-2"
represents a value outside the range of an unsigned long. The
purpose of error detection in strtoul() is to detect the supply of
an invalid value by the user.

I would suggest that the proper interpretation of that string is
to set endptr to point after the 2 (which may be to a '\0'),
return ULONG_MAX, and set errno to ERANGE.

Basically, the argument is for a change of the standard to disallow the
optional - sign. I agree, and Doug implied earlier that such an
interpretation would make sense if we start defining the strtoul
function from scratch. Since that would be a quiet change for working
programs that don't check ERRNO, and it is easy to avoid in the program
by testing, I don't expect it to fly.

Well, not quite. I am willing to face the idea that - signs are
accepted on input, and that the string is then parsed up to the
first non-digit (per base). I could even be persuaded to accept
the modulo arithmetic. But errno should be set to ERANGE for all
of them. Then it is possible to detect user errors. The routine
can be used as the basis for strtol in addition.

As it is I cannot use strtoul to input from somewhere, and either
write out the same value or announce some form of input error.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
Nov 14 '05 #23
CBFalconer wrote:

CBFalconer wrote:
> ... the string "-2"
> represents a value outside the range of an unsigned long. The
> purpose of error detection in strtoul() is to detect the supply of
> an invalid value by the user.
>
> I would suggest that the proper interpretation of that string is
> to set endptr to point after the 2 (which may be to a '\0'),
> return ULONG_MAX, and set errno to ERANGE.

Basically, the argument is for a change of the standard to disallow the
optional - sign. I agree, and Doug implied earlier that such an
interpretation would make sense if we start defining the strtoul
function from scratch. Since that would be a quiet change for working
programs that don't check ERRNO, and it is easy to avoid in the program
by testing, I don't expect it to fly.

Well, not quite. I am willing to face the idea that - signs are
accepted on input, and that the string is then parsed up to the
first non-digit (per base). I could even be persuaded to accept
the modulo arithmetic. But errno should be set to ERANGE for all
of them. Then it is possible to detect user errors. The routine
can be used as the basis for strtol in addition.

As it is I cannot use strtoul to input from somewhere, and either
write out the same value or announce some form of input error.

You have the input in the first place. All you'd have to do is check the
first character for a '-' (for the specific case you mention), once you get
a return and check endptr and errno for other possible errors in the input.
This doesn't seem too unreasonable to me, once you take the step of
accepting modulo arithmetic from strtoul.

-nrk.

--
Remove devnull for email
Nov 14 '05 #24
nrk wrote:
CBFalconer wrote:
CBFalconer wrote:

> ... the string "-2"
> represents a value outside the range of an unsigned long. The
> purpose of error detection in strtoul() is to detect the supply of
> an invalid value by the user.
>
> I would suggest that the proper interpretation of that string is
> to set endptr to point after the 2 (which may be to a '\0'),
> return ULONG_MAX, and set errno to ERANGE.

Basically, the argument is for a change of the standard to disallow
the optional - sign. I agree, and Doug implied earlier that such
an interpretation would make sense if we start defining the strtoul
function from scratch. Since that would be a quiet change for
working programs that don't check ERRNO, and it is easy to avoid in
the program by testing, I don't expect it to fly.

Well, not quite. I am willing to face the idea that - signs are
accepted on input, and that the string is then parsed up to the
first non-digit (per base). I could even be persuaded to accept
the modulo arithmetic. But errno should be set to ERANGE for all
of them. Then it is possible to detect user errors. The routine
can be used as the basis for strtol in addition.

As it is I cannot use strtoul to input from somewhere, and either
write out the same value or announce some form of input error.

You have the input in the first place. All you'd have to do is check
the first character for a '-' (for the specific case you mention),
once you get a return and check endptr and errno for other possible
errors in the input. This doesn't seem too unreasonable to me, once
you take the step of accepting modulo arithmetic from strtoul.

It is not quite that simple. You also have to scan off any
is a legal input field for strtoul, what about "--2"?

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
Nov 14 '05 #25
In article <news:40******* ********@yahoo. com>
CBFalconer <cb********@wor ldnet.att.net> writes:
It is not quite that simple. You also have to scan off any
True (provided you do want to allow leading whitespace;
otherwise a simple isdigit() test will suffice for bases
up to 10). If you are in the C locale and have a pointer
to the start of the string, you can skip whitespace with:

s += strspn(s, " \t\n\r\f\v\b");
And if "-2" is a legal input field for strtoul, what about "--2"?

Read the strtoul() specification (or below).

This text is from the BSD/OS strtol() manual page:

The string may begin with an arbitrary amount of white space (as deter-
mined by isspace(3)) followed by a single optional `+' or `-' sign. If
base is zero or 16, the string may then include a `0x' prefix, and the
number will be read in base 16; otherwise, a zero base is taken as 10
(decimal) unless the next character is `0', in which case it is taken as
8 (octal).
...
Upon success the strtoul() and strtoull() functions return either the re-
sult of the conversion or, if there was a leading minus sign, the nega-
tion of the result of the conversion, unless the original (non-negated)
value would overflow. In the case of an overflow the functions return
ULONG_MAX and UQUAD_MAX respectively and the global variable errno is set
to ERANGE.

(Note that C99 uses ULLONG_MAX, not UQUAD_MAX. Our strtoull()
predates C99 and was originally spelled strtoq(). I originally
wrote this manual page for 4.3BSD, where we used "quad_t" as the
name for 64-bit integers starting in the early 1990s.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #26
CBFalconer wrote:
nrk wrote:
CBFalconer wrote:
>> CBFalconer wrote:
>>
>> > ... the string "-2"
>> > represents a value outside the range of an unsigned long. The
>> > purpose of error detection in strtoul() is to detect the supply of
>> > an invalid value by the user.
>> >
>> > I would suggest that the proper interpretation of that string is
>> > to set endptr to point after the 2 (which may be to a '\0'),
>> > return ULONG_MAX, and set errno to ERANGE.
>>
>> Basically, the argument is for a change of the standard to disallow
>> the optional - sign. I agree, and Doug implied earlier that such
>> an interpretation would make sense if we start defining the strtoul
>> function from scratch. Since that would be a quiet change for
>> working programs that don't check ERRNO, and it is easy to avoid in
>> the program by testing, I don't expect it to fly.
>
> Well, not quite. I am willing to face the idea that - signs are
> accepted on input, and that the string is then parsed up to the
> first non-digit (per base). I could even be persuaded to accept
> the modulo arithmetic. But errno should be set to ERANGE for all
> of them. Then it is possible to detect user errors. The routine
> can be used as the basis for strtol in addition.
>
> As it is I cannot use strtoul to input from somewhere, and either
> write out the same value or announce some form of input error.

You have the input in the first place. All you'd have to do is check
the first character for a '-' (for the specific case you mention),
once you get a return and check endptr and errno for other possible
errors in the input. This doesn't seem too unreasonable to me, once
you take the step of accepting modulo arithmetic from strtoul.

It is not quite that simple. You also have to scan off any
is a legal input field for strtoul, what about "--2"?

Right. Let's take this one thing at a time. "--2", we know, according to
specification, doesn't qualify as an integer for either strtol or strtoul
since both allow exactly *one* optional sign preceding the number (see my
recent thread on a broken implementation of strtol that doesn't grok this
correctly). So, if we do use endptr correctly to track the call to
strtoul, this case will be handled.

initial remedy to check for a '-' sign. Chris has pointed out how you can
skip leading white space if any. However, turns out that things are much
simpler than that if you merely wish to detect the presence of the '-'.
Use strchr and check the return against NULL and endptr! Thusly, we can
come up with something along the lines of (it is even simpler if the entire
string is supposed to be converted):

unsigned long safe_strtoul(co nst char *src, char **endptr, int base) {
unsigned long ret;
int old_errno = errno;
char *cptr;

errno = 0;
ret = strtoul(src, endptr, base);

if ( errno )
return ret; /* lets not bother checking anything */

if ( src == *endptr ) {
/* no conversion, throw back at user */
errno = old_errno;
return ret;
}

cptr = strchr(src, '-');
if ( cptr && cptr < *endptr ) {
/* conversion, but there was a '-' at start */
errno = ERANGE;
return ULONG_MAX;
/* I think at this point we've achieved the CBF strtoul */
}

errno = old_errno;
return ret;
}

-nrk.

--
Remove devnull for email
Nov 14 '05 #27
Another example: application programs which accept 012 as a user input
and convert to 10, not the intended 12.
I chalk both problems up to baggage that C carries around because it is
has a legacy.

And some of that legacy has origins that predate Unix and C.
One saw 012 = 10. and ASCIZ (null-terminated) strings in DEC
software well before C appeared.

Nov 14 '05 #28
CBFalconer wrote:
As it is I cannot use strtoul to input from somewhere, and either
write out the same value or announce some form of input error.

Sure you can, you just have to do part of the checking
that you seem to want for your application yourself.

Really good input checking and validation requires much
more than any Standard C library function provides.

Nov 14 '05 #29
In <AY************ ********@comcas t.com> "Douglas A. Gwyn" <DA****@null.ne t> writes:
Really good input checking and validation requires much
more than any Standard C library function provides.

And this is a deficiency of the C standard that the committee doesn't
bother to fix. Why should each robust C application have to invent its
own wheel?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #30

This thread has been closed and replies have been disabled. Please start a new discussion.