[...] If someone runs the
program with no non-'-' command-line arguments, this test takes
you to Never-Never Land because you're trying to inspect a pointer
that isn't in the argv array, has no defined value, and may
not even exist!
Is it possible to run a program with no non-'-' arguments? The
program name will always be there, won't it? Of course, I see that I
skip over that anyway with the initial ++argv in the while loop.
Yes, the program name will always be there (it might be the
empty string "" if no name can be determined), but suppose
that's the *only* thing that's there. Then the *++argv in the
first loop will find the NULL at the end of the two-place array
{name, NULL}, and the *++argv in the next test will walk right
off the end.
I suppose I should just test it "if (*(argv) == NULL)" then?
That would work: "Look before you leap." An alternative
would be to use the argc value, which tells you the total
number of command-line arguments.
A simpler test for "Is there anything after the semicolon?"
is to ask whether the next character is the end-of-string '\0'
or is something else: `if (s[endpoint+1] != '\0')'.
Much simpler, no function call overhead. Fixed.
It's not about the overhead: Even though there is some, it
is likely[*] to be so tiny as to be difficult to measure. The
real improvement comes from the better expression of intent:
You really don't care about the length of the string, but about
whether the string continues or doesn't continue beyond the
semicolon -- so write your test that way. Don't weigh the car
to find out how many passengers are in it; look through the
window.
[*] I once had the experience of working on a machine where
the overhead was LARGE and could not be neglected. Is it any
wonder that the maker of that machine is no longer in business?
There can't be, as I understand it, because that would mean much more
precision than the data type can handle. I'll write in error checking
on it later.
Take your own route, do what works for you. In close to
forty years of writing programs in dozens of languages, it's
always -- always! -- seemed to me that handling the exceptional
cases is best thought about first, not last. One big reason
is that it's not enough just to detect a strangeness: You must
also choose how to respond to it. That choice may have profound
effects on the overall program structure and on the definitions
of your functions/subroutines/lambdas/methods, and is the sort
of thing that's hard to "stick on" at the end.
Yes; I should pad it with zeroes, and will do so.
Ah, but is there enough room in the string? Those command-
line arguments, for example: If one of them is "12;3e7" you can
only be sure of seven characters (including the '\0'). If you
try to store the eleven characters of "123000000;" into the
space that suffices for seven, ...
Ha; I actually thought of that one.
They're padded on the left
with zeroes, and then semicolon inserted later. That's what
is for. I'm not sure if this actually works (in fact, looking at it I
think it doesn't), but that's what I was going for. I'll test it out
tonight.
I was running out of steam when I got to that loop, and didn't
look at it closely (still haven't). But again: How do you know
there's enough room for all those zeroes? The seven characters
of "12;e-7" won't hold the nine characters of ";0000012".
It is; I'm looking for the maximum possible accuracy. Eliminating all
those small inaccuracies is something I'm very interested in.
Okay. A full-scale job that squeezes the last little bit of
accuracy out of the conversion is, to coin a phrase, "non-trivial."
Pointless initialization: The initial value will never be
used, so you might as well initialize to `-2.71828'. Or to
nothing. As it is, specifying an initial value fools the reader
into thinking that the value is important -- when in fact it is
completely UNimportant.
Well, divisor *has* to start at 1.0, doesn't it? [...]
Sorry if I was unclear: This comment was aimed only at the
single line that preceded it. Yes, divisor's value is used --
but fracval's is not, and that's the useless initialization.