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

In the following runs of the test code below, when

Maybe I'm having a "thick" day today, but for the life of me, I cannot see why?

C:\test>junk0 -K=1e2 K:1e2 L:2 bd => 21537 ux => 27554 su => 32707 db => 10843 fe => 3691 aj => 23335 co => 12919 br => 21357 xw => 15950 hd => 24808 C:\test>junk0 -K=1e3 K:1e3 L:3 ne => 32586 qz => 4931 sp => 30318 tr => 9694 gl => 21489 hn => 6456 xx => 24771 sf => 3369 ro => 27152 pv => 1438 C:\test>junk0 -K=1e4 K:1e4 L:4 alof => 16425 tjtd => 16200 pief => 25920 nlly => 18027 oezy => 19290 eyzv => 5510 ztxl => 11976 yqbz => 8356 hoym => 6193 exid => 29312

The code (run as perl -s scriptname -K=nnn):

#! perl -slw use strict; sub rndStr{ join'', @_[ map{ rand @_ } 1 .. shift ] } sub log10{ log( $_[0] ) / log( 10 ) } our $K ||= 1e3; our $L ||= log10( $K ); print "K:$K L:$L"; my %hash; $hash{ $_ } = int rand 32767 for map{ rndStr( $L, 'a'..'z' ) } 1 .. $K +; print "$_ => $hash{ $_ }" for ( keys %hash )[ 0 .. 9 ];

'Unthick' me please :)


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re: My computer doesn't like the number 3? (Forget it! It's rounding!!!)
by FunkyMonk (Bishop) on Apr 08, 2008 at 17:59 UTC
    It's floating point rounding errors - $L is just a smidgin under 3, but appears to be exactly 2 & 4 for 10**2 & 10**4.

    our $K ||= 1e3; our $L ||=log10( $K ); print $L-3; # -4.44089209850063e-16

Re: My computer doesn't like the number 3? (Forget it! It's rounding!!!)
by ikegami (Patriarch) on Apr 09, 2008 at 00:56 UTC

    A little bit of light for those who didn't follow:

    sub log10{ log( $_[0] ) / log( 10 ) } printf("%.16e\n", log10($_)) for 1e2, 1e3, 1e4;
    2.0000000000000000e+000 2.9999999999999996e+000 4.0000000000000000e+000