in reply to incrementation loop fails?

perlfaq4#Why-am-I-getting-long-decimals

Use an integer counter, divide by 10 where you need the floating point.

Replies are listed 'Best First'.
Re^2: incrementation loop fails?
by herveus (Prior) on Sep 29, 2011 at 18:00 UTC
    Howdy!

    That only works when the precision is 0.1; that cannot be assumed as it is user input. Using printf to limit the precision of the output is one way, but you have to be careful with your limits, based on the user's specified precision. One digit would not be helpful if the precision were 0.01, to haul out an obvious example.

    yours,
    Michael
Re^2: incrementation loop fails?
by kennethk (Abbot) on Sep 29, 2011 at 19:03 UTC
    Why divide, when multiplication handles irregular strides and avoids the need for sprintfs?
    sub DIRICHLET { my ($POWERS,$PRECISION) = @_; print "\n\n\t Power \t Dirichlet's zeta result (harmonic series dep +th = $SERIES_DEPTH)\n\n"; my $start = 2; my $i = 0; while ( (my $POWER = $start + $PRECISION*$i++) < $POWERS+1) { print "\t $POWER \t " . &HARMONIC_SERIES ($POWER) . " \n"; } } # DIRICHLET #