Math problem with C code
-
Just FYI, this is NOT a repost. As usual I have posted the original in WRONG pew. However, I found another even weirder failure of simple math case and I really do not have a clue what I am doing wrong. Are my local variable assignments incorrect ? Is "printf" lying to me ? Here is the entire test code
#define MAX_CIRCLES 5
struct TAG_Circles {
float radius;
float center;
float center_x;
float center_y;} REAL_Circle[MAX_CIRCLES], IMG_Circle[MAX_CIRCLES];
void OpenGL_Stencil_Circle_Build(void);
// copy last circle to stencil
void OpenGL_Stencil_Circle_Build(void) {
//
int index = 5;
{
// last circle
float radius = IMG_circle[index].radius;
float center_x = IMG_circle[index].center_x;
float center_y = IMG_circle[index].center_y;
printf("\n Last circle #%i radius struct %f radius %f", index,
REAL_Circle[index].radius, radius);
printf("\n Last circle #%i center_x struct %f center_x %f", index,
REAL_Circle[index].center_x, center_x);
printf("\n Last circle #%i center_y struct %f center_y %f", index,
REAL_Circle[index].center_y, center_y);}
}
Here is the output
Last circle #5 radius struct 0.065789 radius 0.131579
Last circle #5 center_x struct 0.000000 center_x 0.118421
Last circle #5 center_y struct 0.000000 center_y 0.000000The questions are NOT what I am trying to accomplish with the code , the questions are: 1. why values printed FROM struct DO NOT match assigned values ? 2. why is "radius" TWICE of the value assigned from struct? PS Only
radius struct 0.065789
is of correct value. Cheers
-
Just FYI, this is NOT a repost. As usual I have posted the original in WRONG pew. However, I found another even weirder failure of simple math case and I really do not have a clue what I am doing wrong. Are my local variable assignments incorrect ? Is "printf" lying to me ? Here is the entire test code
#define MAX_CIRCLES 5
struct TAG_Circles {
float radius;
float center;
float center_x;
float center_y;} REAL_Circle[MAX_CIRCLES], IMG_Circle[MAX_CIRCLES];
void OpenGL_Stencil_Circle_Build(void);
// copy last circle to stencil
void OpenGL_Stencil_Circle_Build(void) {
//
int index = 5;
{
// last circle
float radius = IMG_circle[index].radius;
float center_x = IMG_circle[index].center_x;
float center_y = IMG_circle[index].center_y;
printf("\n Last circle #%i radius struct %f radius %f", index,
REAL_Circle[index].radius, radius);
printf("\n Last circle #%i center_x struct %f center_x %f", index,
REAL_Circle[index].center_x, center_x);
printf("\n Last circle #%i center_y struct %f center_y %f", index,
REAL_Circle[index].center_y, center_y);}
}
Here is the output
Last circle #5 radius struct 0.065789 radius 0.131579
Last circle #5 center_x struct 0.000000 center_x 0.118421
Last circle #5 center_y struct 0.000000 center_y 0.000000The questions are NOT what I am trying to accomplish with the code , the questions are: 1. why values printed FROM struct DO NOT match assigned values ? 2. why is "radius" TWICE of the value assigned from struct? PS Only
radius struct 0.065789
is of correct value. Cheers
-
Just FYI, this is NOT a repost. As usual I have posted the original in WRONG pew. However, I found another even weirder failure of simple math case and I really do not have a clue what I am doing wrong. Are my local variable assignments incorrect ? Is "printf" lying to me ? Here is the entire test code
#define MAX_CIRCLES 5
struct TAG_Circles {
float radius;
float center;
float center_x;
float center_y;} REAL_Circle[MAX_CIRCLES], IMG_Circle[MAX_CIRCLES];
void OpenGL_Stencil_Circle_Build(void);
// copy last circle to stencil
void OpenGL_Stencil_Circle_Build(void) {
//
int index = 5;
{
// last circle
float radius = IMG_circle[index].radius;
float center_x = IMG_circle[index].center_x;
float center_y = IMG_circle[index].center_y;
printf("\n Last circle #%i radius struct %f radius %f", index,
REAL_Circle[index].radius, radius);
printf("\n Last circle #%i center_x struct %f center_x %f", index,
REAL_Circle[index].center_x, center_x);
printf("\n Last circle #%i center_y struct %f center_y %f", index,
REAL_Circle[index].center_y, center_y);}
}
Here is the output
Last circle #5 radius struct 0.065789 radius 0.131579
Last circle #5 center_x struct 0.000000 center_x 0.118421
Last circle #5 center_y struct 0.000000 center_y 0.000000The questions are NOT what I am trying to accomplish with the code , the questions are: 1. why values printed FROM struct DO NOT match assigned values ? 2. why is "radius" TWICE of the value assigned from struct? PS Only
radius struct 0.065789
is of correct value. Cheers
Vaclav_ wrote:
Here is the entire test code
As usual it is not the entire code, since none of your struct entities have been initialised with values. Please provide the values of all the variables that you are using, explain what you are trying to calculate, and why you consider the results are wrong.
-
Could it be because you're assigning
radius
from**IMG**_circle
but printing the value of**REAL**_circle
?Keep Calm and Carry On