473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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
10 1807
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Simon | last post by:
I would like to create a very basic file upload add image form to add to my web site and to keep them in a "tmp" directory within my web hosting file manager once uploaded. I understand the basic...
6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
0
by: deploying visual basic .net application | last post by:
Hi There, I'm trying to create a setup for a Windows application to install my application I developed using Visual basic .net. I'm using Visual Studio .net Professional. When I use the Set up...
0
by: planetthoughtful | last post by:
Hi All, It seems my installation of Visual Basic Help for Access 2003 is missing pages / entries. eg, when I do a search on 'subform' and then click on 'Me Property' in the search results, I...
13
by: bjhartin | last post by:
All, I was working at a client's site when I installed Visual Basic 6 on a machine at their request. Existing applications on this machine had specified version 4.0 of the Jet OLEDB provider in...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
3
by: Ben | last post by:
Hi There I am doing some unit testing at the moment, and the majority of the leg work involves taking two objects (expected vs actual) and verifying that their properties are equal. The objects...
1
by: Minh | last post by:
I've just installed VS.NET 2003 on my Athlon XP 1800+. However I couldn't get any project with STL includes to compile even if I create a new empty project (and added #include <string>). It gave me...
0
by: Shashikiran Prabhakar via .NET 247 | last post by:
(Type your message here) Hi, I am not very conversent in VC++, but the requirement for me is to run a rendering code. However i am encountering the following errors. c:\Program...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.