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

J

John

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
 
E

Eric Sosman

John said:
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.
 
M

Martin Dickopp

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;
}
 
M

Martin Ambuhl

John said:
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
 
J

John

Martin Ambuhl said:
[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
 
J

John

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
 
M

Martin Ambuhl

John said:
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.
 
K

Kenneth Brody

Martin said:
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." |
+---------+----------------------------------+-----------------------------+
 
C

CBFalconer

Kenneth said:
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.
 
P

Peter Shaggy Haywood

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 said:
[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"?
 
J

John

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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top