![]() |
Rule 10.3, conversion from complex types to real floating types - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21) +---- Forum: 8.10 The essential type model (https://forum.misra.org.uk/forumdisplay.php?fid=165) +---- Thread: Rule 10.3, conversion from complex types to real floating types (/showthread.php?tid=1244) |
Rule 10.3, conversion from complex types to real floating types - satoshi - 11-05-2016 The following code can be compiled. Code: float f; // sizeof(float) = 4 The "double type" has no imaginary parts. So, I can say "assign to narrower essential type" ? Re: Rule 10.3, conversion from complex types to real floating types - dg1980 - 11-05-2016 The code example also violates Directive 4.6 and Rule 1.2. The problem with language extensions such as _Complex is, that they have no essential type in the MISRA document. But i would imagine that f=fc is a certain violation, because the imaginary part seems to be sliced. And d=fc and fc=d because they are essentially different. Re: Rule 10.3, conversion from complex types to real floating types - satoshi - 17-05-2016 Thank you for answering. Sorry, I had forgotten to write with the C99. Keyword "_Bool", "long long", "_Complex" were all added in C99. _Bool and long long are standard, but _Complex is language extension? I do not want to think the _Complex, but there is no clear reason... dg1980 Wrote:The code example also violates Directive 4.6 and Rule 1.2. Re: Rule 10.3, conversion from complex types to real floating types - dg1980 - 17-05-2016 Sorry, you are correct. _Complex indeed is standardized in C99. The basic type is identical, while the size is not (see quotes below). So, in my opinion, the mixed assignments in your sample code could be classified as violating 10.3 because of a "narrower essential type". Let´s wait for an official statement. Quote:For each floating type there is a corresponding real type, which is always a real floating Quote:Each complex type has the same representation and alignment requirements as an array Re: Rule 10.3, conversion from complex types to real floating types - satoshi - 18-05-2016 Thank you. Your explanation is convincing. dg1980 Wrote:So, in my opinion, the mixed assignments in your sample code could be classified as violating 10.3 because of a "narrower essential type". Re: Rule 10.3, conversion from complex types to real floating types - misra-c - 27-06-2016 When a value of complex floating type is converted to a real floating type, the imaginary part is discarded (C99 6.3.17(2)) and the real part is converted according to the normal C conversion rules. The normal MISRA C:2012 guidelines apply to the conversion on the real part. Code: d = fc; // permitted float -> double conversion Code: double _Complex dc; When a value of real floating type is converted to a complex floating type, the imaginary part is given the value of 0 (C99 6.3.17(1)) and the real part is converted according to the normal C conversion rules. The normal MISRA C:2012 guidelines apply to the conversion on the real part. Code: fc = d; // violates rule 10.3 since real part undergoes narrowing conversion. RE: Rule 10.3, conversion from complex types to real floating types - misra-c - 02-07-2024 Support for _Complex was added my MISRA C:2012 Amendment 3, and incorporated into MISRA C:2023 |