By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,723 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,723 IT Pros & Developers. It's quick & easy.

Some basic error - what am I missing? (NaNs)

P: n/a
Hi all...

Either I'm doing really something really stupid - or maybe there is
some bug somewhere (optimizing?). In a function I have:

int x1, y1, x2, y2;
float dR, dX, dY;

dR = (state.cam1x - x1) - (state.cam2x - x2);
dX = 0.5 * ((state.cam1x - x1) + (state.cam2x - x2));
dY = 0.5 * ((state.cam1y - y1) + (state.cam2y - y2));

printf("%d %d %d %d\n", state.cam1x, state.cam1y, state.cam2x,
state.cam2y);
printf("%d %d %d %d\n", x1, y1, x2, y2);
printf("%f %f %f\n", dX, dY, dR);

(state.xxx are declared globally, outside the function as integers)

I get:

342 224 343 225
339 215 340 215
nan nan nan

No warnings when compiling. I've looked into FAQs and other texts, no
luck...

John
Nov 14 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
John wrote:

Hi all...

Either I'm doing really something really stupid - or maybe there is
some bug somewhere (optimizing?). In a function I have:

int x1, y1, x2, y2;
float dR, dX, dY;

dR = (state.cam1x - x1) - (state.cam2x - x2);
dX = 0.5 * ((state.cam1x - x1) + (state.cam2x - x2));
dY = 0.5 * ((state.cam1y - y1) + (state.cam2y - y2));

printf("%d %d %d %d\n", state.cam1x, state.cam1y, state.cam2x,
state.cam2y);
printf("%d %d %d %d\n", x1, y1, x2, y2);
printf("%f %f %f\n", dX, dY, dR);

(state.xxx are declared globally, outside the function as integers)

I get:

342 224 343 225
339 215 340 215
nan nan nan

No warnings when compiling. I've looked into FAQs and other texts, no
luck...


I don't see anything wrong in the fragments you've
provided, so either I've missed something (it happens)
or the problem is in the parts you've omitted. Please
whittle your code down to the smallest complete program
that demonstrates the problem, and post that complete
program.

--
Er*********@sun.com
Nov 14 '05 #2

P: n/a
jo**@jcoppens.com (John) writes:
Either I'm doing really something really stupid - or maybe there is
some bug somewhere (optimizing?). In a function I have:

int x1, y1, x2, y2;
float dR, dX, dY;

dR = (state.cam1x - x1) - (state.cam2x - x2);
dX = 0.5 * ((state.cam1x - x1) + (state.cam2x - x2));
dY = 0.5 * ((state.cam1y - y1) + (state.cam2y - y2));

printf("%d %d %d %d\n", state.cam1x, state.cam1y, state.cam2x,
state.cam2y);
printf("%d %d %d %d\n", x1, y1, x2, y2);
printf("%f %f %f\n", dX, dY, dR);

(state.xxx are declared globally, outside the function as integers)

I get:

342 224 343 225
339 215 340 215
nan nan nan

No warnings when compiling. I've looked into FAQs and other texts, no
luck...


Please post a minimal but complete program which demonstrates the problem.
The program below prints

342 224 343 225
339 215 340 215
3.000000 9.500000 0.000000

on my system.

Martin

#include <stdio.h>

int main (void)
{
struct {
int cam1x;
int cam1y;
int cam2x;
int cam2y;
} state = {
342, 224, 343, 225
};

int x1 = 339, y1 = 215, x2 = 340, y2 = 215;
float dR, dX, dY;

dR = (state.cam1x - x1) - (state.cam2x - x2);
dX = 0.5 * ((state.cam1x - x1) + (state.cam2x - x2));
dY = 0.5 * ((state.cam1y - y1) + (state.cam2y - y2));

printf("%d %d %d %d\n", state.cam1x, state.cam1y,
state.cam2x, state.cam2y);
printf("%d %d %d %d\n", x1, y1, x2, y2);
printf("%f %f %f\n", dX, dY, dR);

return 0;
}
Nov 14 '05 #3

P: n/a
John wrote:
Hi all...

Either I'm doing really something really stupid - or maybe there is
some bug somewhere (optimizing?). In a function I have:

int x1, y1, x2, y2;
float dR, dX, dY;

dR = (state.cam1x - x1) - (state.cam2x - x2);
dX = 0.5 * ((state.cam1x - x1) + (state.cam2x - x2));
dY = 0.5 * ((state.cam1y - y1) + (state.cam2y - y2));

printf("%d %d %d %d\n", state.cam1x, state.cam1y, state.cam2x,
state.cam2y);
printf("%d %d %d %d\n", x1, y1, x2, y2);
printf("%f %f %f\n", dX, dY, dR);

(state.xxx are declared globally, outside the function as integers)

I get:

342 224 343 225
339 215 340 215
nan nan nan

No warnings when compiling. I've looked into FAQs and other texts, no
luck...


Your problems lie outside of the information you have given us:

#include <stdio.h>
struct
{
int cam1x, cam1y, cam2x, cam2y;
} state = { 342, 224, 343, 225};
int main(void)
{
int x1 = 339, y1 = 215, x2 = 340, y2 = 215;
float dR, dX, dY;

dR = (state.cam1x - x1) - (state.cam2x - x2);
dX = 0.5 * ((state.cam1x - x1) + (state.cam2x - x2));
dY = 0.5 * ((state.cam1y - y1) + (state.cam2y - y2));

printf("[output]\n");
printf("%d %d %d %d\n", state.cam1x, state.cam1y, state.cam2x,
state.cam2y);
printf("%d %d %d %d\n", x1, y1, x2, y2);
printf("%f %f %f\n", dX, dY, dR);
return 0;
}
[output]
342 224 343 225
339 215 340 215
3.000000 9.500000 0.000000
--
Martin Ambuhl
Nov 14 '05 #4

P: n/a
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<qN*****************@newsread1.news.atl.earth link.net>...
[output]
342 224 343 225
339 215 340 215
3.000000 9.500000 0.000000


Thank you both for taking the effort to try it out. This code is part of a large
program I'm doing - it's in a function, and it's ths only code there. That is
what confused me completely. I'm not a software guru, but I do have a few lines
of code behind me...

All this made me suspect even more that there was a library problem somewhere.
Someone suggested I include the math library, which, at first, I discarted
thinking there should be compile errors in that case. Still, that was the
solution! Just added an #include <math.h> and all was well.

I would like to know why though... Someone knows? I haven't seen this include
in any of your code?

Thanks again,
John
Nov 14 '05 #5

P: n/a
A final note:

Darrell Grainger pointed out that gcc actually has two printf's
available - a small one that cannot handle floats and a larger one
that does.

If one does not include <math.h>, apparently only the small version is
active, which explains why I didn't get an error (though in my humble
opinion, it should complain about the floats it can't handle).

Thanks to all for you suggestions and help.

John
Nov 14 '05 #6

P: n/a
John wrote:
A final note:

Darrell Grainger pointed out that gcc actually has two printf's
available - a small one that cannot handle floats and a larger one
that does.

If one does not include <math.h>, apparently only the small version is
active, which explains why I didn't get an error (though in my humble
opinion, it should complain about the floats it can't handle).


This is not a function of gcc, but of the libraries you use. The versions
of gcc and the libraries that I use all handle your code without any need
to #include <math.h>. This happens with no need to import any foreign
libraries.
--
Martin Ambuhl
Nov 14 '05 #7

P: n/a
Martin Ambuhl wrote:

John wrote:
A final note:

Darrell Grainger pointed out that gcc actually has two printf's
available - a small one that cannot handle floats and a larger one
that does.

If one does not include <math.h>, apparently only the small version is
active, which explains why I didn't get an error (though in my humble
opinion, it should complain about the floats it can't handle).


This is not a function of gcc, but of the libraries you use. The versions
of gcc and the libraries that I use all handle your code without any need
to #include <math.h>. This happens with no need to import any foreign
libraries.


On the other hand, I'm surprised that the non-floating version prints
"nan" for such situations. Many years ago, I worked on some *nix
systems that had the option of not including the floating-point
libraries, and in such a case, *printf() would generate something a
bit more meaningful, such as "floating point library not loaded".

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+

Nov 14 '05 #8

P: n/a
Kenneth Brody wrote:
Martin Ambuhl wrote:
John wrote:

Darrell Grainger pointed out that gcc actually has two printf's
available - a small one that cannot handle floats and a larger one
that does.

If one does not include <math.h>, apparently only the small version is
active, which explains why I didn't get an error (though in my humble
opinion, it should complain about the floats it can't handle).


This is not a function of gcc, but of the libraries you use. The versions
of gcc and the libraries that I use all handle your code without any need
to #include <math.h>. This happens with no need to import any foreign
libraries.


On the other hand, I'm surprised that the non-floating version prints
"nan" for such situations. Many years ago, I worked on some *nix
systems that had the option of not including the floating-point
libraries, and in such a case, *printf() would generate something a
bit more meaningful, such as "floating point library not loaded".


The original code has been snipped away, but IIRC the printf
statements were flawed by not allowing for the automatic
promotions.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #9

P: n/a
Groovy hepcat John was jivin' on 8 Jan 2004 17:56:39 -0800 in
comp.lang.c.
Re: Some basic error - what am I missing? (NaNs) - solved's a cool
scene! Dig it!
Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<qN*****************@newsread1.news.atl.earth link.net>...
[output]
342 224 343 225
339 215 340 215
3.000000 9.500000 0.000000
Thank you both for taking the effort to try it out. This code is part of a large
program I'm doing - it's in a function, and it's ths only code there. That is
what confused me completely. I'm not a software guru, but I do have a few lines
of code behind me...

All this made me suspect even more that there was a library problem somewhere.
Someone suggested I include the math library, which, at first, I discarted
thinking there should be compile errors in that case. Still, that was the
solution! Just added an #include <math.h> and all was well.


This is not the math library. It is the math.h header. You need to
learn the difference between libraries and headers.
Libraries are things that contain functions and variables that are
linked into your program. They do all (or some, at least) of the
actual work when your program runs. Libraries contain the actual
implementation of all those functions and the actual variables you've
read so much about in your C book.
Headers, OTOH, are things that contain C source code that provides
declarations of functions and variables, as well as macro definitions.
This is the *interface* to the functions and variables stored in the
libraries. IOW, the declarations in the headers tell the compiler what
it needs to know about the contents of the libraries.
I would like to know why though... Someone knows? I haven't seen this include
in any of your code?


Then you haven't looked.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #10

P: n/a
CBFalconer <cb********@yahoo.com> wrote in message news:<3F***************@yahoo.com>...
Kenneth Brody wrote:
Martin Ambuhl wrote:
John wrote:
>
> Darrell Grainger pointed out that gcc actually has two printf's
> available - a small one that cannot handle floats and a larger one
> that does.
>
> If one does not include <math.h>, apparently only the small version is
> active, which explains why I didn't get an error (though in my humble
> opinion, it should complain about the floats it can't handle).

This is not a function of gcc, but of the libraries you use. The versions
of gcc and the libraries that I use all handle your code without any need
to #include <math.h>. This happens with no need to import any foreign
libraries.

Ok... As I said before, I'm not a C-guru, just a user. I think I have
fairly recent versions of gcc (3.2.2) and the libraries. I can just
assure that the
original code didn't have the <math.h>, and produced NaNs after a
simple assignment of an integer to a float in an expression. After
adding the include
file, all was well.
On the other hand, I'm surprised that the non-floating version prints
"nan" for such situations. Many years ago, I worked on some *nix
systems that had the option of not including the floating-point
libraries, and in such a case, *printf() would generate something a
bit more meaningful, such as "floating point library not loaded".


The original code has been snipped away, but IIRC the printf
statements were flawed by not allowing for the automatic
promotions.


IMHO - the error was produced by the assignment statement (confirmed
by single-stepping with GDB). The printf just dutifully printed the
result.

Again thanks for your help & suggestions.
John
Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.