Class: Numeric

Inherits:
Object show all
Defined in:
lib/origen/core_ext/numeric.rb

Overview

The base class of all numbers, i.e. integers and floats

Instance Method Summary collapse

Instance Method Details

#as_units(units) ⇒ Object


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/origen/core_ext/numeric.rb', line 56

def as_units(units)
  if self >= 1_000_000_000_000_000
    "#{self / 1_000_000_000_000_000.0}P#{units}"
  elsif self >= 1_000_000_000_000
    "#{self / 1_000_000_000_000.0}T#{units}"
  elsif self >= 1_000_000_000
    "#{self / 1_000_000_000.0}G#{units}"
  elsif self >= 1_000_000
    "#{self / 1_000_000.0}M#{units}"
  elsif self >= 1_000
    "#{self / 1_000.0}k#{units}"
  elsif self >= 1
    "#{self}#{units}"
  elsif self >= 1e-3
    "#{self * 1_000}m#{units}"
  elsif self >= 1e-6
    "#{self * 1_000_000}u#{units}"
  elsif self >= 1e-9
    "#{self * 1_000_000_000}n#{units}"
  elsif self >= 1e-12
    "#{self * 1_000_000_000_000}p#{units}"
  elsif self >= 1e-15
    "#{self * 1_000_000_000_000_000}a#{units}"
  else
    "%.3e#{units}" % self
  end
end

#hi_z?Boolean

Returns:

  • (Boolean)

10
11
12
# File 'lib/origen/core_ext/numeric.rb', line 10

def hi_z?;
  false;
end

#reverse_bits(width) ⇒ Object

Reverses the bit representation of a number and returns the new value. Useful when changing register data based on bit order


48
49
50
51
52
53
54
# File 'lib/origen/core_ext/numeric.rb', line 48

def reverse_bits(width)
  result = 0
  0.upto(width - 1) do |i|
    result += self[i] * 2**(width - 1 - i)
  end
  result
end

#to_binObject


34
35
36
# File 'lib/origen/core_ext/numeric.rb', line 34

def to_bin
  "0b#{to_s(2)}"
end

#to_bitstring(width) ⇒ Object

Converts a number to a String representing binary number Requires width of bit string for padding. If the width is less than the number of bits required to represent the number the width argument is meaningless.


42
43
44
# File 'lib/origen/core_ext/numeric.rb', line 42

def to_bitstring(width)
  '%0*b' % [width, self]
end

#to_hexObject


30
31
32
# File 'lib/origen/core_ext/numeric.rb', line 30

def to_hex
  "0x#{to_s(16).upcase}"
end

#undefined?Boolean

Returns:

  • (Boolean)

18
19
20
# File 'lib/origen/core_ext/numeric.rb', line 18

def undefined?;
  false;
end

#x?Boolean

Returns:

  • (Boolean)

14
15
16
# File 'lib/origen/core_ext/numeric.rb', line 14

def x?;
  false;
end

#x_or_z?Boolean

Returns:

  • (Boolean)

22
23
24
# File 'lib/origen/core_ext/numeric.rb', line 22

def x_or_z?;
  false;
end

#z?Boolean

Helpers for cases where a method may return a 0, 1 or an instance of Origen::Value::X or Origen::Value::Z

Returns:

  • (Boolean)

6
7
8
# File 'lib/origen/core_ext/numeric.rb', line 6

def z?;
  false;
end

#z_or_x?Boolean

Returns:

  • (Boolean)

26
27
28
# File 'lib/origen/core_ext/numeric.rb', line 26

def z_or_x?;
  false;
end