diff --git a/CHANGELOG.md b/CHANGELOG.md index bb7703b..7e1719f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,12 @@ -## [4.8.0](https://github.com/shivam091/unit_measurements/compare/v4.7.0...v4.8.0) - 2023-10-11 +## [4.9.0](https://github.com/shivam091/unit_measurements/compare/v4.8.0...v4.9.0) - 2023-10-13 + +### What's new + +- Added units group for `information entropy` units. + +---------- + +## [4.8.0](https://github.com/shivam091/unit_measurements/compare/v4.7.0...v4.8.0) - 2023-10-12 ### What's new diff --git a/Gemfile.lock b/Gemfile.lock index ca6630c..0c4587e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - unit_measurements (4.8.0) + unit_measurements (4.9.0) activesupport (~> 7.0) GEM diff --git a/lib/unit_measurements/unit_groups/all.rb b/lib/unit_measurements/unit_groups/all.rb index 279cd4d..b78e924 100644 --- a/lib/unit_measurements/unit_groups/all.rb +++ b/lib/unit_measurements/unit_groups/all.rb @@ -50,6 +50,7 @@ require_relative "radiation_exposure" require_relative "radiation_absorbed_dose" require_relative "radiation_equivalent_dose" +require_relative "information_entropy" ## Other units diff --git a/lib/unit_measurements/unit_groups/information_entropy.rb b/lib/unit_measurements/unit_groups/information_entropy.rb new file mode 100644 index 0000000..687634f --- /dev/null +++ b/lib/unit_measurements/unit_groups/information_entropy.rb @@ -0,0 +1,15 @@ +# -*- encoding: utf-8 -*- +# -*- frozen_string_literal: true -*- +# -*- warn_indent: true -*- + +UnitMeasurements::InformationEntropy = UnitMeasurements.build do + primitive "nat" + + si_unit "b", value: "1 Sh", aliases: ["bit", "bits"] + si_unit "B", value: [2.pow(3), "b"], aliases: ["byte", "bytes"] + + unit "Sh", value: [Math.log(2), "nat"], aliases: ["shannon", "shannons"] + unit "nat", aliases: ["nit", "nepit", "natural unit of information"] + unit "nybl", value: [2.pow(2), "b"], aliases: ["nibble", "nibbles", "nybble", "nyble"] + unit "Hart", value: [Math.log(10), "nat"], aliases: ["hartley", "ban", "dit"] +end diff --git a/lib/unit_measurements/version.rb b/lib/unit_measurements/version.rb index cb2370c..3bf3a77 100644 --- a/lib/unit_measurements/version.rb +++ b/lib/unit_measurements/version.rb @@ -3,5 +3,5 @@ # -*- warn_indent: true -*- module UnitMeasurements - VERSION = "4.8.0" + VERSION = "4.9.0" end diff --git a/spec/unit_measurements/unit_groups/information_entropy_spec.rb b/spec/unit_measurements/unit_groups/information_entropy_spec.rb new file mode 100644 index 0000000..21ff232 --- /dev/null +++ b/spec/unit_measurements/unit_groups/information_entropy_spec.rb @@ -0,0 +1,151 @@ +# -*- encoding: utf-8 -*- +# -*- frozen_string_literal: true -*- +# -*- warn_indent: true -*- + +# spec/unit_measurements/unit_groups/information_entropy_spec.rb + +RSpec.describe UnitMeasurements::InformationEntropy do + describe "nat" do + subject { described_class.new(1, "nat") } + + it "converts to Sh" do + expect(subject.convert_to("Sh").quantity).to eq(1.44269504088896) + end + + it "converts to Hart" do + expect(subject.convert_to("Hart").quantity).to eq(0.434294481903252) + end + + it "converts to b" do + expect(subject.convert_to("b").quantity).to eq(1.44269504088896) + end + + it "converts to B" do + expect(subject.convert_to("B").quantity).to eq(0.18033688011112) + end + + it "converts to nybl" do + expect(subject.convert_to("nybl").quantity).to eq(0.360673760222241) + end + end + + describe "Sh" do + subject { described_class.new(1, "Sh") } + + it "converts to nat" do + expect(subject.convert_to("nat").quantity).to eq(0.693147180559945) + end + + it "converts to Hart" do + expect(subject.convert_to("Hart").quantity).to eq(0.301029995663981) + end + + it "converts to b" do + expect(subject.convert_to("b").quantity).to eq(1) + end + + it "converts to B" do + expect(subject.convert_to("B").quantity).to eq(0.125) + end + + it "converts to nybl" do + expect(subject.convert_to("nybl").quantity).to eq(0.25) + end + end + + describe "Hart" do + subject { described_class.new(1, "Hart") } + + it "converts to nat" do + expect(subject.convert_to("nat").quantity).to eq(2.30258509299405) + end + + it "converts to Sh" do + expect(subject.convert_to("Sh").quantity).to eq(3.32192809488736) + end + + it "converts to b" do + expect(subject.convert_to("b").quantity).to eq(3.32192809488736) + end + + it "converts to B" do + expect(subject.convert_to("B").quantity).to eq(0.41524101186092) + end + + it "converts to nybl" do + expect(subject.convert_to("nybl").quantity).to eq(0.830482023721841) + end + end + + describe "b" do + subject { described_class.new(1, "b") } + + it "converts to nat" do + expect(subject.convert_to("nat").quantity).to eq(0.693147180559945) + end + + it "converts to Sh" do + expect(subject.convert_to("Sh").quantity).to eq(1) + end + + it "converts to Hart" do + expect(subject.convert_to("Hart").quantity).to eq(0.301029995663981) + end + + it "converts to B" do + expect(subject.convert_to("B").quantity).to eq(0.125) + end + + it "converts to nybl" do + expect(subject.convert_to("nybl").quantity).to eq(0.25) + end + end + + describe "B" do + subject { described_class.new(1, "B") } + + it "converts to nat" do + expect(subject.convert_to("nat").quantity).to eq(5.54517744447956) + end + + it "converts to Sh" do + expect(subject.convert_to("Sh").quantity).to eq(8) + end + + it "converts to Hart" do + expect(subject.convert_to("Hart").quantity).to eq(2.40823996531185) + end + + it "converts to b" do + expect(subject.convert_to("b").quantity).to eq(8) + end + + it "converts to nybl" do + expect(subject.convert_to("nybl").quantity).to eq(2) + end + end + + describe "nybl" do + subject { described_class.new(1, "nybl") } + + it "converts to nat" do + expect(subject.convert_to("nat").quantity).to eq(2.77258872223978) + end + + it "converts to Sh" do + expect(subject.convert_to("Sh").quantity).to eq(4) + end + + it "converts to Hart" do + expect(subject.convert_to("Hart").quantity).to eq(1.20411998265592) + end + + it "converts to b" do + expect(subject.convert_to("b").quantity).to eq(4) + end + + it "converts to B" do + expect(subject.convert_to("B").quantity).to eq(0.5) + end + end +end diff --git a/spec/unit_measurements_spec.rb b/spec/unit_measurements_spec.rb index 2004822..de712e1 100644 --- a/spec/unit_measurements_spec.rb +++ b/spec/unit_measurements_spec.rb @@ -4,6 +4,6 @@ RSpec.describe UnitMeasurements do it "has a valid version number" do - expect(UnitMeasurements::VERSION).to eq("4.8.0") + expect(UnitMeasurements::VERSION).to eq("4.9.0") end end diff --git a/units.md b/units.md index 848707c..25210f4 100644 --- a/units.md +++ b/units.md @@ -553,3 +553,16 @@ These units are defined in `UnitMeasurements::RadiationEquivalentDose`. |:--|:--|:--| | _1_ | _Sv*_ | _sievert, sieverts_ | | 2 | rem | röntgen equivalent man, roentgen equivalent man | + +## 48. Information entropy + +These units are defined in `UnitMeasurements::InformationEntropy`. + +| # | Name | Aliases | +|:--|:--|:--| +| 1 | b* | bit, bits | +| 2 | B* | bytes, bytes | +| _3_ | _nat_ | _nit, nepit, natural unit of information_ | +| 4 | Sh | shannon, shannons | +| 5 | Hart | hartley, ban, dit | +| 6 | nybl | nibble, nibbles, nybble, nyble |