Simple Casting Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alex.j.k2@gmail.com

    Simple Casting Question

    Hello all,


    I have "PRECISION" defined in the preprocessor code
    and it could be int, float or double, but I do not know in the
    code what it is.

    Now if I want to assign zero to a "PRECISION" variable,
    which of the following lines are correct:


    PRECISION myVar = (PRECISION) 0; /* One */

    PRECISION myVar = (PRECISION) 0.0; /* Two */

    PRECISION myVar = 0; /* Three */

    PRECISION myVar = 0.0; /* Four */

    Thank you in advance,
    Alex
  • Amandil

    #2
    Re: Simple Casting Question

    On May 18, 7:35 pm, alex.j...@gmail .com wrote:
    Hello all,
    >
    I have "PRECISION" defined in the preprocessor code
    and it could be int, float or double, but I do not know in the
    code what it is.
    >
    Now if I want to assign zero to a "PRECISION" variable,
    which of the following lines are correct:
    >
    PRECISION myVar = (PRECISION) 0; /* One */
    >
    PRECISION myVar = (PRECISION) 0.0; /* Two */
    >
    PRECISION myVar = 0; /* Three */
    >
    PRECISION myVar = 0.0; /* Four */
    >
    Thank you in advance,
    Alex
    You could try the different combinations and see which gives you an
    error, and which works properly. For example, assuming PRECISION is a
    float:
    1) float myVar = (float) 0; /* cast not needed, the compiler does
    it automatically */
    2) float myVar = (float) 0.0; /* 0.0 defaults to double, cast doesn't
    hurt */
    3) float myVar = 0; /* Compiler performs cast on its own */
    4) float myVar = 0.0; /* 0.0 defaults to double, but this is
    okay */

    Now, assuming PRECISION is an integral type, such as int:
    1) int myVar = (int) 0; /* A waste of typing time */
    2) int myvar = (int) 0.0; /* The compiler can do this at compile
    time, but bad idea */
    3) int myVar = 0; /* That's how we usually do it */
    4) int myVar = 0.0; /* That one is a compiler error */

    So method 4 is problematic, method 3 is the least typing, methods 1
    and 2 are okay too.

    Try this on your own, I hope I'm not wrong.

    -- Marty Wolfe

    "Very simple. This is method three." -- Alfred Hitchcock, "Three
    Ways to Rob a Bank"

    Comment

    • pete

      #3
      Re: Simple Casting Question

      alex.j.k2@gmail .com wrote:
      Hello all,
      >
      >
      I have "PRECISION" defined in the preprocessor code
      and it could be int, float or double, but I do not know in the
      code what it is.
      >
      Now if I want to assign zero to a "PRECISION" variable,
      which of the following lines are correct:
      >
      >
      PRECISION myVar = (PRECISION) 0; /* One */
      >
      PRECISION myVar = (PRECISION) 0.0; /* Two */
      >
      PRECISION myVar = 0; /* Three */
      >
      PRECISION myVar = 0.0; /* Four */
      I like /* Three */.

      --
      pete

      Comment

      • Keith Thompson

        #4
        Re: Simple Casting Question

        alex.j.k2@gmail .com writes:
        I have "PRECISION" defined in the preprocessor code
        and it could be int, float or double, but I do not know in the
        code what it is.
        >
        Now if I want to assign zero to a "PRECISION" variable,
        which of the following lines are correct:
        >
        >
        PRECISION myVar = (PRECISION) 0; /* One */
        >
        PRECISION myVar = (PRECISION) 0.0; /* Two */
        >
        PRECISION myVar = 0; /* Three */
        >
        PRECISION myVar = 0.0; /* Four */
        All four are correct. The third is preferred, IMHO.

        It does seem odd that PRECISION can be either an integer type or a
        floating-point type. Integer and floating-point types behave in very
        different, and sometimes quite subly different, ways. I would think
        that writing code that doesn't know whether it's dealing with integers
        or floating-point values would be difficult and error-prone.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Keith Thompson

          #5
          Re: Simple Casting Question

          Amandil <mazwolfe@gmail .comwrites:
          On May 18, 7:35 pm, alex.j...@gmail .com wrote:
          > I have "PRECISION" defined in the preprocessor code
          >and it could be int, float or double, but I do not know in the
          >code what it is.
          >>
          > Now if I want to assign zero to a "PRECISION" variable,
          >which of the following lines are correct:
          >>
          > PRECISION myVar = (PRECISION) 0; /* One */
          >>
          > PRECISION myVar = (PRECISION) 0.0; /* Two */
          >>
          > PRECISION myVar = 0; /* Three */
          >>
          > PRECISION myVar = 0.0; /* Four */
          >>
          >Thank you in advance,
          >Alex
          >
          You could try the different combinations and see which gives you an
          error, and which works properly. For example, assuming PRECISION is a
          float:
          1) float myVar = (float) 0; /* cast not needed, the compiler does
          it automatically */
          2) float myVar = (float) 0.0; /* 0.0 defaults to double, cast doesn't
          hurt */
          3) float myVar = 0; /* Compiler performs cast on its own */
          4) float myVar = 0.0; /* 0.0 defaults to double, but this is
          okay */
          >
          Now, assuming PRECISION is an integral type, such as int:
          1) int myVar = (int) 0; /* A waste of typing time */
          2) int myvar = (int) 0.0; /* The compiler can do this at compile
          time, but bad idea */
          3) int myVar = 0; /* That's how we usually do it */
          4) int myVar = 0.0; /* That one is a compiler error */
          >
          So method 4 is problematic, method 3 is the least typing, methods 1
          and 2 are okay too.
          >
          Try this on your own, I hope I'm not wrong.
          I'm afraid you are, which you would have learned if you had tried the
          code yourself before posting it.

          The casts are unnecessary in any case. Any arithmetic type can be
          implicitly converted to any other arithmetic type. In all of these:

          int x = (int)0;
          int x = (int)0.0;
          float x = (float)0;
          float x = (float)0.0;

          the cast is entirely superfluous; either the expression is already of
          the target type, or the conversion will be done implicitly and the
          cast is unnecessary. (Note that "cast" refers to the explicit
          operator that specifies a conversion. There's no such thing as an
          "implicit cast"; it's an "implicit conversion".)

          All casts should be viewed with suspicion. There are definitely cases
          where they're necessary and appropriate, but it's usually better to
          rely on the implicit conversions defined by the language (since the
          compiler will guarantee that the conversion is to the correct target
          type).

          And yes, this:

          int x = 0.0;

          is perfectly legal; the double value 0.0 is implicitly converted to
          the int value 0.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • CBFalconer

            #6
            Re: Simple Casting Question

            alex.j.k2@gmail .com wrote:
            >
            I have "PRECISION" defined in the preprocessor code and it could
            be int, float or double, but I do not know in the code what it is.
            >
            Now if I want to assign zero to a "PRECISION" variable, which of
            the following lines are correct:
            >
            PRECISION myVar = (PRECISION) 0; /* One */
            PRECISION myVar = (PRECISION) 0.0; /* Two */
            PRECISION myVar = 0; /* Three */
            PRECISION myVar = 0.0; /* Four */
            Definitely Three. C knows the types, and will make the automatic
            conversions. In general, casts indicate probable errors.

            --
            [mail]: Chuck F (cbfalconer at maineline dot net)
            [page]: <http://cbfalconer.home .att.net>
            Try the download section.


            ** Posted from http://www.teranews.com **

            Comment

            • Bart

              #7
              Re: Simple Casting Question

              On May 19, 12:35 am, alex.j...@gmail .com wrote:
              Hello all,
              >
                 I have "PRECISION" defined in the preprocessor code
              and it could be int, float or double, but I do not know in the
              code what it is.
              >
                 Now if I want to assign zero to a "PRECISION" variable,
              which of the following lines are correct:
              >
                 PRECISION myVar = (PRECISION) 0; /* One */
              >
                 PRECISION myVar = (PRECISION) 0.0; /* Two */
              >
                 PRECISION myVar = 0; /* Three */
              >
                 PRECISION myVar = 0.0; /* Four */
              For setting to zero, no problems; all do the same, and (3) is
              shortest.

              But for setting to 9.82 for example, only (2) and (4) can be used. And
              when PRECISION is int, you may find it rounds wrongly (to 9 not 10).

              Also determining a printf/scanf formatcode for PRECISION could be
              awkward.

              --
              Bartc

              Comment

              • alex.j.k2@gmail.com

                #8
                Re: Simple Casting Question

                First of all, thank you to all that answered.

                On May 18, 8:58 pm, Keith Thompson <ks...@mib.orgw rote:
                >
                All four are correct. The third is preferred, IMHO.
                >
                This is the consensus.
                It does seem odd that PRECISION can be either an integer type or a
                floating-point type. Integer and floating-point types behave in very
                different, and sometimes quite subly different, ways. I would think
                that writing code that doesn't know whether it's dealing with integers
                or floating-point values would be difficult and error-prone.
                >
                I have a function that does large amounts of computations
                (divisions, square roots, etc.) for large datasets. For efficiency
                reasons I allow the option to format the data differently.
                I also have a large dataset of integers which I have to feed to the
                function.

                I do make sure in the preprocessor code that all casting is done
                only in the form (int to float), (float to double), (int to double),
                or just casting identical types, so I hope that I can avoid some
                errors that way.

                I am curios about the subtle and not so subtle bugs introduced
                by overenthusiasti c uses of casting.
                Do you have a reference/link/short paragraph related to this theme?

                Thank you again for your answers,
                Alex


                Comment

                • Amandil

                  #9
                  Re: Simple Casting Question

                  On May 18, 9:05 pm, Keith Thompson <ks...@mib.orgw rote:
                  Amandil <mazwo...@gmail .comwrites:
                  On May 18, 7:35 pm, alex.j...@gmail .com wrote:
                  I have "PRECISION" defined in the preprocessor code
                  and it could be int, float or double, but I do not know in the
                  code what it is.
                  >
                  Now if I want to assign zero to a "PRECISION" variable,
                  which of the following lines are correct:
                  >
                  PRECISION myVar = (PRECISION) 0; /* One */
                  >
                  PRECISION myVar = (PRECISION) 0.0; /* Two */
                  >
                  PRECISION myVar = 0; /* Three */
                  >
                  PRECISION myVar = 0.0; /* Four */
                  >
                  Thank you in advance,
                  Alex
                  >
                  You could try the different combinations and see which gives you an
                  error, and which works properly. For example, assuming PRECISION is a
                  float:
                  1) float myVar = (float) 0; /* cast not needed, the compiler does
                  it automatically */
                  2) float myVar = (float) 0.0; /* 0.0 defaults to double, cast doesn't
                  hurt */
                  3) float myVar = 0; /* Compiler performs cast on its own */
                  4) float myVar = 0.0; /* 0.0 defaults to double, but this is
                  okay */
                  >
                  Now, assuming PRECISION is an integral type, such as int:
                  1) int myVar = (int) 0; /* A waste of typing time */
                  2) int myvar = (int) 0.0; /* The compiler can do this at compile
                  time, but bad idea */
                  3) int myVar = 0; /* That's how we usually do it */
                  4) int myVar = 0.0; /* That one is a compiler error */
                  >
                  So method 4 is problematic, method 3 is the least typing, methods 1
                  and 2 are okay too.
                  >
                  Try this on your own, I hope I'm not wrong.
                  >
                  I'm afraid you are, which you would have learned if you had tried the
                  code yourself before posting it.
                  >
                  The casts are unnecessary in any case. Any arithmetic type can be
                  implicitly converted to any other arithmetic type. In all of these:
                  >
                  int x = (int)0;
                  int x = (int)0.0;
                  float x = (float)0;
                  float x = (float)0.0;
                  >
                  the cast is entirely superfluous; either the expression is already of
                  the target type, or the conversion will be done implicitly and the
                  cast is unnecessary. (Note that "cast" refers to the explicit
                  operator that specifies a conversion. There's no such thing as an
                  "implicit cast"; it's an "implicit conversion".)
                  >
                  All casts should be viewed with suspicion. There are definitely cases
                  where they're necessary and appropriate, but it's usually better to
                  rely on the implicit conversions defined by the language (since the
                  compiler will guarantee that the conversion is to the correct target
                  type).
                  >
                  And yes, this:
                  >
                  int x = 0.0;
                  >
                  is perfectly legal; the double value 0.0 is implicitly converted to
                  the int value 0.
                  You're right. I didn't take the time to test it before I posted. And I
                  was surprised to see that assigning a float value to an int resulted
                  in an implicit conversion, but you're right about that, too. I guess I
                  still have a lot to learn.

                  -- Marty Amandil Wolfe (I know Mom uses my full name only when I'm in
                  trouble...)

                  Comment

                  • Richard Bos

                    #10
                    Re: Simple Casting Question

                    Keith Thompson <kst-u@mib.orgwrote:
                    The casts are unnecessary in any case. Any arithmetic type can be
                    implicitly converted to any other arithmetic type. In all of these:
                    >
                    int x = (int)0;
                    int x = (int)0.0;
                    float x = (float)0;
                    float x = (float)0.0;
                    >
                    the cast is entirely superfluous; either the expression is already of
                    the target type, or the conversion will be done implicitly and the
                    cast is unnecessary. (Note that "cast" refers to the explicit
                    operator that specifies a conversion. There's no such thing as an
                    "implicit cast"; it's an "implicit conversion".)
                    int x = 0.0;
                    >
                    is perfectly legal; the double value 0.0 is implicitly converted to
                    the int value 0.
                    One important thing to note here is that other values - that is, values
                    that cannot be represented in the target type - may have unexpected and
                    indeed highly undesired effects; another important thing to note is that
                    in the case of 0 (or 0.0), that specific value must, obviously, be
                    representable in all arithmetic types.

                    Richard

                    Comment

                    • pete

                      #11
                      Re: Simple Casting Question

                      Amandil wrote:
                      On May 18, 9:05 pm, Keith Thompson <ks...@mib.orgw rote:
                      >Amandil <mazwo...@gmail .comwrites:
                      >>On May 18, 7:35 pm, alex.j...@gmail .com wrote:
                      >>> I have "PRECISION" defined in the preprocessor code
                      >>>and it could be int, float or double, but I do not know in the
                      >>>code what it is.
                      >>> Now if I want to assign zero to a "PRECISION" variable,
                      >>>which of the following lines are correct:
                      >>> PRECISION myVar = (PRECISION) 0; /* One */
                      >>> PRECISION myVar = (PRECISION) 0.0; /* Two */
                      >>> PRECISION myVar = 0; /* Three */
                      >>> PRECISION myVar = 0.0; /* Four */
                      >>>Thank you in advance,
                      >>>Alex
                      >>You could try the different combinations and see which gives you an
                      >>error, and which works properly. For example, assuming PRECISION is a
                      >>float:
                      >>1) float myVar = (float) 0; /* cast not needed, the compiler does
                      >>it automatically */
                      >>2) float myVar = (float) 0.0; /* 0.0 defaults to double, cast doesn't
                      >>hurt */
                      >>3) float myVar = 0; /* Compiler performs cast on its own */
                      >>4) float myVar = 0.0; /* 0.0 defaults to double, but this is
                      >>okay */
                      >>Now, assuming PRECISION is an integral type, such as int:
                      >>1) int myVar = (int) 0; /* A waste of typing time */
                      >>2) int myvar = (int) 0.0; /* The compiler can do this at compile
                      >>time, but bad idea */
                      >>3) int myVar = 0; /* That's how we usually do it */
                      >>4) int myVar = 0.0; /* That one is a compiler error */
                      >>So method 4 is problematic, method 3 is the least typing, methods 1
                      >>and 2 are okay too.
                      >>Try this on your own, I hope I'm not wrong.
                      >I'm afraid you are, which you would have learned if you had tried the
                      >code yourself before posting it.
                      >>
                      >The casts are unnecessary in any case. Any arithmetic type can be
                      >implicitly converted to any other arithmetic type. In all of these:
                      >>
                      > int x = (int)0;
                      > int x = (int)0.0;
                      > float x = (float)0;
                      > float x = (float)0.0;
                      >>
                      >the cast is entirely superfluous; either the expression is already of
                      >the target type, or the conversion will be done implicitly and the
                      >cast is unnecessary. (Note that "cast" refers to the explicit
                      >operator that specifies a conversion. There's no such thing as an
                      >"implicit cast"; it's an "implicit conversion".)
                      >>
                      >All casts should be viewed with suspicion. There are definitely cases
                      >where they're necessary and appropriate, but it's usually better to
                      >rely on the implicit conversions defined by the language (since the
                      >compiler will guarantee that the conversion is to the correct target
                      >type).
                      >>
                      >And yes, this:
                      >>
                      > int x = 0.0;
                      >>
                      >is perfectly legal; the double value 0.0 is implicitly converted to
                      >the int value 0.
                      >
                      You're right. I didn't take the time to test it before I posted. And I
                      was surprised to see that assigning a float value to an int resulted
                      in an implicit conversion, but you're right about that, too. I guess I
                      still have a lot to learn.
                      Any object type can be initialized using {0}.

                      int i = {0};
                      float f = {0};
                      void *p = {0};
                      char array[3][7][9] = {0};

                      #include <stdlib.h>

                      div_t d = {0};

                      --
                      pete

                      Comment

                      • pete

                        #12
                        Re: Simple Casting Question

                        alex.j.k2@gmail .com wrote:
                        I am curios about the subtle and not so subtle bugs introduced
                        by overenthusiasti c uses of casting.
                        Do you have a reference/link/short paragraph related to this theme?
                        Whenever <stdlib.hwas not #included and malloc was used,
                        C89 compilers tended to recommend casting the return value of malloc.
                        The use of such a cast would suppress that warning,
                        but yield undefined behavior.

                        The proper advice, is to #include <stdlib.hprio r to using malloc.
                        That also suppresses whatever warning
                        would result from the non-inclusion, but helps make for correct code.

                        --
                        pete

                        Comment

                        • vippstar@gmail.com

                          #13
                          Re: Simple Casting Question

                          On May 19, 1:05 pm, pete <pfil...@mindsp ring.comwrote:
                          alex.j...@gmail .com wrote:
                          I am curios about the subtle and not so subtle bugs introduced
                          by overenthusiasti c uses of casting.
                          Do you have a reference/link/short paragraph related to this theme?
                          >
                          Whenever <stdlib.hwas not #included and malloc was used,
                          C89 compilers tended to recommend casting the return value of malloc.
                          The use of such a cast would suppress that warning,
                          but yield undefined behavior.
                          >
                          The proper advice, is to #include <stdlib.hprio r to using malloc.
                          That also suppresses whatever warning
                          would result from the non-inclusion, but helps make for correct code.
                          It is also possible to include just the prototype of malloc
                          void *malloc(size_t) ; and not <stdlib.h>.

                          Comment

                          • Barry Schwarz

                            #14
                            Re: Simple Casting Question

                            On May 19, 3:05 am, pete <pfil...@mindsp ring.comwrote:
                            >
                            Whenever <stdlib.hwas not #included and malloc was used,
                            C89 compilers tended to recommend casting the return value of malloc.
                            The use of such a cast would suppress that warning,
                            but yield undefined behavior.
                            >
                            The proper advice, is to #include <stdlib.hprio r to using malloc.
                            That also suppresses whatever warning
                            Eliminates, not suppresses.
                            would result from the non-inclusion, but helps make for correct code.

                            Comment

                            • Barry Schwarz

                              #15
                              Re: Simple Casting Question

                              On May 18, 7:17 pm, alex.j...@gmail .com wrote:
                              First of all, thank you to all that answered.
                              >
                              On May 18, 8:58 pm, Keith Thompson <ks...@mib.orgw rote:
                              >
                              >
                              >
                              All four are correct.  The third is preferred, IMHO.
                              >
                              This is the consensus.
                              You obviously read enough of the messages to conclude this.

                              snip
                              >
                                I do make sure in the preprocessor code that all casting is done
                              only in the form (int to float), (float to double), (int to double),
                              or just casting identical types, so I hope that I can avoid some
                              errors that way.
                              How did you avoid the equally emphatic consensus that superfluous
                              casting is a very bad idea? None of the conversions you describe
                              needs a cast as long as there are prototypes in scope when you call
                              the functions involved.

                              Comment

                              Working...