doubles and ints

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Cunningham

    doubles and ints

    Is this valid C syntax ?

    double=double/int;

    I seem to be having trouble here.

    Bill


  • Bert

    #2
    Re: doubles and ints

    On Jul 11, 10:36 am, "Bill Cunningham" <nos...@nspam.c omwrote:
    Is this valid C syntax ?
    >
    double=double/int;
    >
    I seem to be having trouble here.
    >
    Bill
    So if there's a double d and an int i, you're asking whether d = d / i
    is valid C syntax? I reckon that's right but if it doesn't get what
    you want, go change it. Experiment till the stupid computer gives you
    what you want. Convert i into a double and see what happens. Try

    d = d / (double)i

    Hope that works. Can't be screwed testing the code.

    Comment

    • Walter Roberson

      #3
      Re: doubles and ints

      In article <7Wxdk.858$713. 149@trnddc03>,
      Bill Cunningham <nospam@nspam.c omwrote:
      Is this valid C syntax ?
      >double=doubl e/int;
      >I seem to be having trouble here.
      No it is not.


      $ cat foof.c
      int main(void) {
      double=double/int;
      return 0;
      }
      $ cc -fullwarn foof.c
      cc-1040 cc: ERROR File = foof.c, Line = 2
      An identifier is expected.

      double=double/int;
      ^

      cc-1029 cc: ERROR File = foof.c, Line = 2
      An expression is expected at this point.

      double=double/int;
      ^

      2 errors detected in the compilation of "foof.c".



      'double' is a type name; when it appears at the beginning of
      a statement, it is understood as being part of a variable declarator.
      As you failed to follow the 'double' with a variable name (or
      mix of qualifiers) before the '=', the syntax is invalid.

      If you mean more generally is it valid to divide a variable or
      value of type double by a variable or value of type int, and
      assign the result to a variable of type double, then the answer is Yes,
      that is syntactically and semantically permitted; the int would get
      promoted to double and the expression would proceed from there.
      --
      "I will not approve any plan which is based on the old principle
      of build now and repair later." -- Walter Hickle

      Comment

      • Bert

        #4
        Re: doubles and ints

        On Jul 11, 11:06 am, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
        wrote:
        In article <7Wxdk.858$713. 149@trnddc03>,
        >
        Bill Cunningham <nos...@nspam.c omwrote:
        Is this valid C syntax ?
        double=double/int;
        I seem to be having trouble here.
        >
        No it is not.
        >
        $ cat foof.c
        int main(void) {
        double=double/int;
        return 0;}
        >
        $ cc -fullwarn foof.c
        cc-1040 cc: ERROR File = foof.c, Line = 2
        An identifier is expected.
        >
        double=double/int;
        ^
        >
        cc-1029 cc: ERROR File = foof.c, Line = 2
        An expression is expected at this point.
        >
        double=double/int;
        ^
        >
        2 errors detected in the compilation of "foof.c".
        >
        'double' is a type name; when it appears at the beginning of
        a statement, it is understood as being part of a variable declarator.
        As you failed to follow the 'double' with a variable name (or
        mix of qualifiers) before the '=', the syntax is invalid.
        >
        If you mean more generally is it valid to divide a variable or
        value of type double by a variable or value of type int, and
        assign the result to a variable of type double, then the answer is Yes,
        that is syntactically and semantically permitted; the int would get
        promoted to double and the expression would proceed from there.
        --
        "I will not approve any plan which is based on the old principle
        of build now and repair later." -- Walter Hickle
        Well then I don't see why he seems to be having trouble. The int is
        implicitly being converted to a double and he doesn't like what he's
        getting.

        Comment

        • Barry Schwarz

          #5
          Re: doubles and ints

          On Fri, 11 Jul 2008 00:36:19 GMT, "Bill Cunningham" <nospam@nspam.c om>
          wrote:
          Is this valid C syntax ?
          >
          >double=doubl e/int;
          >
          >I seem to be having trouble here.
          Post your compilable code so we can all see what the problem is.


          Remove del for email

          Comment

          • Bill Cunningham

            #6
            Re: doubles and ints


            "Barry Schwarz" <schwarzb@dqel. comwrote in message
            news:rvdd749dd4 8336l2kigqpqjds po77uheuv@4ax.c om...
            On Fri, 11 Jul 2008 00:36:19 GMT, "Bill Cunningham" <nospam@nspam.c om>
            wrote:
            >
            > Is this valid C syntax ?
            >>
            >>double=doub le/int;
            >>
            >>I seem to be having trouble here.
            >
            Post your compilable code so we can all see what the problem is.
            >
            >
            Remove del for email
            ------
            #include <stdio.h>
            #include <stdlib.h>
            #define _ex exit(EXIT_FAILU RE)

            int main (int argc,char *argv[]) {
            if (argc!=2) {
            puts("usage error");
            _ex;
            }
            int count=1;
            double x,y,z;

            y=x;z=y/count; <-----

            x=strtod(argv[1],NULL);
            FILE *fp;
            fp=fopen("s","a ");
            char *string="%.2f\t %.2f\t%.2f\t%i\ n";
            fprintf(fp,stri ng,x,y,z,count) ;
            fclose(fp);
            return 0;
            }


            Comment

            • Bill Cunningham

              #7
              Re: doubles and ints


              "Walter Roberson" <roberson@ibd.n rc-cnrc.gc.cawrote in message
              news:g56bmq$9j3 $1@canopus.cc.u manitoba.ca...
              In article <7Wxdk.858$713. 149@trnddc03>,
              Bill Cunningham <nospam@nspam.c omwrote:
              >
              > Is this valid C syntax ?
              >
              >>double=doub le/int;
              >
              >>I seem to be having trouble here.
              >
              No it is not.
              [snip]

              double a,b;
              int c;
              a=b/c;

              That's what I mean.

              Bill


              Comment

              • Bert

                #8
                Re: doubles and ints

                On Jul 11, 12:04 pm, "Bill Cunningham" <nos...@nspam.c omwrote:
                "Barry Schwarz" <schwa...@dqel. comwrote in message
                >
                news:rvdd749dd4 8336l2kigqpqjds po77uheuv@4ax.c om...On Fri, 11 Jul 2008 00:36:19 GMT, "Bill Cunningham" <nos...@nspam.c om>
                wrote:
                >
                Is this valid C syntax ?
                >
                >double=doubl e/int;
                >
                >I seem to be having trouble here.
                >
                Post your compilable code so we can all see what the problem is.
                >
                Remove del for email
                >
                ------
                #include <stdio.h>
                #include <stdlib.h>
                #define _ex exit(EXIT_FAILU RE)
                >
                int main (int argc,char *argv[]) {
                if (argc!=2) {
                puts("usage error");
                _ex;
                }
                int count=1;
                double x,y,z;
                >
                y=x;z=y/count; <-----
                >
                x=strtod(argv[1],NULL);
                FILE *fp;
                fp=fopen("s","a ");
                char *string="%.2f\t %.2f\t%.2f\t%i\ n";
                fprintf(fp,stri ng,x,y,z,count) ;
                fclose(fp);
                return 0;
                >
                }
                Why do I get the feeling that argc != 2 wouldn't work?
                You can't write y = x when the comp doesn't know the values stored in
                y and x cos they're UNINITIALIZED. Ya gotta give them something before
                you use them elsewhere. Which means that z=y/count doesn't work
                either. And I hate people who write y=x instead of y = x.

                Comment

                • Keith Thompson

                  #9
                  Re: doubles and ints

                  "Bill Cunningham" <nospam@nspam.c omwrites:
                  "Barry Schwarz" <schwarzb@dqel. comwrote in message
                  news:rvdd749dd4 8336l2kigqpqjds po77uheuv@4ax.c om...
                  >On Fri, 11 Jul 2008 00:36:19 GMT, "Bill Cunningham" <nospam@nspam.c om>
                  >wrote:
                  >>
                  >> Is this valid C syntax ?
                  >>>
                  >>>double=doubl e/int;
                  >>>
                  >>>I seem to be having trouble here.
                  >>
                  >Post your compilable code so we can all see what the problem is.
                  >>
                  >>
                  >Remove del for email
                  ------
                  #include <stdio.h>
                  #include <stdlib.h>
                  #define _ex exit(EXIT_FAILU RE)
                  >
                  int main (int argc,char *argv[]) {
                  if (argc!=2) {
                  puts("usage error");
                  _ex;
                  }
                  int count=1;
                  double x,y,z;
                  >
                  y=x;z=y/count; <-----
                  >
                  x=strtod(argv[1],NULL);
                  FILE *fp;
                  fp=fopen("s","a ");
                  char *string="%.2f\t %.2f\t%.2f\t%i\ n";
                  fprintf(fp,stri ng,x,y,z,count) ;
                  fclose(fp);
                  return 0;
                  }
                  When I compile the above, I get:

                  c.c:13: error: parse error before '<' token

                  When I remove the "<-----", it compiles without error. When I enable
                  more warnings, gcc complains about mixing declarations and statements
                  (allowed only in C99), and says:

                  c.c:11: warning: 'x' might be used uninitialized in this function

                  Looking at the code, x is not given an initial value. Whatever
                  garbage value it has is assigned to y, and then z is assigned y/1.

                  Style suggestions:

                  Add more whitespace, particularly around binary operators and after
                  commas. Drop the "_ex' macro and replace the single invocation of it
                  with a call to exit(EXIT_FAILU RE) (identifiers starting with
                  underscores should be avoided, and the macro does nothing but make
                  your code more obscure anyway).

                  You initially posted a line of something that bore only an indirect
                  resemblance to your actual code, and told us only that you "seem to be
                  having some trouble". After considerable coaxing, you finally posted
                  some real code -- but you *still* haven't told us what the actual
                  problem is. I see no syntax errors in the code you posted (other than
                  the arrow, which I presume you added later). If there had been a
                  syntax error, your compiler would have reported it.

                  What is the actual problem you're having? Are you getting an error or
                  warning message from your compiler? If so, what *exactly* did it
                  print? Is the program misbehaving? If so, what *exactly* does it do,
                  and how does this differ from what you expected?

                  I won't ask this again.

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

                  • Bill Cunningham

                    #10
                    Re: doubles and ints


                    "Keith Thompson" <kst-u@mib.orgwrote in message
                    news:ln63rd40do .fsf@nuthaus.mi b.org...

                    [snip]
                    What is the actual problem you're having? Are you getting an error or
                    warning message from your compiler? If so, what *exactly* did it
                    print? Is the program misbehaving? If so, what *exactly* does it do,
                    and how does this differ from what you expected?
                    >
                    I won't ask this again.
                    >
                    Everything compiles file. But when I input a number like 21.00 I get
                    this printed to a file.

                    21.00 0.00 0.00 1

                    That's not what I envisioned this to print but this at first.


                    21.00 21.00 21.00 1


                    Then I am going to probably add a do while loop.

                    Bill


                    Comment

                    • Dann Corbit

                      #11
                      Re: doubles and ints

                      "Bill Cunningham" <nospam@nspam.c omwrote in message
                      news:Dczdk.864$ 713.772@trnddc0 3...
                      >
                      "Barry Schwarz" <schwarzb@dqel. comwrote in message
                      news:rvdd749dd4 8336l2kigqpqjds po77uheuv@4ax.c om...
                      >On Fri, 11 Jul 2008 00:36:19 GMT, "Bill Cunningham" <nospam@nspam.c om>
                      >wrote:
                      >>
                      >> Is this valid C syntax ?
                      >>>
                      >>>double=doubl e/int;
                      >>>
                      >>>I seem to be having trouble here.
                      >>
                      >Post your compilable code so we can all see what the problem is.
                      >>
                      >>
                      >Remove del for email
                      ------
                      #include <stdio.h>
                      #include <stdlib.h>
                      #define _ex exit(EXIT_FAILU RE)
                      >
                      int main (int argc,char *argv[]) {
                      if (argc!=2) {
                      puts("usage error");
                      _ex;
                      }
                      int count=1;
                      double x,y,z;
                      >
                      y=x;z=y/count; <-----
                      >
                      x=strtod(argv[1],NULL);
                      FILE *fp;
                      fp=fopen("s","a ");
                      char *string="%.2f\t %.2f\t%.2f\t%i\ n";
                      fprintf(fp,stri ng,x,y,z,count) ;
                      fclose(fp);
                      return 0;
                      }
                      Perhaps you meant something like this:

                      #include <stdio.h>
                      #include <stdlib.h>

                      int main(int argc, char *argv[])
                      {
                      int count = 1;
                      double x = 1,
                      y,
                      z;
                      FILE *fp;

                      if (argc != 2) {
                      puts("usage error");
                      exit(EXIT_FAILU RE);
                      }
                      y = x;
                      z = y / count;

                      x = strtod(argv[1], NULL);
                      fp = fopen("s", "a");
                      if (fp) {
                      fprintf(fp, "%.2f\t%.2f\t%. 2f\t%i\n", x, y, z, count);
                      fclose(fp);
                      } else {
                      puts("failed to open file.");
                      }
                      return 0;
                      }


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

                      Comment

                      • Ben Bacarisse

                        #12
                        Re: doubles and ints

                        Bert <albert.xtheunk nown0@gmail.com writes:
                        On Jul 11, 12:04 pm, "Bill Cunningham" <nos...@nspam.c omwrote:
                        <snip>
                        >int main (int argc,char *argv[]) {
                        > if (argc!=2) {
                        > puts("usage error");
                        > _ex;
                        > }
                        > int count=1;
                        > double x,y,z;
                        >>
                        > y=x;z=y/count; <-----
                        >>
                        > x=strtod(argv[1],NULL);
                        <snip>
                        Why do I get the feeling that argc != 2 wouldn't work?
                        I don't know. It seems to be one of more reasonable part of this
                        program.
                        You can't write y = x when the comp doesn't know the values stored in
                        y and x cos they're UNINITIALIZED.
                        That is overstating the problem. There is not problem with using y
                        uninitialised in this context.
                        Ya gotta give them something before
                        you use them elsewhere. Which means that z=y/count doesn't work
                        either. And I hate people who write y=x instead of y = x.
                        Try to limit yourself (especially on Usenet) to hating what people do
                        ("I hate it when people write y=x") or the thing itself ("I hate y=x")
                        rather than hating people.

                        --
                        Ben.

                        Comment

                        • Nick Keighley

                          #13
                          Re: doubles and ints

                          On 11 Jul, 03:04, "Bill Cunningham" <nos...@nspam.c omwrote:
                          "Barry Schwarz" <schwa...@dqel. comwrote in message
                          news:rvdd749dd4 8336l2kigqpqjds po77uheuv@4ax.c om...On Fri, 11 Jul 2008 00:36:19 GMT, "Bill Cunningham" <nos...@nspam.c om>
                          wrote:
                          <snip>
                          >I seem to be having trouble here.
                          <snip>
                          ------
                          #include <stdio.h>
                          #include <stdlib.h>
                          #define _ex exit(EXIT_FAILU RE)
                          try to avoid starting identifiers with '_'. Only
                          The Implementor (the guy who writes the compiler and the
                          standard library) should use such identifiers.

                          Hiding things in macros makes your code obscure.
                          int main (int argc,char *argv[]) {
                              if (argc!=2) {
                                 puts("usage error");
                                 _ex;
                                 }
                              int count=1;
                              double x,y,z;
                          you are mixing ststements with definitions which is not legal C89
                          (the most widely available standard).
                              y=x;z=y/count;   <-----
                          please use whitespace

                          y = x;
                          z = y / count;

                          x is an uninitialised variable. Hence x, y, and z
                          and indetereminate values. Why do you divide
                          by count which is equal to 1 (one)?
                              x=strtod(argv[1],NULL);
                          no error checking
                              FILE *fp;
                              fp=fopen("s","a ");
                          no error checking
                              char *string="%.2f\t %.2f\t%.2f\t%i\ n";
                              fprintf(fp,stri ng,x,y,z,count) ;
                          x is equal to whatever strtod() returned
                          y and z are indeterminate.
                              fclose(fp);
                              return 0;

                          --
                          Nick Keighley

                          Comment

                          • Nick Keighley

                            #14
                            Re: doubles and ints

                            On 11 Jul, 04:07, "Bill Cunningham" <nos...@nspam.c omwrote:
                            "Keith Thompson" <ks...@mib.orgw rote in message
                            >
                            news:ln63rd40do .fsf@nuthaus.mi b.org...
                            >
                            [snip]
                            >
                            What is the actual problem you're having?  Are you getting an error or
                            warning message from your compiler?  If so, what *exactly* did it
                            print?  Is the program misbehaving?  If so, what *exactly* does it do,
                            and how does this differ from what you expected?
                            >
                            I won't ask this again.
                            >
                                Everything compiles file. But when I input a number like 21.00 I get
                            this printed to a file.
                            >
                            21.00    0.00    0.00    1
                            >
                            That's not what I envisioned this to print but this at first.
                            >
                            21.00    21.00    21.00 1
                            >
                            Then I am going to probably add a do while loop.
                            why?

                            --
                            Nick Keighley

                            Comment

                            • Kenny McCormack

                              #15
                              Re: doubles and ints

                              In article <ln63rd40do.fsf @nuthaus.mib.or g>,
                              Keith Thompson <kst-u@mib.orgwrote:
                              ....
                              >I won't ask this again.
                              Sure you will. Again and again. Until you die (or stop posting to clc,
                              which will probably occur that same day).

                              Comment

                              Working...