in reply to Improve my Code: Subroutines

my $r = ($i)*(1e-6); #radii are in microns

I would keep the value of the radii as an integer as long as possible and only "scale" it in the actual calculation. Integers will be presented without any loss of precision in Perl, but once they are turned into a real, it is not certain that they are still exact as their internal representation may involve a repeating sequence of bits, which may be a little bit more or less than you expect and tests such as if ($r<2e-5) may give unexpected results in edge-cases. In this case, it seems to be OK.

An interesting discussion on this issue can be found in My floating point comparison does not work. Why ?.

Running your code through Perl::Critc gives the following:

Code before strictures are enabled at line 1, column 1. See page 429 +of PBP. Severity: 5 Code before warnings are enabled at line 1, column 1. See page 431 of + PBP. Severity: 4 Use "<>" or "" or a prompting module instead of "" at line 1, column 9 +. See pages 216,220,221 of PBP. Severity: 4 Always unpack @_ first at line 29, column 1. See page 178 of PBP. Se +verity: 4 Magic variable "$a" should be assigned as "local" at line 35, column 4 +. See pages 81,82 of PBP. Severity: 4 Always unpack @_ first at line 46, column 1. See page 178 of PBP. Se +verity: 4
Nothing too bad and all of these critics can be easily mended.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: Improve my Code: Subroutines
by afoken (Chancellor) on Jun 14, 2009 at 14:55 UTC
    my $r = ($i)*(1e-6);    #radii are in microns

    Why do you put both factors in brackets?

    Integers will be presented without any loss of precision in Perl

    But only as long as the number "fits" into perls integer type, which typically is a 32 bit signed integer or 64 bit signed integer. Once you exceed the limit (+/- 231 resp. +/- 263), perl silently converts the value to a floating point number with loss of precision (perlnumber).

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Why do you put both factors in brackets?
      I just copied it from the OP. There is no harm to using brackets here, they are just superfluous.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James