Problem with sqrt and cast

This is a discussion on Problem with sqrt and cast within the C Programming forums, part of the General Programming Boards category; There is something I don't understand about casting. This works if I put a double in the place of (float)n. ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    30

    Problem with sqrt and cast

    There is something I don't understand about casting. This works if I put a double in the place of (float)n.

    Code:
    #include <math.h>
    ...
    int n = 10;
    ....
    printf("%f\n", sqrt((float)n));

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    What's your question? double and float are both floating point types, the difference is that double has more precision.

    sqrt() takes a double type as the parameter. If you give it a int or a float, it will be implicitly converted to a double, so the cast is actually unnecessary.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use of static cast
    By alyeska in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2007, 10:08 PM

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