Class: Erlang::Float
- Inherits:
-
Object
- Object
- Erlang::Float
- Defined in:
- lib/erlang/float.rb
Overview
An Float
is a literal constant float-precision number.
Creating Floats
Erlang::Float["1.0e10"]
# => 1.00000000000000000000e+10
Erlang::Float[0]
# => 0.00000000000000000000e+00
Erlang::Float[-0.0, old: true]
# => Erlang::Float["-0.00000000000000000000e+00", old: true]
Erlang::Float[-1e308]
# => -1.00000000000000000000e+308
Erlang::Float[1e-308]
# => 1.00000000000000000000e-308
Instance Attribute Summary collapse
-
#data ⇒ ::BigDecimal
readonly
Return the data for this
Float
. -
#old ⇒ ::Boolean
readonly
Return the old flag for this
Float
.
Class Method Summary collapse
-
.[](data, old: false) ⇒ Float
Create a new
Float
populated with the givendata
. -
.compare(a, b) ⇒ -1, ...
Compares
a
andb
and returns whether they are less than, equal to, or greater than each other.
Instance Method Summary collapse
-
#coerce(numeric) ⇒ ::Array
If a
numeric
is the same type asself
, returns an array containingnumeric
andself
. -
#eql?(other) ⇒ Boolean
(also: #==)
Return true if
other
has the same type and contents as thisFloat
. -
#erlang_inspect(raw = false) ⇒ ::String
Return the contents of this
Float
as a Erlang-readable::String
. -
#inspect ⇒ ::String
The nicely formatted version of the
Float
. -
#to_f ⇒ ::Float
The float version of the
Float
. -
#to_float_string ⇒ ::String
The float string format of the
Float
. -
#to_s ⇒ ::String
(also: #to_str)
The string version of the
Float
.
Instance Attribute Details
#data ⇒ ::BigDecimal (readonly)
Return the data for this Float
25 26 27 |
# File 'lib/erlang/float.rb', line 25 def data @data end |
#old ⇒ ::Boolean (readonly)
Return the old flag for this Float
29 30 31 |
# File 'lib/erlang/float.rb', line 29 def old @old end |
Class Method Details
.[](data, old: false) ⇒ Float
Create a new Float
populated with the given data
.
37 38 39 40 41 42 43 44 |
# File 'lib/erlang/float.rb', line 37 def [](data, old: false) if data.is_a?(::String) data = ::BigDecimal.new(data) elsif not data.is_a?(::BigDecimal) data = ::BigDecimal.new(data.to_s) end return new(data, old) end |
.compare(a, b) ⇒ -1, ...
Compares a
and b
and returns whether they are less than,
equal to, or greater than each other.
53 54 55 56 57 |
# File 'lib/erlang/float.rb', line 53 def compare(a, b) raise ArgumentError, "'a' must be of Erlang::Float type" unless a.kind_of?(Erlang::Float) raise ArgumentError, "'b' must be of Erlang::Float type" unless b.kind_of?(Erlang::Float) return a.data <=> b.data end |
Instance Method Details
#coerce(numeric) ⇒ ::Array
If a numeric
is the same type as self
, returns an array containing numeric
and self
.
Otherwise, returns an array with both a numeric and num represented as Float
objects.
81 82 83 84 85 86 87 |
# File 'lib/erlang/float.rb', line 81 def coerce(numeric) if numeric.is_a?(Erlang::Float) return [numeric, self] else return [numeric.to_f, to_f] end end |
#eql?(other) ⇒ Boolean Also known as: ==
Return true if other
has the same type and contents as this Float
.
93 94 95 96 97 98 99 100 |
# File 'lib/erlang/float.rb', line 93 def eql?(other) return true if other.equal?(self) if instance_of?(other.class) return !!(self.data == other.data) else return !!(Erlang.compare(other, self) == 0) end end |
#erlang_inspect(raw = false) ⇒ ::String
Return the contents of this Float
as a Erlang-readable ::String
.
110 111 112 |
# File 'lib/erlang/float.rb', line 110 def erlang_inspect(raw = false) return to_float_string end |
#inspect ⇒ ::String
Returns the nicely formatted version of the Float
.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/erlang/float.rb', line 115 def inspect if @old return "Erlang::Float[#{to_float_string.inspect}, old: true]" else float_string = to_float_string float_object = ::Kernel::Float(float_string) if Erlang::PositiveInfinity == float_object or Erlang::NegativeInfinity == float_object return "Erlang::Float[#{float_string.inspect}]" else return float_string end end end |
#to_f ⇒ ::Float
Returns the float version of the Float
.
173 174 175 |
# File 'lib/erlang/float.rb', line 173 def to_f return @data.to_f end |
#to_float_string ⇒ ::String
Returns the float string format of the Float
.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/erlang/float.rb', line 130 def to_float_string string = @data.to_s sign = (string.getbyte(0) == 45) ? '-' : '' offset = (sign.bytesize == 1) ? 1 : 0 dotpos = string.index(?.) epos = string.index(?e) if epos.nil? string << "e00" epos = string.index(?e) end if @data.zero? return Erlang::Terms.binary_encoding([ sign, '0.00000000000000000000e+00' ].join) end integer = string.byteslice(offset, dotpos - offset) fractional = string.byteslice(dotpos + 1, epos - dotpos - 1) e = string.byteslice(epos + 1, string.bytesize - epos - 1).to_i while fractional.bytesize > 0 and integer == ?0 and e > -323 b = fractional.getbyte(0) fractional = fractional.byteslice(1, fractional.bytesize - 1) e -= 1 if b != 48 integer.setbyte(0, b) end end if fractional.bytesize > 20 fractional = fractional.byteslice(0, 20) elsif fractional.bytesize < 20 fractional = fractional.ljust(20, ?0) end return Erlang::Terms.binary_encoding([ sign, integer, '.', fractional, (e < 0) ? 'e-' : 'e+', e.abs.to_s.rjust(2, ?0) ].join) end |
#to_s ⇒ ::String Also known as: to_str
Returns the string version of the Float
.
178 179 180 |
# File 'lib/erlang/float.rb', line 178 def to_s return to_float_string end |