Hi,
When running the below testcase, PGCC18 outputs 255 while other compilers like gcc, clang and intelc output 227. I think other compilers might first convert double to int and then covert int to uint8_t. But I am confused why PGCC18 outputs 255.
TestCase:
#include<stdint.h>
#include<stdio.h>
int main(void){
double NISLParameter0 = -29.5;
uint8_t NISLParameter1 = (uint8_t)NISLParameter0;
printf(“%d”,NISLParameter1);
return 0;
}
Compiler Version:
pgcc18 19.10-0 LLVM 64-bit target on x86-64 Linux -tp skylake
Output:
255
Expected output:
227
Looking forward to your reply. Thanks in advance.
Cheers,
Xing
Hi Xing,
I believe casting a double with a value of <-1 to an unsigned integer has undefined behavior. From the C Standard:
6.3.1.4 Real floating and integer
1 When a finite value of real floating type is converted to an integer type other than _Bool,
the fractional part is discarded (i.e., the value is truncated toward zero). > If the value of
the integral part cannot be represented by the integer type, the behavior is undefined. > 61)
- The remaindering operation performed when a value of integer type is converted to unsigned type
need not be performed when a value of real floating type is converted to unsigned type. > Thus, the
range of portable real floating values is (−1, Utype_MAX+1).
What are you trying to do with this conversion?
-Mat
Hi Mat,
If the behavior is undefined,it does allow the pgcc18 to output the maximum value of the uint8_t/uint16_t type. The conversion has no particular purpose, it is just a testcase to test for compiler errors.
Thanks for your reply!