Thread: Type conversion from float to double

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    4

    Type conversion from float to double

    Hi to all,

    I'm studying C from "C Primer Plus" ... and it says that on page 174:

    "When appearing in an expression, char and short, both signed and unsigned, are automatically converted to int or, if necessary, to unsigned int. (If short is the same size as int, unsigned short is larger than int; in that case, unsigned short is converted to unsigned int.) Under K&R C, but not under current C, float is automatically converted to double. Because they are conversions to larger types, they are called promotions."

    In my compiler (Visual Studio), float constants are automatically converted to doubles. So, do you think that the sentence about the conversion from float to double correct?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    In GCC, floats are also converted implicitly to double. I'm guessing that statement is probably incorrect, or outdated.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What about these float constants?
    Code:
    $ cat main.c
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main ( ) {
      float a = 123.456;
      float b = 654.321f;
      double c = 99.88;
      printf("%f %f %f\n", a, b, c);
      return 0;
    }
    $ gcc -Wall -Wfloat-conversion main.c
    main.c: In function ‘main’:
    main.c:6:13: warning: conversion to ‘float’ alters ‘double’ constant value [-Wfloat-conversion]
       float a = 123.456;
                 ^
    If you use the 'f' suffix, then your float constants really are float constants.
    Without a suffix, the constants inherently have a type 'double'.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2017
    Posts
    4
    Thank you Elkvis.

    Quote Originally Posted by Salem View Post
    If you use the 'f' suffix, then your float constants really are float constants.
    Without a suffix, the constants inherently have a type 'double'.
    So, you as well think that the underlined sentence from the book is probably incorrect ... thanks a lot.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    float constants are NOT automatically converted to doubles. That would be idiotic. What would be the point of having float constants then? If you just mean something like 1.23, then that's a double already. 1.23f is a float constant.

    For example, I see no evidence of conversion to double in the assembly code for the following (rather contrived) code.
    Code:
    #include <stdio.h>
    
    int main() {
        int n = 1;
        n *= 1.23f;
        printf("%d\n", n);
        return 0;
    }
    The assembly stores the 1.23f value as
    Code:
        .align 4
    .LC0:
        .long    1067282596
    which is the 4-byte bit pattern for 1.23f.

    Changing 1.23f to 1.23 yields
    Code:
        .align 8
    .LC0:
        .long    2061584302
        .long    1072934420
    which is the 8-byte bit pattern for 1.23.

    However, passing a float to a variadic function (as one of the variadic arguments) will convert it to double.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by algorism View Post
    float constants are NOT automatically converted to doubles.
    The quote in question from C Primer Plus didn't say anything about float constants. The OP will need to clarify, but I got the impression that their remark about float constants was more referring to floating-point constants, rather than literal float constants.

    Quote Originally Posted by algorism View Post
    That would be idiotic.
    I agree.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Jul 2017
    Posts
    4
    Code:
    short sh1 = 10;
    short sh2 = 11;
    short sh3 = sh1 + sh2;
    In this code, as far as I know, sh1 and sh2 are converted to int before adding them and then the resulting int value is trimmed to short when assigning to sh3, right?

    What about the same example for floating points:

    Code:
    float fl1 = 10.0;
    float fl2 = 11.0;
    float fl3 = fl1 + fl2;
    I think that the underlined section in the quoted paragraph refers to this case. Here, are fl1 and fl2 converted to double before adding them?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why not read the standard for yourself?
    Index of /jtc1/sc22/wg14/www/docs/n869
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-24-2012, 09:47 AM
  2. conversion from double to float - loss of data
    By diyteam in forum C++ Programming
    Replies: 20
    Last Post: 03-04-2008, 02:59 AM
  3. float to double conversion
    By cdalten in forum C Programming
    Replies: 2
    Last Post: 04-12-2006, 08:05 AM
  4. Conversion from float to double
    By Ward in forum C++ Programming
    Replies: 17
    Last Post: 10-08-2003, 05:27 PM
  5. 'float' to 'double' conversion warning
    By Achit in forum C Programming
    Replies: 5
    Last Post: 04-07-2003, 06:14 AM

Tags for this Thread