From 3eb837674ce81de7eaa98085d7bcd7a583d6631b Mon Sep 17 00:00:00 2001 From: c4710n Date: Sat, 24 Aug 2024 22:20:24 +0800 Subject: [PATCH] fix: fix case of 0 --- lib/cozy_size.ex | 12 ++++++------ lib/cozy_size/helper.ex | 4 ++++ lib/cozy_size/iec.ex | 24 ++++++++++++++++++++++++ lib/cozy_size/jedec.ex | 24 ++++++++++++++++++++++++ lib/cozy_size/si.ex | 24 ++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 6 deletions(-) diff --git a/lib/cozy_size.ex b/lib/cozy_size.ex index 2caca9a..10d61d3 100644 --- a/lib/cozy_size.ex +++ b/lib/cozy_size.ex @@ -4,9 +4,9 @@ defmodule CozySize do Currently, there're three existing standards for prefixing units of sizes: - * SI - * IEC - * JEDEC + * SI (decimal-based) + * IEC (binary-based) + * JEDEC (binary-based) > Read more about them at . @@ -23,12 +23,12 @@ defmodule CozySize do iex> # get a humanized tuple by following IEC standard iex> CozySize.IEC.from_bytes(bytes) {1, :GiB} + iex> # get a humanized tuple by following SI standard + iex> CozySize.SI.from_bytes(bytes) + {1.07, :GB} iex> # get a humanized tuple by following JEDEC standard iex> CozySize.JEDEC.from_bytes(bytes) {1, :GB} - iex> # get a humanized tuple by following JEDEC standard - iex> CozySize.SI.from_bytes(bytes) - {1.07, :GB} If you want to operate on bits, please check the `*_bits` functions in each module, which will not be further elaborated here. diff --git a/lib/cozy_size/helper.ex b/lib/cozy_size/helper.ex index 89ca395..872deba 100644 --- a/lib/cozy_size/helper.ex +++ b/lib/cozy_size/helper.ex @@ -8,6 +8,10 @@ defmodule CozySize.Helper do end @doc false + def exponent(0, _base), do: 0 + def exponent(+0.0, _base), do: 0 + def exponent(-0.0, _base), do: 0 + def exponent(n, base) do (:math.log(abs(n)) / :math.log(base)) |> Float.floor() diff --git a/lib/cozy_size/iec.ex b/lib/cozy_size/iec.ex index 26f4e0e..decbc8f 100644 --- a/lib/cozy_size/iec.ex +++ b/lib/cozy_size/iec.ex @@ -54,6 +54,9 @@ defmodule CozySize.IEC do ## Examples + iex> CozySize.IEC.to_bits({0, :b}) + 0 + iex> CozySize.IEC.to_bits({1, :b}) 1 @@ -63,6 +66,9 @@ defmodule CozySize.IEC do iex> CozySize.IEC.to_bits({1.1, :Kib}) 1126.4 + iex> CozySize.IEC.to_bits({0, :B}) + 0 + iex> CozySize.IEC.to_bits({1, :B}) 8 @@ -88,6 +94,9 @@ defmodule CozySize.IEC do ## Examples + iex> CozySize.IEC.to_bytes({0, :b}) + 0 + iex> CozySize.IEC.to_bytes({1, :b}) 0.125 @@ -97,6 +106,9 @@ defmodule CozySize.IEC do iex> CozySize.IEC.to_bytes({1.1, :Kib}) 140.8 + iex> CozySize.IEC.to_bytes({0, :B}) + 0 + iex> CozySize.IEC.to_bytes({1, :B}) 1 @@ -122,6 +134,9 @@ defmodule CozySize.IEC do ## Examples + iex> CozySize.IEC.from_bits(0, as: :bits) + {0, :b} + iex> CozySize.IEC.from_bits(8, as: :bits) {8, :b} @@ -137,6 +152,9 @@ defmodule CozySize.IEC do iex> CozySize.IEC.from_bits(1024 * 10 ** 11, as: :bits, precision: 4) {93.1323, :Tib} + iex> CozySize.IEC.from_bits(0, as: :bytes) + {0, :B} + iex> CozySize.IEC.from_bits(8, as: :bytes) {1, :B} @@ -180,6 +198,9 @@ defmodule CozySize.IEC do ## Examples + iex> CozySize.IEC.from_bytes(0, as: :bits) + {0, :b} + iex> CozySize.IEC.from_bytes(8, as: :bits) {64, :b} @@ -195,6 +216,9 @@ defmodule CozySize.IEC do iex> CozySize.IEC.from_bytes(1024 * 10 ** 11, as: :bits, precision: 4) {745.0581, :Tib} + iex> CozySize.IEC.from_bytes(0, as: :bytes) + {0, :B} + iex> CozySize.IEC.from_bytes(8, as: :bytes) {8, :B} diff --git a/lib/cozy_size/jedec.ex b/lib/cozy_size/jedec.ex index 2724b35..20af0c8 100644 --- a/lib/cozy_size/jedec.ex +++ b/lib/cozy_size/jedec.ex @@ -54,6 +54,9 @@ defmodule CozySize.JEDEC do ## Examples + iex> CozySize.JEDEC.to_bits({0, :b}) + 0 + iex> CozySize.JEDEC.to_bits({1, :b}) 1 @@ -63,6 +66,9 @@ defmodule CozySize.JEDEC do iex> CozySize.JEDEC.to_bits({1.1, :Kb}) 1126.4 + iex> CozySize.JEDEC.to_bits({0, :B}) + 0 + iex> CozySize.JEDEC.to_bits({1, :B}) 8 @@ -88,6 +94,9 @@ defmodule CozySize.JEDEC do ## Examples + iex> CozySize.JEDEC.to_bytes({0, :b}) + 0 + iex> CozySize.JEDEC.to_bytes({1, :b}) 0.125 @@ -97,6 +106,9 @@ defmodule CozySize.JEDEC do iex> CozySize.JEDEC.to_bytes({1.1, :Kb}) 140.8 + iex> CozySize.JEDEC.to_bytes({0, :B}) + 0 + iex> CozySize.JEDEC.to_bytes({1, :B}) 1 @@ -122,6 +134,9 @@ defmodule CozySize.JEDEC do ## Examples + iex> CozySize.JEDEC.from_bits(0, as: :bits) + {0, :b} + iex> CozySize.JEDEC.from_bits(8, as: :bits) {8, :b} @@ -137,6 +152,9 @@ defmodule CozySize.JEDEC do iex> CozySize.JEDEC.from_bits(1024 * 10 ** 11, as: :bits, precision: 4) {93.1323, :Tb} + iex> CozySize.JEDEC.from_bits(0, as: :bytes) + {0, :B} + iex> CozySize.JEDEC.from_bits(8, as: :bytes) {1, :B} @@ -180,6 +198,9 @@ defmodule CozySize.JEDEC do ## Examples + iex> CozySize.JEDEC.from_bytes(0, as: :bits) + {0, :b} + iex> CozySize.JEDEC.from_bytes(8, as: :bits) {64, :b} @@ -195,6 +216,9 @@ defmodule CozySize.JEDEC do iex> CozySize.JEDEC.from_bytes(1024 * 10 ** 11, as: :bits, precision: 4) {745.0581, :Tb} + iex> CozySize.JEDEC.from_bytes(0, as: :bytes) + {0, :B} + iex> CozySize.JEDEC.from_bytes(8, as: :bytes) {8, :B} diff --git a/lib/cozy_size/si.ex b/lib/cozy_size/si.ex index b705b23..66d1ab3 100644 --- a/lib/cozy_size/si.ex +++ b/lib/cozy_size/si.ex @@ -54,6 +54,9 @@ defmodule CozySize.SI do ## Examples + iex> CozySize.SI.to_bits({0, :b}) + 0 + iex> CozySize.SI.to_bits({1, :b}) 1 @@ -63,6 +66,9 @@ defmodule CozySize.SI do iex> CozySize.SI.to_bits({1.1, :kb}) 1100 + iex> CozySize.SI.to_bits({0, :B}) + 0 + iex> CozySize.SI.to_bits({1, :B}) 8 @@ -88,6 +94,9 @@ defmodule CozySize.SI do ## Examples + iex> CozySize.SI.to_bytes({0, :b}) + 0 + iex> CozySize.SI.to_bytes({1, :b}) 0.125 @@ -97,6 +106,9 @@ defmodule CozySize.SI do iex> CozySize.SI.to_bytes({1.1, :kb}) 137.5 + iex> CozySize.SI.to_bytes({0, :B}) + 0 + iex> CozySize.SI.to_bytes({1, :B}) 1 @@ -122,6 +134,9 @@ defmodule CozySize.SI do ## Examples + iex> CozySize.SI.from_bits(0, as: :bits) + {0, :b} + iex> CozySize.SI.from_bits(8, as: :bits) {8, :b} @@ -137,6 +152,9 @@ defmodule CozySize.SI do iex> CozySize.SI.from_bits(1024 * 10 ** 11, as: :bits, precision: 4) {102.4, :Tb} + iex> CozySize.SI.from_bits(0, as: :bytes) + {0, :B} + iex> CozySize.SI.from_bits(8, as: :bytes) {1, :B} @@ -180,6 +198,9 @@ defmodule CozySize.SI do ## Examples + iex> CozySize.SI.from_bytes(0, as: :bits) + {0, :b} + iex> CozySize.SI.from_bytes(8, as: :bits) {64, :b} @@ -195,6 +216,9 @@ defmodule CozySize.SI do iex> CozySize.SI.from_bytes(1024 * 10 ** 11, as: :bits, precision: 4) {819.2, :Tb} + iex> CozySize.SI.from_bytes(0, as: :bytes) + {0, :B} + iex> CozySize.SI.from_bytes(8, as: :bytes) {8, :B}