warning while making cast

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roman Mashak

    warning while making cast

    Hello,

    given a simple code:

    #include <math.h>
    long hyp(long height, long base)
    {
    return sqrt(height * height + base * base);
    }

    int main(void)
    {
    long h = hyp(10, 20);
    return 0;
    }

    I compile this on Debian 4.0 (gcc (GCC) 4.1.2 20061115 (prerelease) (Debian
    4.1.1-21)):
    #gcc -ansi -pedantic -W -Wall -lm hyp.c

    As I understand, according to standard sqrt returns double, but we assign
    result to long, resulting in information loss. Nevertheless I get no
    warning, why is it so?

    Hope it's strictly C related question.

    With best regards, Roman Mashak.


  • vippstar@gmail.com

    #2
    Re: warning while making cast

    On Jun 6, 6:04 am, "Roman Mashak" <m...@tusur.ruw rote:
    Hello,
    >
    given a simple code:
    >
    #include <math.h>
    long hyp(long height, long base)
    {
    return sqrt(height * height + base * base);
    >
    }
    >
    int main(void)
    {
    long h = hyp(10, 20);
    return 0;
    >
    }
    >
    I compile this on Debian 4.0 (gcc (GCC) 4.1.2 20061115 (prerelease) (Debian
    4.1.1-21)):
    #gcc -ansi -pedantic -W -Wall -lm hyp.c
    >
    As I understand, according to standard sqrt returns double, but we assign
    result to long, resulting in information loss. Nevertheless I get no
    warning, why is it so?
    >
    Hope it's strictly C related question.
    The return value of sqrt is implicity converted to long.
    6.3.1.4 --
    When a finite value of real floating type is converted to an integer
    type other than _Bool,
    the fractional part is discarded (i.e., the value is truncated toward
    zero). If the value of
    the integral part cannot be represented by the integer type, the
    behavior is undefined.

    Comment

    • Richard Tobin

      #3
      Re: warning while making cast

      In article <g28h8a$147s$1@ relay.tomsk.ru> , Roman Mashak <mrv@tusur.ruwr ote:
      >As I understand, according to standard sqrt returns double, but we assign
      >result to long, resulting in information loss. Nevertheless I get no
      >warning, why is it so?
      Compilers aren't required to warn you about information loss. I would
      certainly find it annoying if a compiler did that by default.

      I would have expected gcc to warn about it if you specified -Wconversion,
      but it doesn't seem to, so you could report that as a bug.

      -- Richard

      --
      In the selection of the two characters immediately succeeding the numeral 9,
      consideration shall be given to their replacement by the graphics 10 and 11 to
      facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

      Comment

      • Roman Mashak

        #4
        Re: warning while making cast

        Hello, vippstar@gmail. com!
        You wrote on Thu, 5 Jun 2008 04:27:18 -0700 (PDT):

        vThe return value of sqrt is implicity converted to long.
        In my understanding implicit conversion is a promotion of types, i.e.
        without loss of bits. For instance, float to double

        v6.3.1.4 --
        vWhen a ?nite value of real ?oating type is converted to an integer
        vtype other than _Bool,
        vthe fractional part is discarded (i.e., the value is truncated toward
        vzero). If the value of
        vthe integral part cannot be represented by the integer type, the
        vbehavior is unde?ned.
        Is this excerpt from C99 standard? I compiled the snippet with -ansi, which
        is C90 conformant.

        With best regards, Roman Mashak. E-mail: mrv@tusur.ru


        Comment

        • Roman Mashak

          #5
          Re: warning while making cast

          Hello, Richard!
          You wrote on 5 Jun 2008 11:41:34 GMT:

          RTCompilers aren't required to warn you about information loss. I would
          RTcertainly find it annoying if a compiler did that by default.

          By default no, but -Wextra is supposed to report about loss of precision as
          well. It does not in this case at least. Perhaps this is more GCC relevant
          topic.

          RTI would have expected gcc to warn about it if you specified
          RT-Wconversion, but it doesn't seem to, so you could report that as a
          RTbug.

          Compiling with -Wconversion invokes this warning:
          warning: passing argument 1 of 'sqrt' as floating rather than integer due to
          prototype

          But nothing about result of function.

          With best regards, Roman Mashak. E-mail: mrv@tusur.ru


          Comment

          • blockstack

            #6
            Re: warning while making cast

            On Jun 5, 11:03 pm, "Roman Mashak" <m...@tusur.ruw rote:
            Hello, Richard!
            You wrote  on 5 Jun 2008 11:41:34 GMT:
            >
             RTCompilers aren't required to warn you about information loss.  I would
             RTcertainly find it annoying if a compiler did that by default.
            >
            By default no, but -Wextra is supposed to report about loss of precision as
            well. It does not in this case at least. Perhaps this is more GCC relevant
            topic.
            >
             RTI would have expected gcc to warn about it if you specified
             RT-Wconversion, but it doesn't seem to, so you could report that as a
             RTbug.
            >
            Compiling with -Wconversion invokes this warning:
            warning: passing argument 1 of 'sqrt' as floating rather than integer due to
            prototype
            >
            But nothing about result of function.
            >
            With best regards, Roman Mashak.  E-mail: m...@tusur.ru
            try using -Wtraditional-conversion

            Comment

            • vippstar@gmail.com

              #7
              Re: warning while making cast

              On Jun 6, 6:54 am, "Roman Mashak" <m...@tusur.ruw rote:
              Hello, vipps...@gmail. com!
              You wrote on Thu, 5 Jun 2008 04:27:18 -0700 (PDT):
              >
              vThe return value of sqrt is implicity converted to long.
              In my understanding implicit conversion is a promotion of types, i.e.
              without loss of bits. For instance, float to double
              >
              v6.3.1.4 --
              vWhen a ?nite value of real ?oating type is converted to an integer
              vtype other than _Bool,
              vthe fractional part is discarded (i.e., the value is truncated toward
              vzero). If the value of
              vthe integral part cannot be represented by the integer type, the
              vbehavior is unde?ned.
              Is this excerpt from C99 standard? I compiled the snippet with -ansi, which
              is C90 conformant.
              Yes it is, but I believe the same rules apply for C90.

              Comment

              • Keith Thompson

                #8
                Re: warning while making cast

                "Roman Mashak" <mrv@tusur.ruwr ites:
                Hello, vippstar@gmail. com!
                You wrote on Thu, 5 Jun 2008 04:27:18 -0700 (PDT):
                >
                vThe return value of sqrt is implicity converted to long.
                >
                In my understanding implicit conversion is a promotion of types, i.e.
                without loss of bits. For instance, float to double
                [...]

                I'm afraid your understanding is incorrect.

                The promotions that occur for operands of most operators are designed
                not to lose information (at least in most cases(?)), but those aren't
                the only implicit conversions that can occur. In an assignment or
                equivalent (initialization , parameter passing), any arithmetic type
                can be implicitly converted to any other arithmetic type. You can
                even assign a long double to a char. No diagnostic is required,
                though compilers are free to print warnings if they choose.

                --
                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

                • Jack Klein

                  #9
                  Re: warning while making cast

                  On Thu, 5 Jun 2008 04:27:18 -0700 (PDT), vippstar@gmail. com wrote in
                  comp.lang.c:
                  On Jun 6, 6:04 am, "Roman Mashak" <m...@tusur.ruw rote:
                  [snip]
                  The return value of sqrt is implicity converted to long.
                  6.3.1.4 --
                  When a ?nite value of real ?oating type is converted to an integer
                  type other than _Bool,
                  the fractional part is discarded (i.e., the value is truncated toward
                  zero). If the value of
                  the integral part cannot be represented by the integer type, the
                  behavior is unde?ned.
                  I assume you're using Foxit for a PDF reader. Surely you've noticed
                  that it does not handle the single glyph combining the letter
                  combination "fi" in some documents, including the C standard.

                  I always do a search and replace of '?' with "fi" before I post a
                  paste from Foxit.

                  --
                  Jack Klein
                  Home: http://JK-Technology.Com
                  FAQs for
                  comp.lang.c http://c-faq.com/
                  comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                  alt.comp.lang.l earn.c-c++

                  Comment

                  • akbar

                    #10
                    Re: warning while making cast

                    On Jun 6, 8:04 am, "Roman Mashak" <m...@tusur.ruw rote:
                    Hello,
                    >
                    given a simple code:
                    >
                    #include <math.h>
                    long hyp(long height, long base)
                    {
                        return sqrt(height * height + base * base);
                    >
                    }
                    >
                    int main(void)
                    {
                       long h = hyp(10, 20);
                       return 0;
                    >
                    }
                    >
                    I compile this on Debian 4.0 (gcc (GCC) 4.1.2 20061115 (prerelease) (Debian
                    4.1.1-21)):
                    #gcc -ansi -pedantic -W -Wall -lm hyp.c
                    >
                    As I understand, according to standard sqrt returns double,  but we assign
                    result to long, resulting in information loss. Nevertheless I get no
                    warning,  why is it so?
                    >
                    Hope it's strictly C related question.
                    >
                    With best regards, Roman Mashak.
                    Hello Roman,

                    Yes, GCC doesn't show any warning as such even after trying with
                    multiple warning options.

                    Please try using Splint tool, it should definetly show return-type-
                    mismatch warning.

                    Thanks
                    Akbar

                    Comment

                    • Richard Bos

                      #11
                      Re: warning while making cast

                      Jack Klein <jackklein@spam cop.netwrote:
                      On Thu, 5 Jun 2008 04:27:18 -0700 (PDT), vippstar@gmail. com wrote in
                      >
                      On Jun 6, 6:04 am, "Roman Mashak" <m...@tusur.ruw rote:
                      >
                      When a ?nite value of real ?oating type is converted to an integer
                      type other than _Bool,
                      >
                      I assume you're using Foxit for a PDF reader. Surely you've noticed
                      that it does not handle the single glyph combining the letter
                      combination "fi" in some documents, including the C standard.
                      It's not necessarily Foxit. I've seen the same with pastes from Acrobat
                      Reader, into a newsreader which does not recognise those ligatures.
                      I always do a search and replace of '?' with "fi" before I post a
                      paste from Foxit.
                      You have fioating point types?

                      Richard

                      Comment

                      • jaysome

                        #12
                        Re: warning while making cast

                        On Thu, 5 Jun 2008 20:04:10 -0700, "Roman Mashak" <mrv@tusur.ru >
                        wrote:
                        >Hello,
                        >
                        >given a simple code:
                        >
                        >#include <math.h>
                        >long hyp(long height, long base)
                        >{
                        return sqrt(height * height + base * base);
                        >}
                        >
                        >int main(void)
                        >{
                        long h = hyp(10, 20);
                        return 0;
                        >}
                        >
                        >I compile this on Debian 4.0 (gcc (GCC) 4.1.2 20061115 (prerelease) (Debian
                        >4.1.1-21)):
                        >#gcc -ansi -pedantic -W -Wall -lm hyp.c
                        >
                        >As I understand, according to standard sqrt returns double, but we assign
                        >result to long, resulting in information loss. Nevertheless I get no
                        >warning, why is it so?
                        >
                        >Hope it's strictly C related question.
                        >
                        >With best regards, Roman Mashak.
                        The Microsoft Visual C++ 6.0 compiler (circa 1998) gives these
                        warnings:

                        Compiling...
                        hyp.c
                        hyp.c(4) : warning C4244: 'return' : conversion from 'double ' to
                        'long ', possible loss of data
                        hyp.c(9) : warning C4189: 'h' : local variable is initialized but not
                        referenced

                        hyp.obj - 0 error(s), 2 warning(s)

                        PC-lint gives these warnings:

                        PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software
                        1985-2006

                        --- Module: hyp.c (C)
                        _
                        return sqrt(height * height + base * base);
                        hyp.c(4) : Info 747: Significant prototype coercion (arg. no. 1) long
                        to double
                        hyp.c(4) : Info 790: Suspicious truncation, integral to float
                        hyp.c(4) : Warning 524: Loss of precision (return) (double to long)
                        _
                        }
                        hyp.c(11) : Warning 529: Symbol 'h' (line 9) not subsequently
                        referenced
                        hyp.c(9) : Info 830: Location cited in prior message

                        --- Global Wrap-up

                        Note 900: Successful completion, 5 messages produced
                        Tool returned code: 0

                        If you want warnings, you could:

                        (1) Use Windows and compile with VC++ 6.0 (circa 1998) or even give
                        the free VC++ Express Editions a try.
                        (2) Use PC-lint on your code.

                        --
                        jaysome

                        Visual Studio dev tools & services make app development easy for any developer, on any platform & language. Develop with our code editor or IDE anywhere for free.

                        PC-lint Plus is a static analysis tool that finds defects in software by analyzing the C and C++ source code.

                        Comment

                        • CBFalconer

                          #13
                          Re: warning while making cast

                          Jack Klein wrote:
                          >
                          .... snip ...
                          >
                          I assume you're using Foxit for a PDF reader. Surely you've noticed
                          that it does not handle the single glyph combining the letter
                          combination "fi" in some documents, including the C standard.
                          >
                          I always do a search and replace of '?' with "fi" before I post a
                          paste from Foxit.
                          I am using FoxitReader 2.1, which has this fault. I recently
                          downloaded version 2.5, and have not yet installed it. Do you know
                          if it fixes that glitch? If not I won't install it.

                          --
                          [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

                          • Richard Tobin

                            #14
                            Re: warning while making cast

                            In article <pl8h445i08n698 ivr1e7t9kl877t6 8qe06@4ax.com>,
                            Jack Klein <jackklein@spam cop.netwrote:
                            >I assume you're using Foxit for a PDF reader. Surely you've noticed
                            >that it does not handle the single glyph combining the letter
                            >combination "fi" in some documents, including the C standard.
                            It has handled it corrrectly. The post you were replying to contained
                            the unicode characters for the ligatures, encoded in UTF-8. The
                            problem is presumably that your newsreader (like mine) does not
                            display UTF-8 correctly.

                            -- Richard
                            --
                            In the selection of the two characters immediately succeeding the numeral 9,
                            consideration shall be given to their replacement by the graphics 10 and 11 to
                            facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

                            Comment

                            • lawrence.jones@siemens.com

                              #15
                              Re: warning while making cast

                              Jack Klein <jackklein@spam cop.netwrote:
                              >
                              I always do a search and replace of '?' with "fi" before I post a
                              paste from Foxit.
                              Unfortunately, the ? might actually be supposed to be "fl" instead. (Or
                              potentially lots of other things, too, but the C Standard should only
                              have fi and fl ligatures.)

                              -- Larry Jones

                              Good gravy, whose side are you on?! -- Calvin

                              Comment

                              Working...