Thread: Implicit conversion between float and double

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    10

    Implicit conversion between float and double

    Hi,
    I'm dealing with the complex.h header and the following lines give me a warning:

    Code:
        complex double W[MAX_V];
        W[1] = (1 * cos(-2. * M_PI / N)) + (I * 1 * sin(-2. * M_PI / N));
    Code:
    implicit conversion increases floating-point precision:
          '_Complex float' to '_Complex double' [-Wdouble-promotion]
        W[1] = (1 * cos(-2. * M_PI / N)) + (I * 1 * sin(-2. * M_PI / N));
                                            ~~^~~ ~
    I think this warning is related to those lines in complex.h, but I don't know how to fix it.

    Code:
    #define _Complex_I    (__extension__ 1.0iF)
    
    
    /* Another more descriptive name is `I'.
       XXX Once we have the imaginary support switch this to _Imaginary_I.  */
    #undef I
    #define I _Complex_I

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yeah, the F suffix means the expression Complex_I is a float type expression.

    You can avoid the warning by not compiling with -Wdouble-promotion. In fact, I can only reproduce the problem if I compile with it.

    Code:
    PS C:\Users\jk\desktop> gcc -Wall foo.c -o foo.exe -lm
    PS C:\Users\jk\desktop> .\foo.exe
    0.500000
    PS C:\Users\jk\desktop> more foo.c
    #define N 6
    
    #include <stdio.h>
    #include <math.h>
    #include <complex.h>
    
    int main(void)
    {
       _Complex double W[10];
       W[1] = (1 * cos(-2. * M_PI / N)) + (I * 1 * sin(-2. * M_PI / N));
       printf("%lf \n", __real__ W[1]);
       return 0;
    }
    
    PS C:\Users\jk\desktop> gcc -Wall -Wdouble-promotion foo.c -o foo.exe -lm
    foo.c: In function 'main':
    foo.c:10:46: warning: implicit conversion from 'complex float' to 'complex double' to match other operand of binary expression [-Wdouble-promotion]
        W[1] = (1 * cos(-2. * M_PI / N)) + (I * 1 * sin(-2. * M_PI / N));
                                                  ^
    PS C:\Users\jk\desktop>
    In my opinion, the warning causes more trouble than it is worth.

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    10
    Thank you, I figured it out it wasn't a big issue since I wasn't losing precision but I wanted to be sure

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    696
    Quote Originally Posted by whiteflags View Post
    You can avoid the warning by not compiling with -Wdouble-promotion. In fact, I can only reproduce the problem if I compile with it.
    gcc doesn't even complain with -Wall -Wextra -pedantic, so it's considered by gcc to be beyond pedantic.

    BTW, you are both forgetting that M_PI is not guaranteed to be defined in math.h (sadly). Try compiling with gcc -std=c99, for instance. You need something like:
    Code:
    #ifndef M_PI
    #define M_PI 3.141592653589793238
    #endif
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Type conversion from float to double
    By Ufuk in forum C Programming
    Replies: 7
    Last Post: 07-20-2017, 01:51 PM
  2. Replies: 7
    Last Post: 03-20-2016, 12:29 PM
  3. implicit int to float type conversion in expression
    By megafiddle in forum C Programming
    Replies: 5
    Last Post: 08-18-2015, 03:47 PM
  4. float to double conversion
    By cdalten in forum C Programming
    Replies: 2
    Last Post: 04-12-2006, 08:05 AM
  5. Conversion from float to double
    By Ward in forum C++ Programming
    Replies: 17
    Last Post: 10-08-2003, 05:27 PM

Tags for this Thread