in reply to Re^2: Calculating base difference of numbers
in thread Calculating base difference of numbers
In the examples above I'm looking to see the base point movements of +1,+4 and -5.
I'm not really sure I understand, but maybe something like this?
#!/usr/bin/perl use strict; use warnings; print base_point_movement($_),"\n" for ( 1.5553 - 1.5552, 0.9984 - 0.998, 100.25 - 100.3, ); sub base_point_movement { my $diff = shift; (sprintf "%+e", $diff) =~ /([+-]\d)\./; return $1; }
Output:
+1 +4 -5
Update: as has been pointed out, this only works for +/-9 — which is not too surprising, as it just extracts the before-comma digit of the difference in exponential floating-point representation (not knowing what a "base point" is defined as, I figured it might suffice...).
Anyhow, here's another variant, kind of extending the idea to what might have been meant — though honestly, I don't have the foggiest whether that's what the OP had in mind. (Of course, this will also run into problems when the number of significant digits exceeds floating-point precision.)
sub base_point_movement { my $bpm = sprintf "%+e", shift; $bpm =~ s/0*e.*$//; $bpm =~ tr/.//d; return $bpm; } print base_point_movement($_),"\n" for ( 1.5553 - 1.5552, # +1 1.5553 - 1.55, # +53 1.56 - 1.55, # +1 155.53 - 155.52, # +1 15553 - 15552, # +1 1555300 - 1555200, # +1 1555300 - 155520, # +139978 1555.300 - 1555.20, # +1 1555300 - 1555201, # +99 1.5553 - 1.555201, # +99 1.5553 - 1.55521, # +9 1.5553 - 1.555301, # -1 1.5553 - 1.55530101, # -101 1.5553 - 1.5553101, # -101 15553 - 15552.9999, # +1 15553.001 - 15552.999, # +2 15553.001 - 15552.9999, # +11 15553.1 - 15552.99, # +11 # etc. (you get the idea :) );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Calculating base difference of numbers
by tachyon-II (Chaplain) on May 31, 2008 at 00:44 UTC | |
|
Re^4: Calculating base difference of numbers
by FunkyMonk (Bishop) on May 30, 2008 at 23:08 UTC | |
|
Re^4: Calculating base difference of numbers
by Anonymous Monk on May 30, 2008 at 20:40 UTC |