Skip to content

Commit

Permalink
Replaced unit \number with \unitless. New method Number:tosiuntix() m…
Browse files Browse the repository at this point in the history
…ethod added.
  • Loading branch information
tjenni committed May 8, 2021
1 parent 79732c1 commit 1753b81
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 38 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Thomas Jenni
Copyright (c) 2021 Thomas Jenni

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

Author: Thomas Jenni

Version: 1.0.4
Version: 1.0.5

Date: 2020-09-15
Date: 2021-05-08

License: MIT

Expand Down
Binary file added lua-physical.pdf
Binary file not shown.
77 changes: 59 additions & 18 deletions lua-physical.tex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

% lua code and lua-physical command definitions
\begin{luacode}
physical = require("physical")
physical = require('src.physicalx')
N = physical.Number
Q = physical.Quantity
\end{luacode}
Expand Down Expand Up @@ -70,6 +70,8 @@
round-mode=off
}

\DeclareSIUnit\unitless{}

\DeclareSIUnit\fahrenheit{\ensuremath{{}^{\circ}}F}

\DeclareSIUnit\inch{in}
Expand Down Expand Up @@ -154,7 +156,7 @@

\lstset{language=[LaTex]Tex}

\title{The \textsc{lua-physical} library \\\ \\\normalsize Version 1.0.4}
\title{The \textsc{lua-physical} library \\\ \\\normalsize Version 1.0.5}
\author{Thomas Jenni}
\date{\today}
\maketitle
Expand Down Expand Up @@ -209,7 +211,7 @@ \section{Loading}
}

% declare the unitless unit (siunitx package)
\DeclareSIUnit\number{}
\DeclareSIUnit\unitless{}

% print a quantity using the \SI{}{} macro.
\newcommand{\q}[1]{%
Expand Down Expand Up @@ -635,19 +637,13 @@ \subsubsection{Print the Uncertainty explicitly}
A = ( a * b ):to(_m^2)
\end{luacode}

\leftbar
Calculate the area of a rectangle with lengths $\q{a}$ and $\q{b}$.
%
\begin{equation*}
A = a \cdot b
= \q{a} \cdot \q{b}
= \underline{\q{A}}
\end{equation*}
\endleftbar







Expand Down Expand Up @@ -716,10 +712,6 @@ \subsection{Mathematical operations}







%------------------------------------------------------------
\newpage
\section{Supported Units}
Expand Down Expand Up @@ -836,6 +828,12 @@ \subsection{Prefixes}









%------------------------------------------------------------
\subsection{Base Units}
\label{ch:base units}
Expand All @@ -855,7 +853,7 @@ \subsection{Base Units}
\thead{Quantity} & \thead{Unit} & \thead{Symbol} & \thead{Dim.} & \thead{Definition} \\\hline

number \protect\footnotemark &
-- &
unitless &
|_1| &
$\mathrm{1}$ &
The dimensionless number one. \\
Expand All @@ -878,7 +876,7 @@ \subsection{Base Units}
\end{table}

\footnotetext[1]{
The number one is a unit with dimension zero. Stricly speaking it is not a base unit.
The number one is a unit with dimension zero. It is not a base unit stricty speaking.
}


Expand Down Expand Up @@ -1177,9 +1175,9 @@ \subsection{Nominal Astronomical Units}


The mass of the sun is proposed to be used as the astronomical mass unit.
\unittable{
\printunit[Mass]{_M_S}{N(1.98847e30,0.00007e30) * _kg}
}
%\unittable{
% \printunit[Mass]{_M_S}{N(1.98847e30,0.00007e30) * _kg}
%}


%------------------------------------------------------------
Expand Down Expand Up @@ -2737,6 +2735,38 @@ \subsection{physical.Number}
\end{quote}


\method{N}{tosiunitx()}
\begin{quote}
Converts the number to an siunitx string.

\subtitle{Parameters / Return}
\begin{description}
\item returns : |string|
\end{description}

\begin{lstlisting}
N.omitUncertainty = false
N.seperateUncertainty = true

n = N(5.25,0.0045)
print( n:tosiunitx() )
`
\begin{luacode}
N.omitUncertainty = false
N.seperateUncertainty = true

n = N(1.25,0.0023)
tex.write(tostring(n:tosiunitx()))

N.omitUncertainty = true
N.seperateUncertainty = false
\end{luacode}
`
\end{lstlisting}

\end{quote}



\method{N}{mean(n)}
\begin{quote}
Expand Down Expand Up @@ -3337,6 +3367,12 @@ \section{Change History}
\vspace{1em}
}

\vhist{V1.0.5}{2021/05/08}{Minor release}{
The unitmacro |\textbackslash number| had some collision with other packages. The unitless unit has been renamed from |\textbackslash number| to |\textbackslash unitless|. The declaration in the preamble |\textbackslash DeclareSIUnit\textbackslash number\{\}| has therefore to be replaced with |\textbackslash DeclareSIUnit\textbackslash unitless\{\}|. \\
\ \\
The parsing of uncertainty values in the package |siunitx| has changed. It was incompatible with the output of |Number.\_\_tostring()|. To mitigate this issue a new method |Number.tosiunitx()| has been added.
}

\vhist{V1.0.4}{2020/09/15}{Minor release}{
Changed default value of |Number.omitUncertainty| and |Number.seperateUncertainty| to |true|. Documentation added.
}
Expand Down Expand Up @@ -3382,4 +3418,9 @@ \section{Change History}
\printindex[lua]







\end{document}
Binary file modified lua-physical_example.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion lua-physical_example.tex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}

% empty unit
\DeclareSIUnit\number{}
\DeclareSIUnit\unitless{}

\DeclareSIUnit\inch{in}

Expand Down
2 changes: 1 addition & 1 deletion src/physical-definition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Q.definePrefix("Ki", "kibi", 1024)
local IEC_prefixes = {"Ki","Mi","Gi","Ti","Pi","Ei","Zi","Yi"}

-- define the dimensionless base quantity for real numbers
Q.define("1", "number", Q(1))
Q.define("1", "unitless", Q(1))

-- define SI base units
Q.defineBase("m", "meter", L)
Expand Down
44 changes: 37 additions & 7 deletions src/physical-number.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,20 @@ end


-- plus minus notation, i.e. (5.040 +/- 0.001)
function Number:toPlusMinusNotation(format)
function Number:toPlusMinusNotation(format, parenthesis, pmsign)

if format == nil then
format = Number.format
end

if parenthesis == nil then
parenthesis = true
end

if pmsign == nil then
pmsign = " +/- "
end

local m, e = self._frexp(self._x)
local dm, de = self._frexp(self._dx)

Expand All @@ -216,13 +224,15 @@ function Number:toPlusMinusNotation(format)
-- In the decimal format, the numbers are given as decimals, i.e. (0.02 +/- 0.001)
if format == Number.DECIMAL then
if de - udigit >= 0 then
str = self._flt2str(self._x, 0).." +/- "..self._flt2str(self._dx, 0)
str = self._flt2str(self._x, 0)..pmsign..self._flt2str(self._dx, 0)
else
local digits = math.abs(-de + udigit)
str = self._flt2str(self._x, digits).." +/- "..self._flt2str(self._dx, digits)
str = self._flt2str(self._x, digits)..pmsign..self._flt2str(self._dx, digits)
end

str = "("..str..")"
if parenthesis then
str = "("..str..")"
end

-- In the scientific format, the numbers are written with powers of ten, i.e. (2.0 +/- 0.1) e-2
elseif format == Number.SCIENTIFIC then
Expand All @@ -232,13 +242,15 @@ function Number:toPlusMinusNotation(format)
de = de - e

if de >= 0 then
str = self._flt2str(m, 0).." +/- "..self._flt2str(dm, 0)
str = self._flt2str(m, 0)..pmsign..self._flt2str(dm, 0)
else
local digits = math.abs(-de + udigit)
str = self._flt2str(m, digits).." +/- "..self._flt2str(dm, digits)
str = self._flt2str(m, digits)..pmsign..self._flt2str(dm, digits)
end

str = "("..str..")"
if parenthesis then
str = "("..str..")"
end

if e ~= 0 then
str = str.."e"..e
Expand Down Expand Up @@ -341,6 +353,24 @@ function Number:__tostring()
end


-- convert number to a string
function Number:tosiunitx()

if self._dx == 0 then
return tostring(self._x)

elseif Number.omitUncertainty then
return self:toOmitUncertaintyNotation()

elseif Number.seperateUncertainty then
return self:toPlusMinusNotation(Number.format, false, "+-")

else
return self:toParenthesisNotation()
end

end

-- equal
-- Two physical numbers are equal if they have the same value and uncertainty
function Number.__eq(n1,n2)
Expand Down
12 changes: 10 additions & 2 deletions src/physical-quantity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,18 @@ function Quantity:tosiunitx(param,mode)
end

if mode == Quantity.SIUNITX_SI then
return "\\SI"..param.."{"..tostring(self.value).."}".."{"..self.unit:tosiunitx().."}"
if type(self.value) == "number" then
return "\\SI"..param.."{"..tostring(self.value).."}".."{"..self.unit:tosiunitx().."}"
else
return "\\SI"..param.."{"..self.value:tosiunitx().."}".."{"..self.unit:tosiunitx().."}"
end

elseif mode == Quantity.SIUNITX_num then
return "\\num"..param.."{"..tostring(self.value).."}"
if type(self.value) == "number" then
return "\\num"..param.."{"..tostring(self.value).."}"
else
return "\\num"..param.."{"..self.value:tosiunitx().."}"
end

elseif mode == Quantity.SIUNITX_si then
return "\\si"..param.."{"..self.unit:tosiunitx().."}"
Expand Down
14 changes: 8 additions & 6 deletions src/physical.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ SOFTWARE.

-- Source: http://kiki.to/blog/2014/04/12/rule-5-beware-of-multiple-files/

local Dimension = require('physical-dimension')
local Unit = require('physical-unit')
local Quantity = require('physical-quantity')
local Number = require('physical-number')
local prefix = ''

require('physical-definition')
local Dimension = require(prefix..'physical-dimension')
local Unit = require(prefix..'physical-unit')
local Quantity = require(prefix..'physical-quantity')
local Number = require(prefix..'physical-number')

local Data = require('physical-data')
require(prefix..'physical-definition')

local Data = require(prefix..'physical-data')

local m = {
Dimension = Dimension,
Expand Down

0 comments on commit 1753b81

Please sign in to comment.