Re: weird problem





On Jan 27, 12:57 am, j...@xxxxxxxxxxx (Jens Thoms Toerring) wrote:


I hope I didn't miss anything essential. Now again, this time with
comments.






Now you print some text into 'o_tail->x' - please look again at what
you are doing here. This isn't 'tmp->x' (as you told in a different
post, 'o_tail' is the last element of the linked list and that's not
what 'tmp->x' is except possibly in a single case). It seems at least
strange that you put some random value into the last element of the
linked list and don't use the newly allocated memory, pointed to by
'tmp->x' for that purpose, especially considering what's happening
later.

Aaaaargh. How stupid of me. I can't believe i did not notice that. This
explains
a lot :-)

| while (search != NULL) {
|
| if (collisions>0 &&
| collisions ==((grid_size - grid_start) * (grid_size -
| grid_start)))

I already told you that the comparison between an integer and a float
value for equality is rather likely not to work - you have to round
the result of the floating point calculation to the nearest integer
before you can do a meaningful comparison.

I'm going to use decimals. But maybe not testing for equality, but
would it not
be possible to do 1 > 00000000.2 for example ? The reason why i
initially used
floats, is because, although i divided by ^grid in squares, i wanted .
precision
because i might want to later overlap certain squares.



And now a question about something else: why do you use floating
point numbers for your grid points when you obviously only need
integers?

See above answer.

You always round them anyway (by using the "%.0f" con-
version specifier), so why use floats at all?

I rounded them off by %.1f so i would still have 10 units of floating
point precision.

Lots of things are
probably going to be quite a bit simpler if you use integers in-
stead.

I agree. There is another reason i used floats, but i have to look that
up.


And now concerining the 'sizeof(float) + 7' thing from above (we al-
ready had an exchange about that in comp.unix.programmer and I am
going to repeat parts of what I already wrote in the hope that it
gets through this time). You asked:

I was under the wrong impression that i need to give snprintf a number
of how many bytes i wanted to copy from the variable in question, be it
int,float, char, etc. At least, that's what the manpages told me. The
+7 was used to reserve some extra bytes for . precision. What would you
use then to copy a float into a char *1. You don't "copy" a float into a char. A float is a floating point
number in a binary representation. A floating point value in a
binary representation doesn't resemble a string like "123.46343"
im any way at all, so there's no way you could "copy" them to a
string. What you do via e.g. snprintf() is create a string repre-
sentation of that value. While a float always has the same number
of bytes (typically something in the order of 4 or 8) the string
representations of different floating point numbers can vary
widely in size - just compare e.g. "0.1" and "123422313123231.0",
both are perfectly valid floating point numbers but the first
you only need 3 characters while for second you need 17 chars
(if I counted correctly;-).

2. How many chars you need for the string representation of a float
depends on its value. If the value is e.g. 1.0e37 and you use
the "%.0f conversion specifier you will probably need 40 chars
for the string (plus one for the trailing '\0') since it will
end up as something like "10000000000000000000000000000000000000.".
You can only find out directly how many you will need by taking
the logarithm to base 10 plus 2 more (aand that for the case that
its a positive number larger than 1, there are more cases to be
taken care of). It's simpler if you have an implementation that
is C99 compliant at least for the snprintf() function, you then
can use the return value of

snprintf( NULL, 0, "%.0f", d );

(where 'd' is the value you want to represent as a string). That's
right, give it a NULL pointer as the place to print to and tell
it not to print out anything by setting the second argument to 0.
The return value then tells you how much space it would take to
print out the whole number and you then only need to add 1 for
the trailing '\0'.

I did not know that. I have snprintf so that should work fine.



Regards, Jens

PS: Please be so kind to follow the convention normally used in
technical newsgroups and don't top-post. Top-posting means
putting your text before what you are replying to. This is
rather annoying since it makes it hard to figure our what you
are reacting to. So please try to put your replies below a
citation of what you are reacting to, but make sure you only
cite what is still relevant to your reply. That way the readers
get an idea what's it all about without having to sift through
long and partly irrelevant texts. This, in turn, will increase
the likelyhood of getting useful replies to your questions.

Personally i don't mind to read top-posts, i just scroll from up to
down instead of the other way
around. Besides, i usually want to know the answer instead of the
question so this save me
alot of time. If i think the answer is interesting, i back up. I think
the whole top-posting thing is
getting way out of hand. As long as a post is readable and not written
by a 5 year old, it should
be ok by my standards.

I also personally get quite agitated by people pushing down their
usenet etiquette down
my throat (i don't mean specifically you, but i mean those who do not
seem to have a life and
actually suspect are usenet bots :-). I think there is something to say
for both methods, be it top
posting or bottom posting. Having said that, i will conform with the
majority and top-post.
Halleluja :-)

Quoting properly is a different matter. My apologies if i did not
citate properly. Since google groups
the new version went live i now use this as it is noticably faster (i
do not have to wait 5 days for a post
to be posted) but the quoting system leaves something to be desired. I
will probably revert to my usenet
agent again.

--
\ Jens Thoms Toerring ___ j...@xxxxxxxxxxx
\__________________________ http://toerring.de

Thanks for your answers. They where really helpful.
Kind regards,
-alef

.



Relevant Pages

  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: numpy: handling float(NaN) different in XP vs. Linux
    ... Microsoft Windows XP ... ValueError: invalid literal for float(): NaN ... Convert a string or a number to floating point. ...
    (comp.lang.python)
  • Re: Converting floats to Strings and back
    ... > I'm trying to convert a String to a float, do some arithmetic on it, then ... > The code works fine until I exceed 8 digits. ... the nasty things that happen with floating point, ...
    (comp.lang.java.programmer)
  • Re: hexadecimal to float conversion
    ... > number and would like to convert it into float. ... are using scanf() you apparently have a text string. ... floating point values in some hex format. ...
    (comp.lang.c)
  • Re: a newbie question.
    ... After compiled by Visual Studio, the above program can input a. ... You should not assign anything to it, because it might be stored in a place in memory to which your program cannot write, at which point it will crash (this idea can be summarized easily: never cast away the constness of string literals). ... Have a look at mallocand freeinstead, or just declare an array of char, eg. char s. ...
    (comp.lang.c)