// -----------------------------------------------
  mySquare( y1)                    // rcv'ing dbl = 4
{
double x1=1 ;// valid, but impossible to reach.     

printf("\n SQRD incoming  Y1: %d",y1);    //  shows 4
printf("\n SQRD return    y1*y1: %d",y1*y1); //shows 16
x1 = (y1*y1);                                 //   ???
printf("\n SQRD return    X1: %d",x1);      //shows 0 !
         
return x1;                       // rtns zero, not 16 !
}
// -------------------------------------------------

Maybe I'm just tired, but I just don't see why x1 isn't 16 .
Hope no-one flames me too badly for the lack of correct tags
TNX
Shift-Stop

Recommended Answers

All 7 Replies

If you want a function to square a double then try

double mysquare(double x)
{
	return x * x;
}
// -----------------------------------------------
int  mySquare(int y1)                    // rcv'ing dbl = 4
{
double x1=1 ;// valid, but impossible to reach.     

printf("\n SQRD incoming  Y1: %d",y1);    //  shows 4
printf("\n SQRD return    y1*y1: %d",y1*y1); //shows 16
x1 = (int)(y1*y1);     //   ???
printf("\n SQRD return    X1: %d",x1);      //shows 0 !
         
return x1;                       // rtns zero, not 16 !
}
// -------------------------------------------------

Tnx,

I should have stated up-front that I already knew that one.

What i don't know - and want to know is why THIS code is not functioning
as expected. (Well,...my code,.. above.)

I 'expected' to see the correct value in the print statement;
I often use this type of statement to flag my VARs as i unit test my code
- a habit I got into during my undergrad Comp/Sci stuff in the 1970's,
but THIS flag failed and I (still) don't see the reason.

If you - or anyone, has any idea pls let me now.

TNX.

Shift-Stop


If you want a function to square a double then try

double mysquare(double x)
{
	return x * x;
}

Your format specifiers

printf("\n SQRD return X1: %d",x1);

%d is the integer format specifier...You should be using %f.

Tnx Gerard4143,

I'll try it
BTW - I've always thought of 16 as being in INT
as in (INT 4) * (INT 4) = (INT 16)
therefore
(INT x) * (INT x) "should" = (INT y)

_Later,

TNX
Shift-Stop

Tnx Gerard4143,

I'll try it
BTW - I've always thought of 16 as being in INT
as in (INT 4) * (INT 4) = (INT 16)
therefore
(INT x) * (INT x) "should" = (INT y)

_Later,

TNX
Shift-Stop

Well the literal value 16 is some form of integer but x1 is defined as a float type.

oooooooooooooohhhh!

Yes, The light bulb begins to brighten !

TNX!!!

dbl - oops.

tnx gerard4143

ps any relation to Gerard at Ms Win Support '87 ?
Shift-Stop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.