Thread: Type Casting for return statements, any gains??

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    2

    Question Type Casting for return statements, any gains??

    Is the following type cast really needed???

    float myFunction(float inVal)
    {
    //does this type cast of a float really gain anything??
    return (float) (inVal * 25.25687);
    }

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I don't believe so. your return value is a float, your original value is a float. if the compiler sees the 25.25687 as a double that's the only thing I can see as an issue. I say leave it out. I could be wrong.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    2
    Would the compiler and/or target platform be an issue?

  4. #4
    Registered User
    Join Date
    Aug 2001
    Location
    Fort Worth, TX
    Posts
    53
    I don't see any gains. It does floating-point arithmetic and then the return type is a float.

    What is the overhead of type casting?

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by vVv
    >What is the overhead of type casting?

    There's none, it's merely a way to tell the compiler how to view the data. And in this particular case there aren't even any changes to the regular treatment without a cast, because both variables are floats already.
    No, actually they both weren't floats. The value he used is a double not a float and so the value of the expression on the right hand side of the equals was double not float. You'd typecast to float for clarity -- it should automatically make the conversion however most compilers will warn you that you will lose precission. If you explicitly typecast, the compiler understands that that is truely what you wanted to do and most likely won't display a warning. Anytime you are doing arithmetic that results in a datatype of more precission than that of the variable you are storing the value to it's a good idea to explicitly type-cast, though it is certainly not necissary.
    Last edited by Polymorphic OOP; 12-18-2002 at 10:52 PM.

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    Actually, to cast you should use static_cast<>() syntax instead of the older C way.
    Mr. C: Author and Instructor

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Mister C
    Actually, to cast you should use static_cast<>() syntax instead of the older C way.
    Didn't we just have this debate? Let's not go through this again.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    OK, fine. Well at least we all know it is int main()!!!
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. difference between type conversion and type casting
    By Bargi in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 03:17 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 12:38 PM