Skip to content

Commit 08c7414

Browse files
authored
best_prefix produces invalid results (#347)
1 parent d25259d commit 08c7414

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/ruby_units/unit.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,12 +1499,22 @@ def coerce(other)
14991499
[self.class.new(other), self]
15001500
end
15011501

1502-
# returns a new unit that has been scaled to be more in line with typical usage.
1502+
# Returns a new unit that has been scaled to be more in line with typical usage. This is highly opinionated and not
1503+
# based on any standard. It is intended to be used to make the units more human readable.
1504+
#
1505+
# Some key points:
1506+
# * Units containing 'kg' will be returned as is. The prefix in 'kg' makes this an odd case.
1507+
# * It will use `centi` instead of `milli` when the scalar is between 0.01 and 0.001
1508+
#
1509+
# @return [Unit]
15031510
def best_prefix
15041511
return to_base if scalar.zero?
1512+
return self if units.include?('kg')
15051513

15061514
best_prefix = if kind == :information
15071515
self.class.prefix_values.key(2**((::Math.log(base_scalar, 2) / 10.0).floor * 10))
1516+
elsif ((1/100r)..(1/10r)).cover?(base_scalar)
1517+
self.class.prefix_values.key(1/100r)
15081518
else
15091519
self.class.prefix_values.key(10**((::Math.log10(base_scalar) / 3.0).floor * 3))
15101520
end

spec/ruby_units/unit_spec.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,10 +2262,17 @@
22622262
specify { expect(RubyUnits::Unit.new('23 m').div(RubyUnits::Unit.new('2 m'))).to eq(11) }
22632263
end
22642264

2265-
context '#best_prefix' do
2266-
specify { expect(RubyUnits::Unit.new('1024 KiB').best_prefix).to eq(RubyUnits::Unit.new('1 MiB')) }
2267-
specify { expect(RubyUnits::Unit.new('1000 m').best_prefix).to eq(RubyUnits::Unit.new('1 km')) }
2268-
specify { expect { RubyUnits::Unit.new('0 m').best_prefix }.to_not raise_error }
2265+
describe '#best_prefix' do
2266+
it { expect(RubyUnits::Unit.new('1024 KiB').best_prefix).to have_attributes(scalar: 1, units: 'MiB') }
2267+
it { expect(RubyUnits::Unit.new('1000 m').best_prefix).to have_attributes(scalar: 1, units: 'km') }
2268+
it { expect(RubyUnits::Unit.new('1/1000 m').best_prefix).to have_attributes(scalar: 1.0, units: 'mm') }
2269+
it { expect(RubyUnits::Unit.new('1/100000 m').best_prefix).to have_attributes(scalar: 10, units: 'um') }
2270+
it { expect(RubyUnits::Unit.new('0 m').best_prefix).to have_attributes(scalar: 0, units: 'm') }
2271+
it { expect(RubyUnits::Unit.new('1000 kg').best_prefix).to have_attributes(scalar: 1000, units: 'kg') }
2272+
it { expect(RubyUnits::Unit.new('1_000_000 kg').best_prefix).to have_attributes(scalar: 1_000_000, units: 'kg') }
2273+
it { expect(RubyUnits::Unit.new('1/100 m').best_prefix).to have_attributes(scalar: 1, units: 'cm') }
2274+
it { expect(RubyUnits::Unit.new('1/10 m').best_prefix).to have_attributes(scalar: 10, units: 'cm') }
2275+
it { expect(RubyUnits::Unit.new('0.234 s').best_prefix).to have_attributes(scalar: 234, units: 'ms') }
22692276
end
22702277

22712278
context 'Time helper functions' do

0 commit comments

Comments
 (0)