problem returning a very large double
Author Message problem returning a very large double

Hi,

I'm using gcc, and when I try to return a very large double, it get's
mangled. for example...

double point_line_intersect( XYZ_t p1, XYZ_t p2, XYZ_t p3, XYZ_t *pa )
{
double temp;

// (p1 - p3) dot (p2 - p1)
temp = (p1.x - p3.x) * ( p2.x - p1.x ) +
(p1.y - p3.y) * ( p2.y - p1.y ) +
(p1.z - p3.z) * ( p2.z - p1.z );

// divide by |p2 - p1|^2
temp /= (p2.x - p1.x)*(p2.x - p1.x) +
(p2.y - p1.y)*(p2.y - p1.y) +
(p2.z - p1.z)*(p2.z - p1.z);

pa->x = p1.x - ( p2.x - p1.x ) * temp;
pa->y = p1.y - ( p2.y - p1.y ) * temp;
pa->z = p1.z - ( p2.z - p1.z ) * temp;

printf( "temp = %g\n", temp );
return -temp;

Quote:
}

if temp = -1.27433e+241

the value returned is = 0.994549

double rtn;

rtn = point_line_intersect( p1, p2, p3, &pa );
printf( "rtn = %g\n", rtn );

any idea?

Thanks
Jason

Tue, 29 Nov 2005 02:52:08 GMT  problem returning a very large double
On 12 Jun 2003, you wrote in comp.lang.c:

Quote:
> Hi,

> I'm using gcc, and when I try to return a very large double, it get's
> mangled. for example...

[irrelevant computation snipped]

Quote:
> if temp = -1.27433e+241
> the value returned is = 0.994549
> double rtn;

> rtn = point_line_intersect( p1, p2, p3, &pa );
> printf( "rtn = %g\n", rtn );

> any idea?

Frankly, I don't believe you.  Try the code below.  I compiled it with
gcc, and got the output shown.  If your symptom is shown with this code,
maybe some of the people at the gnu.gcc.help newsgroup that you
crossposted to can help.  By the way, this probably does not belong in
comp.lang.c, anyway, since this seems to be at most implementation-
specific and perhaps your-installation-only specific.

#include <stdio.h>
#include <float.h>

float return_float(float x)
{
return x;

Quote:
}

double return_double(double x)
{
return x;
Quote:
}

long double return_longdouble(long double x)
{
return x;

Quote:
}

int main(void)
{
long double temp = -1.27433e+241L;
if (temp > FLT_MAX)
printf("temp exceeds FLT_MAX = %g\n", FLT_MAX);
else
printf("returned float is %g\n", return_float(temp));
if (temp > DBL_MAX)
printf("temp exceeds DBL_MAX = %g\n", DBL_MAX);
else
printf("returned double is %g\n", return_double(temp));
if (temp > LDBL_MAX)
printf("temp exceeds DBL_MAX = %Lg\n", LDBL_MAX);
else
printf("returned long double is %Lg\n",
return_longdouble(temp));
return 0;

Quote:
}

returned float is -Inf
returned double is -1.27433e+241
returned long double is -1.27433e+241

--
Martin Ambuhl
now exiled to
Hurricane Bait, Texas

Tue, 29 Nov 2005 03:27:57 GMT  problem returning a very large double

Quote:

> Hi,

> I'm using gcc, and when I try to return a very large double, it get's
> mangled. for example...
> [snippets snipped]

There's nothing obviously wrong with the code fragments
you posted, so the difficulty is most likely in the "connective
tissue" you didn't show us.  Please post a minimal complete
compilable program demonstrating the problem.

--

Tue, 29 Nov 2005 03:22:49 GMT  problem returning a very large double
maybe some intermediate result was too large or too small so it produced an
overflow. you could try it with long double indstead. long double is has *on
some implementations* more digits than double.

--
cody

Freeware Tools, Games and Humour
http://www.*-*-*.com/

Tue, 29 Nov 2005 04:30:15 GMT  problem returning a very large double

Quote:
>Hi,

>I'm using gcc, and when I try to return a very large double, it get's
>mangled. for example...

[unenlightening definition of function snipped]

Quote:
>double rtn;

>rtn = point_line_intersect( p1, p2, p3, &pa );
>printf( "rtn = %g\n", rtn );

You don't show anywhere near enough code.  My first guess would be that
you forgot to declare the function before calling it.  In that case the
compiler would assume it returns an int, which would garble the return
value spectacularly.

--
Eric Amick
Columbia, MD

Wed, 30 Nov 2005 09:28:32 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages