author | Francesco Abbate <francesco.bbt@gmail.com> | 2013年02月04日 21:46:49 +0100 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2013年02月04日 21:46:49 +0100 |
commit | 858c66cac8902ad7fd32d10ae82bb885f9774904 (patch) | |
tree | d9d5e118f3cc21336da56263470a6ffd3462f47d /matrix.lua | |
parent | fe5cea0c67ac178d6476beb64f2bd5fcf4acb0f0 (diff) | |
parent | 9b63ca608ee573a2588ad14a3a46a5ca9f00f9a2 (diff) | |
download | gsl-shell-858c66cac8902ad7fd32d10ae82bb885f9774904.tar.gz |
-rw-r--r-- | matrix.lua | 23 |
diff --git a/matrix.lua b/matrix.lua index 87df4581..d7c5e75b 100644 --- a/matrix.lua +++ b/matrix.lua @@ -3,7 +3,7 @@ local ffi = require 'ffi' local gsl = require 'gsl' -local sqrt, abs = math.sqrt, math.abs +local sqrt, abs, floor = math.sqrt, math.abs, math.floor local format = string.format local check = require 'check' @@ -208,21 +208,34 @@ local function itostr(im, eps, fmt, signed) end end +local function is_small_integer(x) + local ax = abs(x) + return (ax < 2^31 and floor(ax) == ax) +end + local function recttostr(x, y, eps) local x_sub, y_sub = abs(x) < eps, abs(y) < eps - local fmt = '%.8g' + local fmt_x, fmt_y = '%.8g', '%.8g' + if is_small_integer(x) then + fmt_x = '%.0f' + x_sub = false + end + if is_small_integer(y) then + fmt_y = '%.0f' + y_sub = false + end if not x_sub then local sign = x+eps < 0 and '-' or '' local ax = abs(x) if y_sub then - return format('%s'..fmt, sign, ax) + return format('%s'..fmt_x, sign, ax) else - return format('%s'..fmt..'%s', sign, ax, itostr(y, eps, fmt, true)) + return format('%s'..fmt_x..'%s', sign, ax, itostr(y, eps, fmt_y, true)) end else - return (y_sub and '0' or itostr(y, eps, fmt, false)) + return (y_sub and '0' or itostr(y, eps, fmt_y, false)) end end |