Strange output

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

    Strange output

    I have written some programs in c lang yet but today I get confused
    with output i get.
    I have function educate(..) which i call in main program this way:

    J = educate(no_laye rs, no_neurons, input, output, lay, weights, 0.1,0);
    printf("J= %f\n",J);

    In the educate function i count J and i return it (but before I return
    it I output it with printf):

    float educate (int no_layers, int no_neurons[], float input[], float
    output[], float* lay[], float* weights[], float gama, int debug) {
    ....
    printf("J= %f\n",J);
    return(J);
    }

    This is what i get:

    J= 0.304447
    J= 1050402944.0000 00

    Is there any syntax prob? Why the values arent the same? :(

  • Flash Gordon

    #2
    Re: Strange output

    unique wrote:[color=blue]
    > I have written some programs in c lang yet but today I get confused
    > with output i get.
    > I have function educate(..) which i call in main program this way:[/color]

    <snip description of program>
    [color=blue]
    > This is what i get:
    >
    > J= 0.304447
    > J= 1050402944.0000 00
    >
    > Is there any syntax prob? Why the values arent the same? :([/color]

    Post a *complete* small program that exhibits your problem, NOT a
    description. How are we to guess which of the many possible errors you
    have committed if you don't show us your actual code?
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.

    Comment

    • unique

      #3
      Re: Strange output

      I was investigating the problem a bit and the problem seems to be that
      I call in file educate.c function educate(...) from the other file
      neuron.c. I dont use any include, so code is here:
      ***test1.c***
      float educate() {
      float a=2.54f;
      printf("a= %f", a);
      return a;
      }


      ***test2.c***
      int main(int argc, char **argv) {
      float b=educate();
      printf("b= %f", b);
      }

      After compiling it with: cc test1.c test2.c -lm -o test2 and running it
      the output is: a= 2.540000b= 1076006784.0000 00

      So the problem is definitely in including file test2... how can i
      include it right? what's going on in my example?

      Thank you for your reply
      Flash Gordon wrote:[color=blue]
      > unique wrote:[color=green]
      > > I have written some programs in c lang yet but today I get confused
      > > with output i get.
      > > I have function educate(..) which i call in main program this way:[/color]
      >
      > <snip description of program>
      >[color=green]
      > > This is what i get:
      > >
      > > J= 0.304447
      > > J= 1050402944.0000 00
      > >
      > > Is there any syntax prob? Why the values arent the same? :([/color]
      >
      > Post a *complete* small program that exhibits your problem, NOT a
      > description. How are we to guess which of the many possible errors you
      > have committed if you don't show us your actual code?
      > --
      > Flash Gordon
      > Living in interesting times.
      > Although my email address says spam, it is real and I read it.[/color]

      Comment

      • pete

        #4
        Re: Strange output

        Flash Gordon wrote:[color=blue]
        >
        > unique wrote:[color=green]
        > > I have written some programs in c lang yet but today I get confused
        > > with output i get.
        > > I have function educate(..) which i call in main program this way:[/color]
        >
        > <snip description of program>
        >[color=green]
        > > This is what i get:
        > >
        > > J= 0.304447
        > > J= 1050402944.0000 00
        > >
        > > Is there any syntax prob? Why the values arent the same? :([/color]
        >
        > Post a *complete* small program that exhibits your problem, NOT a
        > description. How are we to guess which of the many possible errors you
        > have committed if you don't show us your actual code?[/color]

        My guess is that changing the declaration of all of
        the float type objects, to type double, will fix the problem.

        I avoid the small arithmetic types, :

        char
        unsigned char
        /* Especially these next 4 */
        signed char
        short
        unsigned short
        float

        unless there's a special reason.
        The problem is that those types tend to get promoted a lot.

        Strings are a special enough reason to use type char.
        Reading and/or writing bytes in raw memory
        is a good reason to use unsigned char.

        --
        pete

        Comment

        • pete

          #5
          Re: Strange output

          unique wrote:[color=blue]
          >
          > I was investigating the problem a bit and the problem seems to be that
          > I call in file educate.c function educate(...) from the other file
          > neuron.c. I dont use any include, so code is here:
          > ***test1.c***
          > float educate() {
          > float a=2.54f;
          > printf("a= %f", a);
          > return a;
          > }
          >
          > ***test2.c***
          > int main(int argc, char **argv) {
          > float b=educate();
          > printf("b= %f", b);
          > }
          >
          > After compiling it with:
          > cc test1.c test2.c -lm -o test2 and running it[/color]

          What does "cc test1.c test2.c -lm -o test2" mean?
          [color=blue]
          > the output is: a= 2.540000b= 1076006784.0000 00
          >
          > So the problem is definitely in including file test2...
          > how can i include it right? what's going on in my example?[/color]

          I don't see #include <stdio.h> in either file,
          so both files are undefined.
          [color=blue][color=green]
          > > Post a *complete* small program that exhibits your problem, NOT a
          > > description.
          > > How are we to guess which of the many possible errors you
          > > have committed if you don't show us your actual code?[/color][/color]

          --
          pete

          Comment

          • Skarmander

            #6
            Re: Strange output

            unique wrote:[color=blue]
            > I was investigating the problem a bit and the problem seems to be that
            > I call in file educate.c function educate(...) from the other file
            > neuron.c. I dont use any include, so code is here:
            > ***test1.c***
            > float educate() {
            > float a=2.54f;
            > printf("a= %f", a);
            > return a;
            > }
            >
            >
            > ***test2.c***
            > int main(int argc, char **argv) {
            > float b=educate();
            > printf("b= %f", b);
            > }
            >
            > After compiling it with: cc test1.c test2.c -lm -o test2 and running it
            > the output is: a= 2.540000b= 1076006784.0000 00
            >
            > So the problem is definitely in including file test2... how can i
            > include it right? what's going on in my example?
            >[/color]
            <snip>
            What's going on is that the compiler doesn't know what sort of function
            educate() is when it's compiling test2.c. In C, units are compiled one
            at a time, even if you pass multiple to the compiler.

            So the compiler must assume a default of educate() returning an int,
            which of course it doesn't. The bits that make up the float are then
            interpreted as an int and converted back to a float. To fix this, you
            should add a function prototype, like so:

            test2.c:
            float educate(void);

            int main(int argc, char **argv) {
            float b=educate();
            printf("b= %f", b);
            return 0;
            }

            But good style is to put prototypes of functions accessed by other units
            in headers and include them:

            test1.h:
            float educate(void);

            test2.c:
            #include "test1.h"

            int main(...

            Read any good book on C that talks about functions and prototypes for more.

            S.

            Comment

            • unique

              #7
              Re: Strange output

              > So the compiler must assume a default of educate() returning an int,
              which of course it doesn't.

              Thank you very much Skarmander, you are completely right, i solved it
              with header file.

              Have a gr8 day all

              Comment

              • Flash Gordon

                #8
                Re: Strange output

                unique wrote:

                Your reply belongs *after* the text you are replying to, not before,
                after deleting (snipping) the text you are not replying to.
                [color=blue]
                > I was investigating the problem a bit and the problem seems to be that
                > I call in file educate.c function educate(...) from the other file
                > neuron.c. I dont use any include, so code is here:
                > ***test1.c***[/color]

                #include <stdio.h>
                #include <test1.h>

                I'll explain the reasons for these further down.
                [color=blue]
                > float educate() {[/color]

                If it doesn't take parameters it is better to explicitly say so.
                float educate(void)
                {
                [color=blue]
                > float a=2.54f;
                > printf("a= %f", a);[/color]

                printf is a varidac function and *requires* a prototype in scope. The
                normal way to do this is to include stdio.h at or about the top of the
                source file.
                [color=blue]
                > return a;
                > }
                >
                >
                > ***test2.c***[/color]

                #include <stdio.h>
                #include <test1.h>

                I'll explain the reasons for these further down.
                [color=blue]
                > int main(int argc, char **argv) {[/color]

                You are not using the parameters, so you might as well say so.
                int main(void)
                {

                Note that main returns and int (no other return value including void is
                standard).
                [color=blue]
                > float b=educate();[/color]

                There is no prototype for educate in scope, so the compiler is
                *required* to assume it returns an int. Since it does not the behaviour
                is undefined and the effect in your case is that b is assigned a garbage
                value. The standard way to deal with this is using a header file such as
                the one I show below and to include it in *both* the file defining the
                function (to ensure the prototype matches) and the file from which it is
                called. If your C text book (and/or tutor) does not explain this they
                need to be replaced.
                [color=blue]
                > printf("b= %f", b);[/color]

                Again, the prototype is required for printf.
                [color=blue]
                > }
                >
                > After compiling it with: cc test1.c test2.c -lm -o test2 and running it
                > the output is: a= 2.540000b= 1076006784.0000 00
                >
                > So the problem is definitely in including file test2... how can i
                > include it right? what's going on in my example?[/color]

                <snip>

                Having made the above changes you create a header file which provides a
                prototype for educate:

                /* test1.h */
                #ifdef TEST1_H
                #define TEST1_H
                float educate(void);
                #endif

                Then the compiler knows what is going on.

                To understand the reason for the #ifdef etc search for include guards.

                I also suggest reading the comp.lang.c FAQ (google will find it) and
                K&R2 (the FAQ will tell you what that is).
                --
                Flash Gordon
                Living in interesting times.
                Although my email address says spam, it is real and I read it.

                Comment

                • Martin Ambuhl

                  #9
                  Re: Strange output

                  unique wrote:[color=blue]
                  > I have written some programs in c lang yet but today I get confused
                  > with output i get.
                  > I have function educate(..) which i call in main program this way:
                  >
                  > J = educate(no_laye rs, no_neurons, input, output, lay, weights, 0.1,0);
                  > printf("J= %f\n",J);
                  >
                  > In the educate function i count J and i return it (but before I return
                  > it I output it with printf):
                  >
                  > float educate (int no_layers, int no_neurons[], float input[], float
                  > output[], float* lay[], float* weights[], float gama, int debug) {
                  > ...
                  > printf("J= %f\n",J);
                  > return(J);
                  > }
                  >
                  > This is what i get:
                  >
                  > J= 0.304447
                  > J= 1050402944.0000 00
                  >
                  > Is there any syntax prob? Why the values arent the same? :(
                  >[/color]

                  $oracle -n
                  ---
                  ---
                  ---
                  ---
                  -X-
                  - -

                  (MWD)
                  Jian over Gen
                  3 Yuan 'Wielding'
                  Wielding: Receipt; little beneficial to determine.

                  (WB)
                  Qian over Gen
                  33 Dun 'Withdrawal'
                  Withdrawal: prevalence is had. It is fitting
                  to practice constancy in small matters.

                  Transforming:
                  second yin
                  (MWD) Uphold it using a yellow ox's bridle;
                  no one will succeed in overturning it,

                  (WB) If one holds then with yellow ox hide,
                  none will manage to break away.

                  Approaches:
                  ---
                  ---
                  ---
                  ---
                  ---
                  - -
                  (MWD)
                  Jian over Suan
                  8 Gou 'Meeting'
                  [Meeting]: The maiden matures; do not herewith take
                  a maiden.

                  (WB)
                  Qian over Sun
                  44 Gou 'Encounter'
                  Encounter: the woman is strong; it would not do to
                  marry this woman.

                  $

                  Comment

                  • Martin Ambuhl

                    #10
                    Re: Strange output

                    unique wrote:[color=blue]
                    > I was investigating the problem a bit and the problem seems to be that
                    > I call in file educate.c function educate(...) from the other file
                    > neuron.c. I dont use any include, so code is here:
                    > ***test1.c***
                    > float educate() {
                    > float a=2.54f;
                    > printf("a= %f", a);
                    > return a;
                    > }
                    >
                    >
                    > ***test2.c***
                    > int main(int argc, char **argv) {
                    > float b=educate();
                    > printf("b= %f", b);
                    > }
                    >
                    > After compiling it with: cc test1.c test2.c -lm -o test2 and running it
                    > the output is: a= 2.540000b= 1076006784.0000 00
                    >
                    > So the problem is definitely in including file test2[/color]

                    WRONG! Your problem is the failure to provide a declaration for
                    educate() in test2.c. The educate() function test2 knows about returns
                    an int (in C89; your code is just broken in C99), while educate() in
                    test1.c returns a float.

                    Your code is broken even if you include a declaration for educate() in
                    test2.c, since you fail to provide the required declaration for the
                    variadic function printf(); that's what <stdio.h> is for.

                    Comment

                    • Barry Schwarz

                      #11
                      Re: Strange output

                      On 9 Oct 2005 06:39:15 -0700, "unique" <julius4@gmail. com> wrote:
                      [color=blue][color=green]
                      >> So the compiler must assume a default of educate() returning an int,[/color]
                      >which of course it doesn't.
                      >
                      >Thank you very much Skarmander, you are completely right, i solved it
                      >with header file.
                      >[/color]
                      In addition to solving it this time, you should up the warning level
                      of your compiler so that it tells anytime you invoke a function for
                      which a prototype is not in scope.


                      <<Remove the del for email>>

                      Comment

                      • Emmanuel Delahaye

                        #12
                        Re: Strange output

                        unique a écrit :[color=blue]
                        > I was investigating the problem a bit and the problem seems to be that
                        > I call in file educate.c function educate(...) from the other file
                        > neuron.c. I dont use any include, so code is here:
                        > ***test1.c***
                        > float educate() {
                        > float a=2.54f;
                        > printf("a= %f", a);
                        > return a;
                        > }
                        >
                        >
                        > ***test2.c***
                        > int main(int argc, char **argv) {
                        > float b=educate();
                        > printf("b= %f", b);
                        > }
                        >
                        > After compiling it with: cc test1.c test2.c -lm -o test2 and running it
                        > the output is: a= 2.540000b= 1076006784.0000 00[/color]

                        This works fine to me (single compile unit)

                        #include <stdio.h>

                        float educate(void)
                        {
                        float a=2.54f;
                        printf("a= %f\n", a);
                        return a;
                        }

                        int main(void)
                        {
                        float b=educate();
                        printf("b= %f\n", b);
                        }

                        Now, if you are gooing to use 2 separated compile units (CUs), you must
                        define a protoype in a common file called a header. This is the proper
                        way of doing it:

                        /* educate.h */
                        #ifndef H_EDUCATE
                        #define H_EDUCATE

                        /* function prototypes */
                        float educate(void);

                        #endif /* guard */

                        Then you must include this header in the definition CU (for consistency)
                        and in all user CU:

                        /* educate.c */
                        #include <stdio.h>
                        #include "educate.h"
                        float educate(void)
                        {
                        float a=2.54f;
                        printf("a= %f\n", a);
                        return a;
                        }

                        /* main.c */
                        #include <stdio.h>
                        #include "educate.h"

                        int main(void)
                        {
                        float b=educate();
                        printf("b= %f\n", b);
                        return 0;
                        }

                        Comment

                        • Emmanuel Delahaye

                          #13
                          Re: Strange output

                          unique a écrit :[color=blue]
                          > I was investigating the problem a bit and the problem seems to be that
                          > I call in file educate.c function educate(...) from the other file
                          > neuron.c. I dont use any include, so code is here:
                          > ***test1.c***
                          > float educate() {
                          > float a=2.54f;
                          > printf("a= %f", a);
                          > return a;
                          > }
                          >
                          >
                          > ***test2.c***
                          > int main(int argc, char **argv) {
                          > float b=educate();
                          > printf("b= %f", b);
                          > }
                          >
                          > After compiling it with: cc test1.c test2.c -lm -o test2 and running it
                          > the output is: a= 2.540000b= 1076006784.0000 00[/color]

                          This works fine to me (single compile unit)

                          #include <stdio.h>

                          float educate(void)
                          {
                          float a=2.54f;
                          printf("a= %f\n", a);
                          return a;
                          }

                          int main(void)
                          {
                          float b=educate();
                          printf("b= %f\n", b);
                          }

                          Now, if you are going to use two separated compile units (CUs), you must
                          define a protoype in a common file called a header. This is the proper
                          way of doing it (the guard protects agains multiple inclusions in a
                          single CU):

                          /* educate.h */
                          #ifndef H_EDUCATE
                          #define H_EDUCATE

                          /* function prototypes */
                          float educate(void);

                          #endif /* guard */

                          Then you must include this header in the definition CU (for consistency)
                          and in all user CU:

                          /* educate.c */
                          #include <stdio.h>
                          #include "educate.h"
                          float educate(void)
                          {
                          float a=2.54f;
                          printf("a= %f\n", a);
                          return a;
                          }

                          /* main.c */
                          #include <stdio.h>
                          #include "educate.h"

                          int main(void)
                          {
                          float b=educate();
                          printf("b= %f\n", b);
                          return 0;
                          }

                          Comment

                          Working...