diff options
Diffstat (limited to 'Master/texmf-dist/scripts/lua-physical/physical-number.lua')
-rw-r--r-- | Master/texmf-dist/scripts/lua-physical/physical-number.lua | 52 |
1 files changed, 41 insertions, 11 deletions
diff --git a/Master/texmf-dist/scripts/lua-physical/physical-number.lua b/Master/texmf-dist/scripts/lua-physical/physical-number.lua index 8c278aacfee..bd6a01e4513 100644 --- a/Master/texmf-dist/scripts/lua-physical/physical-number.lua +++ b/Master/texmf-dist/scripts/lua-physical/physical-number.lua @@ -2,7 +2,7 @@ This file contains the number class which allows to do calculations with uncertainties. -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 @@ -39,12 +39,12 @@ setmetatable(Number, { -- allows to set a default uncertainty, i.e. (5.6) will become (5.60 +/- 0.05) Number.defaultUncertainty = 0.5 --- Switch for writing uncertainty or not -Number.omitUncertainty = false +-- Switch for writing the uncertainty or not +Number.omitUncertainty = true -- If true, the plus-minus notation will be used, otherwise the uncertainty -- will be appended to the value in parentheses. -Number.seperateUncertainty = false +Number.seperateUncertainty = true -- number format "decimal" or "scientific" Number.DECIMAL = 0 @@ -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) @@ -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 @@ -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 @@ -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) |