lib/pdground/pdground.rb in pdground-0.1.3 vs lib/pdground/pdground.rb in pdground-0.1.4
- old
+ new
@@ -6,10 +6,11 @@
def self.round(args)
unless args[:meas] && args[:unc]
raise ArgumentError 'To round a value you need a value and an
uncertainty (:val and :unc parameters)'
end
+ args.each { |k, v| args[k] = v.to_s if v.is_a? Numeric }
dig = digits(args[:unc]).to_i
return round_to(args, 2) if dig < 355
return round_to(args, 1) if dig < 950
round_to_up(args)
end
@@ -28,11 +29,11 @@
end
def self.round_to(args, digits)
# Round to a given number of significant digits
unc = args[:unc].to_f.sigfig(digits)
- unc = prepare(unc, digits) if unc.size < digits
+ unc = prepare(unc, digits)
# puts "Rounding: #{args[:meas]} +- #{args[:unc]} ---> #{val} +- #{unc}"
[meas(args[:meas], unc), unc]
end
def self.prepare(unc, digits)
@@ -47,12 +48,12 @@
unc
end
def self.meas(meas, unc)
if (dec = unc.split('.')[1])
- format("%.#{dec.size}f", meas.to_f)
+ meas.to_f.round(dec.size).to_s
else
- meas.to_f.round(unc.gsub(/0+$/, '').size - unc.size)
+ meas.to_f.round(unc.gsub(/0+$/, '').size - unc.size).to_s
end
end
private_class_method :meas, :round_to, :digits, :round_to_up, :prepare
end