Class: TTFunk::SciForm

Inherits:
Object
  • Object
show all
Defined in:
lib/ttfunk/sci_form.rb

Overview

Scientific number representation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(significand, exponent = 0) ⇒ SciForm

Returns a new instance of SciForm.

Parameters:

  • significand (Float, Integer)
  • exponent (Float, Integer) (defaults to: 0)


16
17
18
19
# File 'lib/ttfunk/sci_form.rb', line 16

def initialize(significand, exponent = 0)
  @significand = significand
  @exponent = exponent
end

Instance Attribute Details

#exponentFloat, Integer (readonly)

Exponent

Returns:

  • (Float, Integer)


12
13
14
# File 'lib/ttfunk/sci_form.rb', line 12

def exponent
  @exponent
end

#significandFloat, Integer (readonly)

Significand

Returns:

  • (Float, Integer)


8
9
10
# File 'lib/ttfunk/sci_form.rb', line 8

def significand
  @significand
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Check equality to another number.

Parameters:

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ttfunk/sci_form.rb', line 32

def ==(other)
  case other
  when Float
    other == to_f # rubocop: disable Lint/FloatComparison
  when self.class
    other.significand == significand &&
      other.exponent == exponent
  else
    false
  end
end

#to_fFloat

Convert to Float.

Returns:

  • (Float)


24
25
26
# File 'lib/ttfunk/sci_form.rb', line 24

def to_f
  significand * (10**exponent)
end