author | Francesco Abbate <francesco.bbt@gmail.com> | 2013年01月14日 17:13:36 +0100 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2013年01月14日 17:13:36 +0100 |
commit | 44b1bffb2220da2220aee2588143e11026e10083 (patch) | |
tree | 560d892981bcf26e275c8f855ba30ec7e545f640 /matrix.lua | |
parent | 5f89faaf5172d8fc9075d0094433121fa6f7304d (diff) | |
download | gsl-shell-44b1bffb2220da2220aee2588143e11026e10083.tar.gz |
-rw-r--r-- | matrix.lua | 23 |
diff --git a/matrix.lua b/matrix.lua index 19390dfc..b911812b 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 |