Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:12:20+00:00 2026-06-15T03:12:20+00:00

Can someone explain why the following code produces different output for the test4 variable

  • 0

Can someone explain why the following code produces different output for the test4 variable compared to the other 3? I have checked this with gcc version 4.2.1 and 4.5.3 (and others in between).

Maybe I am missing something obvious, but it looks pretty straightforward…

#include <stdio.h>
#include <complex.h>

main()
{
    double complex test1, test2, test3, test4;

    test1 = 81141117.0;
    test2 = 81141117.0 + I * 0;
    test3 = 81141117 + I * 0.0;
    test4 = 81141117 + I * 0;
    printf("%ld + %ld I, %ld + %ld I, ", (long)creal(test1), (long)cimag(test1), (long)creal(test2), (long)cimag(test2));
    printf("%ld + %ld I, %ld + %ld I\n", (long)creal(test3), (long)cimag(test3), (long)creal(test4), (long)cimag(test4));
}

Output:

81141117 + 0 I, 81141117 + 0 I
81141117 + 0 I, 81141120 + 0 I

It appears that test4, having integer terms only, is being promoted to a float instead of the declared double, and round-off is coming into play.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:12:21+00:00Added an answer on June 15, 2026 at 3:12 am

    I’ve tested GCC 4.7.1 on Mac OS X 10.7.5 (home-built GCC), and also the system provided /usr/bin/gcc and /usr/bin/clang, and they all produce the same result you report.

    I agree with your analysis that there must be a conversion to float instead of double when both terms of the complex number are expressed as integers. The C 2011 standard includes the example:

    ISO/IEC 9899:2011 §6.7.9 Initialization

    ¶24 EXAMPLE 1 Provided that <complex.h> has been #included, the declarations

    int i = 3.5;
    double complex c = 5 + 3 * I;
    

    define and initialize i with the value 3 and c with the value 5. 0 + i3. 0.

    This clearly indicates that you should be able to write integer expressions and obtain a valid double complex (though examples are non-normative). However, it does not address whether the integer value should be converted to float before being converted to double, but there’s nowhere else in the language that would do that automatically (obviously, you can force it), so it is unlikely to be the intended interpretation.

    On the whole, I think this is likely to be a bug that could be reported to the GCC team (and that’s not something I advocate doing lightly). If you run gcc --help, the output ends with the message:

    For bug reporting instructions, please see:
    <http://gcc.gnu.org/bugs.html>
    

    I would extend your example as shown below (or, rather, I did extend your example as shown below):

    #include <stdio.h>
    #include <complex.h>
    
    int main(void)
    {
        double complex test1, test2, test3, test4, test5, test6, test7, test8;
    
        test1 = 81141117.0;
        test2 = 81141117.0 + I * 0;
        test3 = 81141117 + I * 0.0;
        test4 = 81141117 + I * 0;
        test5 = (float)81141117 + I * 0;
        test6 = 81141117 + I * (float)0;
        test7 = 81141117.F + I * 0;
        test8 = 81141117 + I * 0.F;
        printf("%ld + %ld I, %ld + %ld I\n", (long)creal(test1), (long)cimag(test1), (long)creal(test2), (long)cimag(test2));
        printf("%ld + %ld I, %ld + %ld I\n", (long)creal(test3), (long)cimag(test3), (long)creal(test4), (long)cimag(test4));
        printf("%ld + %ld I, %ld + %ld I\n", (long)creal(test5), (long)cimag(test5), (long)creal(test6), (long)cimag(test6));
        printf("%ld + %ld I, %ld + %ld I\n", (long)creal(test7), (long)cimag(test7), (long)creal(test8), (long)cimag(test8));
    }
    

    The output I got was:

    81141117 + 0 I, 81141117 + 0 I
    81141117 + 0 I, 81141120 + 0 I
    81141120 + 0 I, 81141120 + 0 I
    81141120 + 0 I, 81141120 + 0 I
    

    As you can see, the last two lines, where there was an explicit (float) cast or explicit float constants, produce the same results as your problematic line (but the values are legitimate).

    You might (or might not) want to experiment with the CMPLX, CMPLXF (and maybe CMPLXL) macros; I’d only add them to the reported example if they produced ‘interesting’ values for some definition of ‘interesting’.


    Compiler version numbers:

    $ /usr/gcc/v4.7.1/bin/gcc --version
    gcc (GCC) 4.7.1
    Copyright (C) 2012 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    $ /usr/bin/gcc --version
    i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
    Copyright (C) 2007 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    $ clang --version
    Apple clang version 4.1 (tags/Apple/clang-421.11.65) (based on LLVM 3.1svn)
    Target: x86_64-apple-darwin11.4.2
    Thread model: posix
    $
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain this to me? I have the following code: <form action=<?php
Can someone explain the following code sample what does album[photo_attributes][] mean I found this
Can someone explain the output of the following code char* a[] = {ABC123, DEF456,
Can someone explain to me why the following code produces the exception it does?
I have the following code: oTable.fnSort([[columnIndex, 'asc']]); Can someone explain to me what the
Can someone explain the output of the following code? #include <iostream> template <class T>
Am new to matlab. Can someone explain me the following code. this code is
Can someone explain the following code snippet for me? // Bind base object so
I'm having a problem with TRY...CATCH blocks. Can someone explain why the following code
Hi can someone explain if the in the following code the synchronized code will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.