Skip to content

Commit

Permalink
fix: fix case of 0
Browse files Browse the repository at this point in the history
  • Loading branch information
c4710n committed Aug 24, 2024
1 parent e0bc87a commit 3eb8376
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/cozy_size.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://en.wikipedia.org/wiki/Binary_prefix>.
Expand All @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions lib/cozy_size/helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions lib/cozy_size/iec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand Down
24 changes: 24 additions & 0 deletions lib/cozy_size/jedec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand Down
24 changes: 24 additions & 0 deletions lib/cozy_size/si.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand Down

0 comments on commit 3eb8376

Please sign in to comment.