Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement bit and byte for information and data #419

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/pkgdefaults.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@dimension 𝚯 "𝚯" Temperature # This one is \bfTheta
@dimension 𝐉 "𝐉" Luminosity
@dimension 𝐍 "𝐍" Amount
@dimension 𝐛 "𝐛" Information
Copy link
Contributor

@mcabbott mcabbott Apr 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a dimension? I thought that nat is, like rad, a pure number -- it's just some p*log(p) where p is a probability. And bits, like degrees etc, are multiples of that.

Suggested change
@dimension 𝐛 "𝐛" Information

const RelativeScaleTemperature = Quantity{T, 𝚯, <:AffineUnits} where T
const AbsoluteScaleTemperature = Quantity{T, 𝚯, <:ScalarUnits} where T

Expand Down Expand Up @@ -60,6 +61,7 @@ const AbsoluteScaleTemperature = Quantity{T, 𝚯, <:ScalarUnits} where T
@refunit cd "cd" Candela 𝐉 true
@refunit g "g" Gram 𝐌 true
@refunit mol "mol" Mole 𝐍 true
@refunit b "b" Bit 𝐛 false

# Angles and solid angles
@unit sr "sr" Steradian 1 true
Expand Down Expand Up @@ -113,7 +115,7 @@ end
# The hectare is used more frequently than any other power-of-ten of an are.
@unit a "a" Are 100m^2 false
const ha = Unitful.FreeUnits{(Unitful.Unit{:Are, 𝐋^2}(2, 1//1),), 𝐋^2}()
@unit b "b" Barn 100fm^2 true
@unit bn "bn" Barn 100fm^2 true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking


# Volume
# `l` is also an acceptable symbol for liters
Expand Down Expand Up @@ -216,11 +218,29 @@ const Å = Å = angstrom
# Pressure
@unit psi "psi" PoundsPerSquareInch 1lbf/inch^2 false

# Data
@unit B "B" Byte 8b false
Comment on lines +221 to +222
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the definitions were like this, would it still convert MB to GB as integers, without introducing the log(2)?

Suggested change
# Data
@unit B "B" Byte 8b false
# Information
@unit nat "nat" Nat 1 false
@unit bit "bit" Bit log(2)*u"nat" false
@unit B "B" Byte 8bit false

# deciBytes can be a problem with deciBels, and we don't actually care about orders < 1
# Instead define all units in common use
# Overflow warning:
# Gigascale and above likely exceed Int32
# Exascale likely exceeds Int64
# Omitting ZB and YB: Julia interprets literal as Float64, which causes rounding errors
@unit kB "kB" KiloByte 8_000b false
@unit MB "MB" MegaByte 8_000_000b false
@unit GB "GB" GigaByte 8_000_000_000b false
Comment on lines +229 to +231
Copy link
Contributor

@mcabbott mcabbott Apr 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the base 2 ones also be included?

Suggested change
@unit kB "kB" KiloByte 8_000b false
@unit MB "MB" MegaByte 8_000_000b false
@unit GB "GB" GigaByte 8_000_000_000b false
@unit kB "kB" KiloByte 8_000b false
@unit kiB "kiB" KibiByte 8_192b false
@unit MB "MB" MegaByte 8_000_000b false
@unit MiB "MiB" MebiByte 8_388_608b false
@unit GB "GiB" GigaByte 8_000_000_000b false
@unit GiB "GiB" GibiByte 8_589_934_592b false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"GiB" appears twice in the suggested change, looks like a typo.

@unit TB "TB" TeraByte 8_000_000_000_000b false
@unit PB "PB" PetaByte 8_000_000_000_000_000b false
@unit EB "EB" ExaByte 8_000_000_000_000_000_000b false
# Data rate
@unit bps "bps" BitPerSecond 1b/s true
@unit Bps "Bps" BytePerSecond 8b/s true

#########
# Logarithmic scales and units

@logscale dB "dB" Decibel 10 10 false
@logscale B "B" Bel 10 1 false
@logscale bel "Bel" Bel 10 1 false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this one as well

@logscale Np "Np" Neper ℯ 1//2 true
@logscale cNp "cNp" Centineper ℯ 50 true

Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Unitful:
ms, s, minute, hr, d, yr, Hz,
J, A, N, mol, V,
mW, W,
B, kB, MB, GB, TB, PB, EB,
dB, dB_rp, dB_p, dBm, dBV, dBSPL, Decibel,
Np, Np_rp, Np_p, Neper

Expand Down Expand Up @@ -300,6 +301,16 @@ end
end
end

@testset "Data prefixes" begin
# Ensure manually setting the SI prefixes was consistent
@test 1kB/B == 1_000
@test 1MB/kB == 1_000
@test 1GB/MB == 1_000
@test 1TB/GB == 1_000
@test 1PB/TB == 1_000
@test 1EB/PB == 1_000
end

@testset "Promotion" begin
@testset "> Unit preferences" begin
# Only because we favor SI, we have the following:
Expand Down