in reply to Re^2: Maintain accuracy of floating point numbers in simple arithmetic operation
in thread Maintain accuracy of floating point numbers in simple arithmetic operation
... why the + after the first set of parens?
Because I made a mistake and didn't notice because I was getting the right results. It is redundant and removing it makes no difference--'cept maybe it'll run a insy winsy bit quicker? :)
There is a second 'artifact' in the replacement. ($1||'') can become just $1.
I was attempting to avoid the need to suppress warnings, but it turns the code it a mess of bracketed ||s and ternaries; or a long if then else chain. Warnings are optional and turning them off, as an informed decision to avoid complicated code, is (IMO) legit. So I chose the cleaner option. You can replace local $^W; with no warnings 'uninitialised'; or the chain of defined and length tests if you prefer.
Does this explain it?
#! perl -slw use strict; while( <DATA> ) { chomp; printf "Before '%s' becomes ", $_; s[ ( \d+ ) ## Grab any digits before a decimal point (?: \. ## If there is a decimal point ( \d{0,2} ) ## Try grab two digits after it ( \d* ) ## And any more into another capture )? ## All conditionally % ## And finally... ]{ local $^W; ## I know some of the captures will be emp +ty. ## Pad (trailing zeros) the digits being moved to ensure exact +ly 2 my $n = substr( $2 . '00', 0, 2 ); ## Reassemble the number from ## Any digits previously before the decimal point ## The two digits being moved ## A decimal point and the remaining digits if there are +any. $1 . $n . ( length($3) ? ".$3" : '' ); }xe; print "'$_'"; } __DATA__
|
|---|