Maahi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Guys, I am beginner of perl. I craeted a small program using while loop but its output is cumbersome. Can anybody provide me some suggestion on it. Below are the code and output details:
#!usr/bin/perl
print " This program will print all 10 decimal places.\n";
print "Enter an integer value.\n";
$value =<STDIN>;
#$value=1;
#chop($value);
$a = $value+1;
print "$a\n";
while ($value<$a) {
##      if($value == 9){
##              #exit;
##              print "Value is now 9\n";
##      }
        print "value has become $value\n";
        $value=$value+0.1;
##      print "Value after increment is : $value\n"
}
print "EOF.\n";
~
output:
1.When I entered 3:
 This program will print all 10 decimal places.
Enter an integer value.
3
4
value has become 3

Value after increment is : 3.1
value has become 3.1
Value after increment is : 3.2
value has become 3.2
Value after increment is : 3.3
value has become 3.3
Value after increment is : 3.4
value has become 3.4
Value after increment is : 3.5
value has become 3.5
Value after increment is : 3.6
value has become 3.6
Value after increment is : 3.7
value has become 3.7
Value after increment is : 3.8
value has become 3.8
Value after increment is : 3.9
value has become 3.9
Value after increment is : 4
EOF.
2. when I entered 10:
This program will print all 10 decimal places.
Enter an integer value.
10
11
value has become 10

value has become 10.1
value has become 10.2
value has become 10.3
value has become 10.4
value has become 10.5
value has become 10.6
value has become 10.7
value has become 10.8
value has become 10.9
value has become 11
EOF.
There are two different outputs: When I enter number 1 to 4 (1,2,3,4) then output is what is expected but when input is greater than 4 then output is different. 

Replies are listed 'Best First'.
Re: While loop;s different behavior
by huck (Prior) on Aug 31, 2017 at 06:28 UTC

    You are not doing decimal math, but binary math. Perhaps the following will help you understand.

    #!usr/bin/perl print " This program will print all 10 decimal places.\n"; print "Enter an integer value.\n"; $value =<STDIN>; #$value=1; #chop($value); $a = $value+1; print "$a\n"; while ($value<$a) { print "value has become $value\n"; $value=$value+0.1; print "Value after increment is :".sprintf('%20.17f',$value)." +\n" } print "EOF.\n";
    result
    This program will print all 10 decimal places. Enter an integer value. 10 11 value has become 10 Value after increment is :10.10000000000000000 value has become 10.1 Value after increment is :10.19999999999999900 value has become 10.2 Value after increment is :10.29999999999999900 value has become 10.3 Value after increment is :10.39999999999999900 value has become 10.4 Value after increment is :10.49999999999999800 value has become 10.5 Value after increment is :10.59999999999999800 value has become 10.6 Value after increment is :10.69999999999999800 value has become 10.7 Value after increment is :10.79999999999999700 value has become 10.8 Value after increment is :10.89999999999999700 value has become 10.9 Value after increment is :10.99999999999999600 value has become 11 Value after increment is :11.09999999999999600 EOF.

Re: While loop;s different behavior
by thanos1983 (Parson) on Aug 31, 2017 at 08:07 UTC

    Hello Maahi,

    Welcome to the Monastery. Although the fellow Monk huck has provided you with a solution to your problem. I think you can still learn a few more things by reading this similar question perl floating number addition, with solutions provided to your problem.

    Just for your information in Perl if your search the web 90% if not 99% you will find someone has created a module that can do exactly what you want to accomplish. For example Math::Decimal, this is the beauty of Perl.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: While loop;s different behavior
by AnomalousMonk (Archbishop) on Aug 31, 2017 at 17:20 UTC
Re: While loop;s different behavior
by Anonymous Monk on Aug 31, 2017 at 14:40 UTC
    As others have pointed out, the number 0.1 is approximate in binary floating point. Thus, you should only use integers as loop controls.
    print $value+$_/10, "\n" for 0 .. 10;