From 8c9d31da32715d8583f738516d170c2d4785ecf0 Mon Sep 17 00:00:00 2001 From: Harshal LADHE Date: Thu, 12 Oct 2023 16:29:29 +0530 Subject: [PATCH 1/3] Added unit group for `information entropy` --- lib/unit_measurements/unit_groups/all.rb | 7 +- .../unit_groups/information_entropy.rb | 15 ++ .../unit_groups/information_entropy_spec.rb | 151 ++++++++++++++++++ 3 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 lib/unit_measurements/unit_groups/information_entropy.rb create mode 100644 spec/unit_measurements/unit_groups/information_entropy_spec.rb diff --git a/lib/unit_measurements/unit_groups/all.rb b/lib/unit_measurements/unit_groups/all.rb index 279cd4d3..c3156103 100644 --- a/lib/unit_measurements/unit_groups/all.rb +++ b/lib/unit_measurements/unit_groups/all.rb @@ -2,8 +2,6 @@ # -*- frozen_string_literal: true -*- # -*- warn_indent: true -*- -## Fundamental SI units - require_relative "amount_of_substance" require_relative "electric_current" require_relative "length" @@ -12,8 +10,6 @@ require_relative "time" require_relative "weight" -## Derived units - require_relative "area" require_relative "volume" require_relative "density" @@ -50,8 +46,7 @@ require_relative "radiation_exposure" require_relative "radiation_absorbed_dose" require_relative "radiation_equivalent_dose" - -## Other units +require_relative "information_entropy" require_relative "sound_level" require_relative "plane_angle" 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 00000000..687634f4 --- /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/spec/unit_measurements/unit_groups/information_entropy_spec.rb b/spec/unit_measurements/unit_groups/information_entropy_spec.rb new file mode 100644 index 00000000..21ff2320 --- /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 From 36650d4f70af3f91ea975e7def92454008b158a9 Mon Sep 17 00:00:00 2001 From: Harshal LADHE Date: Thu, 12 Oct 2023 16:32:34 +0530 Subject: [PATCH 2/3] Updated changelog and unit reference file --- CHANGELOG.md | 10 +++++++++- units.md | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb7703b6..4c703371 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 unit 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/units.md b/units.md index 848707c5..3cae0833 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* | byte, bytes | +| 3 | Sh | shannon, shannons | +| _4_ | _nat_ | _nit, nepit, natural unit of information_ | +| 5 | nybl | nibble, nibbles, nybble, nyble | +| 6 | Hart | hartley, ban, dit | From 02d63e745618d5bd917649c28a0a5da940f5b330 Mon Sep 17 00:00:00 2001 From: Harshal LADHE Date: Thu, 12 Oct 2023 16:33:04 +0530 Subject: [PATCH 3/3] Bump version to `4.9.0` --- Gemfile.lock | 2 +- lib/unit_measurements/version.rb | 2 +- spec/unit_measurements_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index ca6630c5..0c4587e0 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/version.rb b/lib/unit_measurements/version.rb index cb2370cc..3bf3a775 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_spec.rb b/spec/unit_measurements_spec.rb index 20048227..de712e11 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