Operating System - HP-UX
1752756 Members
5071 Online
108789 Solutions
New Discussion

double to unsigned int typecasting is failing in HPUX itanium 11.31

 
Dibendusah
Occasional Contributor

double to unsigned int typecasting is failing in HPUX itanium 11.31

Hi All,

 

I have come across a very surprising behaviour of s programme in HPUX itanium11.31

 

The conversion from double to unsigned int is failing under certain circumstances and making it zero. Howevr the same code works in HPUX itanium 11.23.

 

Below is the sample programme which is failing.

 

=====================

#include<stdio.h>

int main()
{

double dd = -2147483648.000000;
unsigned int k = dd;
printf("%lf",dd);
printf(":::::");
printf("%d",k);     /* here is the problem after assignment value of k is becoming zero

return 1;

}

====================================

 

How ever if I am converting the double to long and then that long again gets converted to unsigned it, it works. sample programme:

 

#include<stdio.h>

int main()
{

double dd = -2147483648.000000;
long ll = dd;
unsigned int k = ll;
printf("%lf",dd);
printf(":::::");
printf("%d",k);

return 1;

}

 

==============================================

In our application we have been using the double to unsigned it many palces from long time ( or from HPUX itanium 11.23). However the code is breaking only in HPUX itanium 11.31 .

 

Any patch is mising in 11.31 version ?

 

 

Please put some light in this direction.

 

Thanks

Dibendusah

 

 

P.S. This thread has been moved from HP-UX > Patches to HP-UX > languages - HP Forums Moderator

 

 

 

 

2 REPLIES 2
Dennis Handly
Acclaimed Contributor

Re: double to unsigned int casting is failing in HP-UX Integrity 11.31

What versions of aCC6 are you using on 11.23 and 11.31?

Dennis Handly
Acclaimed Contributor

Re: double to unsigned int casting is failing in HP-UX Integrity 11.31

>The conversion from double to unsigned int is failing under certain circumstances and making it zero.

 

This a correct behavior for your undefined code.  A negative double is NOT valid to be assigned to an unsigned integral type and has undefined behavior.  For Integrity it causes an IEEE Invalid operation with the result of 0x8000000000000000ULL, which is truncated to 0.  Linking with +FPV will cause a runtime SIGFPE.

 

>the same code works in HP-UX Integrity 11.23.

 

Not that I can see.  All aCC5 and aCC6 versions do the same.  Only PA-RISC does what you said.

(Unless you were running it under Aries.)

 

The difference is that IA64 contains an instruction to convert directly from double to unsigned long long.

But PA-RISC at one time (PA1.0) didn't have that instruction and unfortunately it was never used after it was added.

 

Note these formats aren't valid:

   printf("%lf", dd);
   printf("%d", k);

 

In the first the "l" is ignored, so it should just be %f.  The second should be %u for unsigned.