Module: UnicodePlot::Utils

Defined in:
src/lib/unicode_plot/utils.rb

Constant Summary collapse

INT64_MIN =
-9223372036854775808
INT64_MAX =
9223372036854775807

Class Method Summary collapse

Class Method Details

.ceil_neg_log10(x) ⇒ Object



59
60
61
62
63
64
65
# File 'src/lib/unicode_plot/utils.rb', line 59

def ceil_neg_log10(x)
  if roundable?(-Math.log10(x))
    (-Math.log10(x)).ceil
  else
    (-Math.log10(x)).floor
  end
end

.extend_limits(values, limits) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'src/lib/unicode_plot/utils.rb', line 5

def extend_limits(values, limits)
  mi, ma = limits.minmax.map(&:to_f)
  if mi == 0 && ma == 0
    mi, ma = values.minmax.map(&:to_f)
  end
  diff = ma - mi
  if diff == 0
    ma = mi + 1
    mi = mi - 1
  end
  if limits == [0, 0]
    plotting_range_narrow(mi, ma)
  else
    [mi, ma]
  end
end

.float_round_log10(x, m) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'src/lib/unicode_plot/utils.rb', line 29

def float_round_log10(x, m)
  if x == 0
    0.0
  elsif x > 0
    x.round(ceil_neg_log10(m) + 1).to_f
  else
    -(-x).round(ceil_neg_log10(m) + 1).to_f
  end
end

.plotting_range_narrow(xmin, xmax) ⇒ Object



22
23
24
25
26
27
# File 'src/lib/unicode_plot/utils.rb', line 22

def plotting_range_narrow(xmin, xmax)
  diff = xmax - xmin
  xmax = round_up_subtick(xmax, diff)
  xmin = round_down_subtick(xmin, diff)
  [xmin.to_f, xmax.to_f]
end

.round_down_subtick(x, m) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'src/lib/unicode_plot/utils.rb', line 49

def round_down_subtick(x, m)
  if x == 0
    0.0
  elsif x > 0
    x.floor(ceil_neg_log10(m) + 1)
  else
    -(-x).ceil(ceil_neg_log10(m) + 1)
  end
end

.round_up_subtick(x, m) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'src/lib/unicode_plot/utils.rb', line 39

def round_up_subtick(x, m)
  if x == 0
    0.0
  elsif x > 0
    x.ceil(ceil_neg_log10(m) + 1)
  else
    -(-x).floor(ceil_neg_log10(m) + 1)
  end
end

.roundable?(x) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'src/lib/unicode_plot/utils.rb', line 70

def roundable?(x)
  x.to_i == x && INT64_MIN <= x && x < INT64_MAX
end