Type conversions

This is a discussion on Type conversions within the C++ Programming forums, part of the General Programming Boards category; Consider the following code (I'd use code tags - but I can't find the button for them): #include <iostream.h> int ...

  1. #1
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    Type conversions

    Consider the following code (I'd use code tags - but I can't find the button for them):

    #include <iostream.h>

    int main()
    {
    int x;
    int y = 159; // <- no meaning - just a number
    float z = (float)y;
    cout >> x >> y >> z;
    return 0;
    }

    x would be essentially random - you have no control over it whatsoever, y is 159, but would z be 159.000 etc... or would it come out with a completely random, something like 159.31415298? I have a lot of questions all for the same project all to do with data types - but I've posted them separately to avoid confusion and complexity.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Re: Type conversions

    Originally posted by Sean

    Code:
    #include <iostream.h>
    
    int main()
    {
    int x;
    int y = 159; // <- no meaning - just a number
    float z = (float)y;
    cout >> x >> y >> z;
    return 0;
    }
    >>>float z = (float)y;
    Do you mean float (y);
    C++
    The best

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >but would z be 159.000 etc... or would it come out with a completely random, something like 159.31415298?<

    From the standard -

    An rvalue of an integer type can be converted to an rvalue of a floating point type. The result is exact if
    possible. Otherwise, it is an unspecified choice of either the next lower or higher representable value. Loss
    of precision occurs if the integral value cannot be represented exactly as a value of the floating type.
    So it should be 159.000 if possible.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    No. That's how you do type conversions - type casting. And thanks for the guy who posted just above me - I think you pressed post while I was still typing it - so I had to edit say thanks.
    Last edited by sean; 06-25-2002 at 02:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21