Loading ...
Sorry, an error occurred while loading the content.

Evaluating the expression in if VS While

Expand Messages
  • edu_kumar
    Hi all, I m a beginner in Perl and I slowly started writing small programs in it. While I was playing with the if and while loops in Perl, I got into a really
    Message 1 of 10 , Jun 2, 2009
    • 0 Attachment
      Hi all,

      I'm a beginner in Perl and I slowly started writing small programs in it. While I was playing with the if and while loops in Perl, I got into a really weird scenario. Please look at the snippet of code below..

      -----------------------Program starts here-------------------------
      #!/usr/bin/perl

      $index = 0;
      $s = 1;
      if($s<1){
      print("Hey, in perl its different man ! ");
      }
      else
      {
      print("The whole world stays as is..! \n");
      }

      while($index <1) {
      print ("$index < 1 is true! \n");
      $index += 0.1;

      print ("$index\n");

      ---------------------------Program ends here -----------

      the output I got is pretty weird. Here's the output...

      -- This is the output of the if statement and it looks very fine to me.

      The whole world stays as is..!

      -- However, this is the output of the While loop. Strangely the condition ( 1 < 1 ) evaluates to false in case of if loop and true in case of while loop.

      0 < 1 is true!
      0.1
      0.1 < 1 is true!
      0.2
      0.2 < 1 is true!
      0.3
      0.3 < 1 is true!
      0.4
      0.4 < 1 is true!
      0.5
      0.5 < 1 is true!
      0.6
      0.6 < 1 is true!
      0.7
      0.7 < 1 is true!
      0.8
      0.8 < 1 is true!
      0.9
      0.9 < 1 is true!
      1
      1 < 1 is true!
      1.1


      Obviously I might be doing something stupid here and I wasn't able to figure it out. can someone point me here in the right direction ? given below is my perl version..

      ------------------------------------------------
      bash-3.00$ /usr/bin/perl -version

      This is perl, v5.8.4 built for i86pc-solaris-64int
      (with 29 registered patches, see perl -V for more detail)

      Copyright 1987-2004, Larry Wall

      Perl may be copied only under the terms of either the Artistic License or the
      GNU General Public License, which may be found in the Perl 5 source kit.

      Complete documentation for Perl, including FAQ lists, should be found on
      this system using `man perl' or `perldoc perl'. If you have access to the
      Internet, point your browser at http://www.perl.com/, the Perl Home Page.

      ------------------------------------------------
    • Charles K. Clarkson
      ... Generally, algorithms should avoid comparing integer values to floating point values. Now you know why. HTH, Charles Clarkson -- Mobile Home Investor Free
      Message 2 of 10 , Jun 2, 2009
      • 0 Attachment
        edu_kumar wrote:


        > Strangely the condition ( 1 < 1 ) evaluates to false

        Generally, algorithms should avoid comparing integer values to
        floating point values. Now you know why.



        HTH,

        Charles Clarkson
        --
        Mobile Home Investor
        Free Market Advocate
        Programmer

        I'm not really a smart person. I just play one on the Internet.

        Stephenville, TX
        http://www.clarksonenergyhomes.com/wordpress/about/
        http://twitter.com/CharlesClarkson
        +1 (254) 968-8328
      • Shlomi Fish
        ... A few notes about your code: 1. You re missing use strict; and use warnings; . Please add them. 2. Your first print is missing a n . 3. You are
        Message 3 of 10 , Jun 2, 2009
        • 0 Attachment
          On Tuesday 02 June 2009 15:59:50 edu_kumar wrote:
          > Hi all,
          >
          > I'm a beginner in Perl and I slowly started writing small programs in it.
          > While I was playing with the if and while loops in Perl, I got into a
          > really weird scenario. Please look at the snippet of code below..
          >
          > -----------------------Program starts here-------------------------
          > #!/usr/bin/perl
          >
          > $index = 0;
          > $s = 1;
          > if($s<1){
          > print("Hey, in perl its different man ! ");
          > }
          > else
          > {
          > print("The whole world stays as is..! \n");
          > }
          >
          > while($index <1) {
          > print ("$index < 1 is true! \n");
          > $index += 0.1;
          >
          > print ("$index\n");
          >
          > ---------------------------Program ends here -----------
          >

          A few notes about your code:

          1. You're missing "use strict;" and "use warnings;". Please add them.

          2. Your first print is missing a "\n".

          3. You are missing the closing right brace of the while loop.

          > the output I got is pretty weird. Here's the output...
          >
          > -- This is the output of the if statement and it looks very fine to me.
          >
          > The whole world stays as is..!
          >
          > -- However, this is the output of the While loop. Strangely the condition (
          > 1 < 1 ) evaluates to false in case of if loop and true in case of while
          > loop.

          That's because you're using the variable $index in the while loop which is
          initially set to 0. And 0 < 1. If you've used $s then the while loop would
          have terminated.

          Regards,

          Shlomi Fish

          >
          > 0 < 1 is true!
          > 0.1
          > 0.1 < 1 is true!
          > 0.2
          > 0.2 < 1 is true!
          > 0.3
          > 0.3 < 1 is true!
          > 0.4
          > 0.4 < 1 is true!
          > 0.5
          > 0.5 < 1 is true!
          > 0.6
          > 0.6 < 1 is true!
          > 0.7
          > 0.7 < 1 is true!
          > 0.8
          > 0.8 < 1 is true!
          > 0.9
          > 0.9 < 1 is true!
          > 1
          > 1 < 1 is true!
          > 1.1
          >
          >
          > Obviously I might be doing something stupid here and I wasn't able to
          > figure it out. can someone point me here in the right direction ? given
          > below is my perl version..
          >
          > ------------------------------------------------
          > bash-3.00$ /usr/bin/perl -version
          >
          > This is perl, v5.8.4 built for i86pc-solaris-64int
          > (with 29 registered patches, see perl -V for more detail)
          >
          > Copyright 1987-2004, Larry Wall
          >
          > Perl may be copied only under the terms of either the Artistic License or
          > the GNU General Public License, which may be found in the Perl 5 source
          > kit.
          >
          > Complete documentation for Perl, including FAQ lists, should be found on
          > this system using `man perl' or `perldoc perl'. If you have access to the
          > Internet, point your browser at http://www.perl.com/, the Perl Home Page.
          >
          > ------------------------------------------------

          --
          -----------------------------------------------------------------
          Shlomi Fish http://www.shlomifish.org/
          Best Introductory Programming Language - http://xrl.us/bjn84

          God gave us two eyes and ten fingers so we will type five times as much as we
          read.
        • merlyn@stonehenge.com
          ... edu while($index print ( $index
          Message 4 of 10 , Jun 2, 2009
          • 0 Attachment
            >>>>> "edu" == edu kumar <sarat.beesa@...> writes:

            edu> while($index <1) {
            edu> print ("$index < 1 is true! \n");
            edu> $index += 0.1;

            edu> print ("$index\n");

            edu> ---------------------------Program ends here -----------

            edu> the output I got is pretty weird. Here's the output...

            This is one of the FAQs. It'd be good to get familiar with the
            Perl FAQ, especially if you're a beginner.

            In particular, "perldoc -q int" says:

            ....
            Found in /usr/libdata/perl5/pod/perlfaq4.pod
            Why is int() broken?
            Your int() is most probably working just fine. It's the numbers that
            aren't quite what you think.

            First, see the above item "Why am I getting long decimals (eg,
            19.9499999999999) instead of the numbers I should be getting (eg,
            19.95)?".

            For example, this

            print int(0.6/0.2-2), "\n";

            will in most computers print 0, not 1, because even such simple numbers
            as 0.6 and 0.2 cannot be presented exactly by floating-point numbers.
            What you think in the above as 'three' is really more like
            2.9999999999999995559.

            --
            Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
            <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
            Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
            See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
          • edu_kumar
            Hey Charles, Thanks very much for your response. It truly an eye opener, i guess i m still thinking of it as Java since I was more of a java programmer till
            Message 5 of 10 , Jun 3, 2009
            • 0 Attachment
              Hey Charles,

              Thanks very much for your response. It truly an eye opener, i guess i'm still thinking of it as Java since I was more of a java programmer till now.

              I converted those numbers into integers explicitly and it worked properly as expected.

              Thanks again, I really appreciate it.


              --- In perl-beginner@yahoogroups.com, "Charles K. Clarkson" <cclarkson@...> wrote:
              >
              > edu_kumar wrote:
              >
              >
              > > Strangely the condition ( 1 < 1 ) evaluates to false
              >
              > Generally, algorithms should avoid comparing integer values to
              > floating point values. Now you know why.
              >
              >
              >
              > HTH,
              >
              > Charles Clarkson
              > --
              > Mobile Home Investor
              > Free Market Advocate
              > Programmer
              >
              > I'm not really a smart person. I just play one on the Internet.
              >
              > Stephenville, TX
              > http://www.clarksonenergyhomes.com/wordpress/about/
              > http://twitter.com/CharlesClarkson
              > +1 (254) 968-8328
              >
            • Robert Lee Binkley
              You are adding after the fact and printing then it goes back to the test. If I move up the print to before the print true, here is the output: [C:/Common]
              Message 6 of 10 , Jun 3, 2009
              • 0 Attachment
                You are adding after the fact and printing then it goes back to the
                test. If I move up the print to before the print true, here is the output:

                [C:/Common] aapl013j
                The whole world stays as is..!
                0
                0 < 1 is true!
                0.1
                0.1 < 1 is true!
                0.2
                0.2 < 1 is true!
                0.3
                0.3 < 1 is true!
                0.4
                0.4 < 1 is true!
                0.5
                0.5 < 1 is true!
                0.6
                0.6 < 1 is true!
                0.7
                0.7 < 1 is true!
                0.8
                0.8 < 1 is true!
                0.9
                0.9 < 1 is true!
                1
                1 < 1 is true!



                Which is what you are after I believe.....





                From: perl-beginner@yahoogroups.com [mailto:perl-beginner@yahoogroups.com]
                On Behalf Of edu_kumar
                Sent: Tuesday, June 02, 2009 8:00 AM
                To: perl-beginner@yahoogroups.com
                Subject: [PBML] Evaluating the expression in if VS While








                Hi all,

                I'm a beginner in Perl and I slowly started writing small programs in it.
                While I was playing with the if and while loops in Perl, I got into a really
                weird scenario. Please look at the snippet of code below..

                -----------------------Program starts here-------------------------
                #!/usr/bin/perl

                $index = 0;
                $s = 1;
                if($s<1){
                print("Hey, in perl its different man ! ");
                }
                else
                {
                print("The whole world stays as is..! \n");
                }

                while($index <1) {
                print ("$index < 1 is true! \n");
                $index += 0.1;

                print ("$index\n");

                ---------------------------Program ends here -----------

                the output I got is pretty weird. Here's the output...

                -- This is the output of the if statement and it looks very fine to me.

                The whole world stays as is..!

                -- However, this is the output of the While loop. Strangely the condition (
                1 < 1 ) evaluates to false in case of if loop and true in case of while
                loop.

                0 < 1 is true!
                0.1
                0.1 < 1 is true!
                0.2
                0.2 < 1 is true!
                0.3
                0.3 < 1 is true!
                0.4
                0.4 < 1 is true!
                0.5
                0.5 < 1 is true!
                0.6
                0.6 < 1 is true!
                0.7
                0.7 < 1 is true!
                0.8
                0.8 < 1 is true!
                0.9
                0.9 < 1 is true!
                1
                1 < 1 is true!
                1.1

                Obviously I might be doing something stupid here and I wasn't able to figure
                it out. can someone point me here in the right direction ? given below is my
                perl version..

                ------------------------------------------------
                bash-3.00$ /usr/bin/perl -version

                This is perl, v5.8.4 built for i86pc-solaris-64int
                (with 29 registered patches, see perl -V for more detail)

                Copyright 1987-2004, Larry Wall

                Perl may be copied only under the terms of either the Artistic License or
                the
                GNU General Public License, which may be found in the Perl 5 source kit.

                Complete documentation for Perl, including FAQ lists, should be found on
                this system using `man perl' or `perldoc perl'. If you have access to the
                Internet, point your browser at http://www.perl.com/, the Perl Home Page.

                ------------------------------------------------





                [Non-text portions of this message have been removed]
              • merlyn@stonehenge.com
                ... edu Thanks very much for your response. It truly an eye opener, i guess i m edu still thinking of it as Java since I was more of a java programmer till
                Message 7 of 10 , Jun 3, 2009
                • 0 Attachment
                  >>>>> "edu" == edu kumar <sarat.beesa@...> writes:

                  edu> Thanks very much for your response. It truly an eye opener, i guess i'm
                  edu> still thinking of it as Java since I was more of a java programmer till
                  edu> now.

                  Actually, that floating point issue is true for *all* languages.

                  Perl is doing this as accurately as C does it, which is also how
                  Java and Fortran do it. And Python. And Ruby.

                  This is a problem of *all* Floating Point values that use the modern accepted
                  "IEEE 754" representation. Namely, that 0.1 is not a precise number in
                  binary. So 10 times that is not precisely 1.0, necessarily.

                  --
                  Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                  <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                  Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                  See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
                • merlyn@stonehenge.com
                  ... Charles Generally, algorithms should avoid comparing integer values to Charles floating point values. Now you know why. Even comparing floats to floats
                  Message 8 of 10 , Jun 3, 2009
                  • 0 Attachment
                    >>>>> "Charles" == Charles K Clarkson <cclarkson@...> writes:

                    Charles> Generally, algorithms should avoid comparing integer values to
                    Charles> floating point values. Now you know why.

                    Even comparing floats to floats might hurt.

                    The better thing to repeat as a mantra is:

                    Floating point values are *always* an approximation.
                    Floating point values are *always* an approximation.
                    Floating point values are *always* an approximation.
                    Floating point values are *always* an approximation.

                    Except in a few rare cases, but there's nearly an infinite number
                    of cases that outweigh them. :)
                    --
                    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                    <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                    Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
                    See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
                  • murali iyer
                      [Non-text portions of this message have been removed]
                    Message 9 of 10 , Jun 7, 2009
                    • 0 Attachment
                       




                      [Non-text portions of this message have been removed]
                    • Jeff Pinyan
                      In case you didn t see (and I assume you didn t): ... -- The Cross Reference - http://thecrossreference.blogspot.com/ Critical Mass (The Science of the
                      Message 10 of 10 , Jun 7, 2009
                      • 0 Attachment
                        In case you didn't see (and I assume you didn't):

                        To unsubscribe from this group, send an email to:
                        > perl-beginner-unsubscribe@yahoogroups.com
                        >

                        --
                        The Cross Reference - http://thecrossreference.blogspot.com/
                        Critical Mass (The Science of the Liturgy) -
                        http://romanliturgy.blogspot.com/

                        [Mary said,] "Do whatever he tells you." ~ John 2:5


                        [Non-text portions of this message have been removed]
                      Your message has been successfully submitted and would be delivered to recipients shortly.