in reply to Simple calculation.

Here's what I got so far. The code's not great, can it be improved?

Can you think of a set of inputs that will exercise it better? (Note: I haven't got around to log() scales yet.)

#! perl -slw use strict; sub log10{ log( $_[0] ) / log( 10 ) } sub ticks { my( $start, $end, $n ) = @_; $n ||= 10; my $range = $end - $start; $range += $range / $n; my $interval = $range / $n; $interval = 10**int( log10( $interval ) ); $interval *= 10 while ( $range / $interval ) > ( $n * 2 ); $interval /= 2 while ( $range / $interval ) < ( $n / 2 ); $start = int( $start / $interval ) * $interval; my @ticks; while( $start < ( $end + $interval ) ) { push @ticks, $start; $start += $interval; } return @ticks; } for my $range ( [0,9], [0.123,8.673], [0.022, 0.086], [1_123_345_456, 1_124_246_357.5], [1_123_345_456, 1_123_346_357.5] +, [ 0.00011, 0.00041], ) { for my $n ( 0, 4, 5, 8 , 10, 15, 16, 20, 21 ) { my @ticks = ticks( @$range, $n ); printf "from:%12.12g to %12.12g by:%2u -> [%2u][ %s ]\n", @$range, $n, scalar( @ticks ), join ' ', map{ my $s = sprintf( "%.8g", $_ ); $s =~ s[e([+-])0*([1-9] ++)]['e' . ($1 eq '-' ? '-' : '') . $2]e; $s; } @ticks; } }

Output:

C:\test>calcTicks.pl from: 0 to 9 by: 0 -> [10][ 0 1 2 3 4 5 6 7 8 9 ] from: 0 to 9 by: 4 -> [ 3][ 0 5 10 ] from: 0 to 9 by: 5 -> [ 5][ 0 2.5 5 7.5 10 ] from: 0 to 9 by: 8 -> [10][ 0 1 2 3 4 5 6 7 8 9 ] from: 0 to 9 by:10 -> [10][ 0 1 2 3 4 5 6 7 8 9 ] from: 0 to 9 by:15 -> [10][ 0 1 2 3 4 5 6 7 8 9 ] from: 0 to 9 by:16 -> [10][ 0 1 2 3 4 5 6 7 8 9 ] from: 0 to 9 by:20 -> [19][ 0 0.5 1 1.5 2 2.5 3 3 +.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 ] from: 0 to 9 by:21 -> [19][ 0 0.5 1 1.5 2 2.5 3 3 +.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 ] from: 0.123 to 8.673 by: 0 -> [10][ 0 1 2 3 4 5 6 7 8 9 ] from: 0.123 to 8.673 by: 4 -> [ 3][ 0 5 10 ] from: 0.123 to 8.673 by: 5 -> [ 5][ 0 2.5 5 7.5 10 ] from: 0.123 to 8.673 by: 8 -> [10][ 0 1 2 3 4 5 6 7 8 9 ] from: 0.123 to 8.673 by:10 -> [10][ 0 1 2 3 4 5 6 7 8 9 ] from: 0.123 to 8.673 by:15 -> [10][ 0 1 2 3 4 5 6 7 8 9 ] from: 0.123 to 8.673 by:16 -> [10][ 0 1 2 3 4 5 6 7 8 9 ] from: 0.123 to 8.673 by:20 -> [19][ 0 0.5 1 1.5 2 2.5 3 3 +.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 ] from: 0.123 to 8.673 by:21 -> [19][ 0 0.5 1 1.5 2 2.5 3 3 +.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 ] from: 0.022 to 0.086 by: 0 -> [ 8][ 0.02 0.03 0.04 0.05 0 +.06 0.07 0.08 0.09 ] from: 0.022 to 0.086 by: 4 -> [ 5][ 0 0.025 0.05 0.075 0. +1 ] from: 0.022 to 0.086 by: 5 -> [ 5][ 0 0.025 0.05 0.075 0. +1 ] from: 0.022 to 0.086 by: 8 -> [ 8][ 0.02 0.03 0.04 0.05 0 +.06 0.07 0.08 0.09 ] from: 0.022 to 0.086 by:10 -> [ 8][ 0.02 0.03 0.04 0.05 0 +.06 0.07 0.08 0.09 ] from: 0.022 to 0.086 by:15 -> [15][ 0.02 0.025 0.03 0.035 + 0.04 0.045 0.05 0.055 0.06 0.065 0.07 0.075 0.08 0.085 0.09 ] from: 0.022 to 0.086 by:16 -> [15][ 0.02 0.025 0.03 0.035 + 0.04 0.045 0.05 0.055 0.06 0.065 0.07 0.075 0.08 0.085 0.09 ] from: 0.022 to 0.086 by:20 -> [15][ 0.02 0.025 0.03 0.035 + 0.04 0.045 0.05 0.055 0.06 0.065 0.07 0.075 0.08 0.085 0.09 ] from: 0.022 to 0.086 by:21 -> [15][ 0.02 0.025 0.03 0.035 + 0.04 0.045 0.05 0.055 0.06 0.065 0.07 0.075 0.08 0.085 0.09 ] from: 1123345456 to 1124246357.5 by: 0 -> [11][ 1.1233e9 1.1234e9 1.1 +235e9 1.1236e9 1.1237e9 1.1238e9 1.1239e9 1.124e9 1.1241e9 1.1242e9 1 +.1243e9 ] from: 1123345456 to 1124246357.5 by: 4 -> [ 4][ 1.123e9 1.1235e9 1.12 +4e9 1.1245e9 ] from: 1123345456 to 1124246357.5 by: 5 -> [ 5][ 1.12325e9 1.1235e9 1. +12375e9 1.124e9 1.12425e9 ] from: 1123345456 to 1124246357.5 by: 8 -> [11][ 1.1233e9 1.1234e9 1.1 +235e9 1.1236e9 1.1237e9 1.1238e9 1.1239e9 1.124e9 1.1241e9 1.1242e9 1 +.1243e9 ] from: 1123345456 to 1124246357.5 by:10 -> [11][ 1.1233e9 1.1234e9 1.1 +235e9 1.1236e9 1.1237e9 1.1238e9 1.1239e9 1.124e9 1.1241e9 1.1242e9 1 +.1243e9 ] from: 1123345456 to 1124246357.5 by:15 -> [11][ 1.1233e9 1.1234e9 1.1 +235e9 1.1236e9 1.1237e9 1.1238e9 1.1239e9 1.124e9 1.1241e9 1.1242e9 1 +.1243e9 ] from: 1123345456 to 1124246357.5 by:16 -> [11][ 1.1233e9 1.1234e9 1.1 +235e9 1.1236e9 1.1237e9 1.1238e9 1.1239e9 1.124e9 1.1241e9 1.1242e9 1 +.1243e9 ] from: 1123345456 to 1124246357.5 by:20 -> [20][ 1.1233e9 1.12335e9 1. +1234e9 1.12345e9 1.1235e9 1.12355e9 1.1236e9 1.12365e9 1.1237e9 1.123 +75e9 1.1238e9 1.12385e9 1.1239e9 1.12395e9 1.124e9 1.12405e9 1.1241e9 + 1.12415e9 1.1242e9 1.12425e9 ] from: 1123345456 to 1124246357.5 by:21 -> [20][ 1.1233e9 1.12335e9 1. +1234e9 1.12345e9 1.1235e9 1.12355e9 1.1236e9 1.12365e9 1.1237e9 1.123 +75e9 1.1238e9 1.12385e9 1.1239e9 1.12395e9 1.124e9 1.12405e9 1.1241e9 + 1.12415e9 1.1242e9 1.12425e9 ] from: 1123345456 to 1123346357.5 by: 0 -> [11][ 1.1233454e9 1.1233455 +e9 1.1233456e9 1.1233457e9 1.1233458e9 1.1233459e9 1.123346e9 1.12334 +61e9 1.1233462e9 1.1233463e9 1.1233464e9 ] from: 1123345456 to 1123346357.5 by: 4 -> [ 4][ 1.123345e9 1.1233455e +9 1.123346e9 1.1233465e9 ] from: 1123345456 to 1123346357.5 by: 5 -> [ 6][ 1.1233453e9 1.1233455 +e9 1.1233458e9 1.123346e9 1.1233463e9 1.1233465e9 ] from: 1123345456 to 1123346357.5 by: 8 -> [11][ 1.1233454e9 1.1233455 +e9 1.1233456e9 1.1233457e9 1.1233458e9 1.1233459e9 1.123346e9 1.12334 +61e9 1.1233462e9 1.1233463e9 1.1233464e9 ] from: 1123345456 to 1123346357.5 by:10 -> [11][ 1.1233454e9 1.1233455 +e9 1.1233456e9 1.1233457e9 1.1233458e9 1.1233459e9 1.123346e9 1.12334 +61e9 1.1233462e9 1.1233463e9 1.1233464e9 ] from: 1123345456 to 1123346357.5 by:15 -> [11][ 1.1233454e9 1.1233455 +e9 1.1233456e9 1.1233457e9 1.1233458e9 1.1233459e9 1.123346e9 1.12334 +61e9 1.1233462e9 1.1233463e9 1.1233464e9 ] from: 1123345456 to 1123346357.5 by:16 -> [11][ 1.1233454e9 1.1233455 +e9 1.1233456e9 1.1233457e9 1.1233458e9 1.1233459e9 1.123346e9 1.12334 +61e9 1.1233462e9 1.1233463e9 1.1233464e9 ] from: 1123345456 to 1123346357.5 by:20 -> [20][ 1.1233455e9 1.1233455 +e9 1.1233456e9 1.1233456e9 1.1233457e9 1.1233457e9 1.1233458e9 1.1233 +458e9 1.1233459e9 1.1233459e9 1.123346e9 1.123346e9 1.1233461e9 1.123 +3461e9 1.1233462e9 1.1233462e9 1.1233463e9 1.1233463e9 1.1233464e9 1. +1233464e9 ] from: 1123345456 to 1123346357.5 by:21 -> [20][ 1.1233455e9 1.1233455 +e9 1.1233456e9 1.1233456e9 1.1233457e9 1.1233457e9 1.1233458e9 1.1233 +458e9 1.1233459e9 1.1233459e9 1.123346e9 1.123346e9 1.1233461e9 1.123 +3461e9 1.1233462e9 1.1233462e9 1.1233463e9 1.1233463e9 1.1233464e9 1. +1233464e9 ] from: 0.00011 to 0.00041 by: 0 -> [ 8][ 0.0001 0.00015 0.0002 + 0.00025 0.0003 0.00035 0.0004 0.00045 ] from: 0.00011 to 0.00041 by: 4 -> [ 5][ 0.0001 0.0002 0.0003 +0.0004 0.0005 ] from: 0.00011 to 0.00041 by: 5 -> [ 5][ 0.0001 0.0002 0.0003 +0.0004 0.0005 ] from: 0.00011 to 0.00041 by: 8 -> [ 8][ 0.0001 0.00015 0.0002 + 0.00025 0.0003 0.00035 0.0004 0.00045 ] from: 0.00011 to 0.00041 by:10 -> [ 8][ 0.0001 0.00015 0.0002 + 0.00025 0.0003 0.00035 0.0004 0.00045 ] from: 0.00011 to 0.00041 by:15 -> [14][ 0.0001 0.000125 0.000 +15 0.000175 0.0002 0.000225 0.00025 0.000275 0.0003 0.000325 0.00035 +0.000375 0.0004 0.000425 ] from: 0.00011 to 0.00041 by:16 -> [14][ 0.0001 0.000125 0.000 +15 0.000175 0.0002 0.000225 0.00025 0.000275 0.0003 0.000325 0.00035 +0.000375 0.0004 0.000425 ] from: 0.00011 to 0.00041 by:20 -> [14][ 0.0001 0.000125 0.000 +15 0.000175 0.0002 0.000225 0.00025 0.000275 0.0003 0.000325 0.00035 +0.000375 0.0004 0.000425 ] from: 0.00011 to 0.00041 by:21 -> [14][ 0.0001 0.000125 0.000 +15 0.000175 0.0002 0.000225 0.00025 0.000275 0.0003 0.000325 0.00035 +0.000375 0.0004 0.000425 ]

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked