Improve matrix pretty printing with small integer numbers - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
path: root/matrix.lua
diff options
context:
space:
mode:
authorFrancesco Abbate <francesco.bbt@gmail.com>2013年01月14日 17:13:36 +0100
committerFrancesco Abbate <francesco.bbt@gmail.com>2013年01月14日 17:13:36 +0100
commit44b1bffb2220da2220aee2588143e11026e10083 (patch)
tree560d892981bcf26e275c8f855ba30ec7e545f640 /matrix.lua
parent5f89faaf5172d8fc9075d0094433121fa6f7304d (diff)
downloadgsl-shell-44b1bffb2220da2220aee2588143e11026e10083.tar.gz
Improve matrix pretty printing with small integer numbers
Diffstat (limited to 'matrix.lua')
-rw-r--r--matrix.lua 23
1 files changed, 18 insertions, 5 deletions
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
generated by cgit v1.2.3 (git 2.39.1) at 2025年09月19日 05:40:11 +0000

AltStyle によって変換されたページ (->オリジナル) /