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

Re: [PBML] Evaluating the expression in if VS While

Expand Messages
  • 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 1 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.
    • 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 2 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
        >
      • 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 3 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 4 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 5 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 6 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.