#!/usr/local/bin/perl
# significant figure functions in perl
$x = 123.66678 ;
$stdev = 0.01 ;
$r = &RNDPD($x,3) ;
$s = &SIGFG($x,$stdev) ;
$o = &ORD($x) ;
$a1 = &ABS($x) ;
$a2 = &ABS(-$x) ;
print "rounded= $r \n" ;
print "order= $o \n" ;
print "absolute= $a1  $a2 \n" ;
print "sigfigs= $s \n" ;
$stdev = 1.0 ;
$s = &SIGFG($x,$stdev) ;
print "sigfigs= $s \n" ;

sub ORD {
local($x) = $_[0] ;
local($order) = 0 ;
if ($x != 0) {
$order = int(log(&ABS($x+1e-35))/2.30259)
   }
} ; # end sub ORD

sub ABS {
local($x) = $_[0] ;
$x = ( $x >= 0 ) ? $x : -$x
} ; # end sub ABS

sub RNDPD {
# round to $ipd places past the decimal point
local($x,$ipd) = @_ ;
$x = int($x*(10**$ipd)+0.5)/(10**$ipd)
} ; # end sub ROUND

sub SIGFG {
# round to number of significant figures in stdev+1
local($x,$sg) = @_ ;
local($ord) = 0 ;
if ($x != 0 ) {
$ord = &ORD($sg/10) ;
$ord = ( $ord <= 0 ) ? $ord-1 : $ord ;
$x = int($x/(10**$ord)+0.5)*(10**$ord)
   }
  else { $x = 0 }
} ; # end sub SIGFG

