Class: DrLight::ScientificNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/dr_light/scientific_number.rb,
lib/dr_light/scientific_number/formatter.rb,
lib/dr_light/scientific_number/normalizer.rb,
lib/dr_light/scientific_number/deviance_distance.rb

Overview

Number to be exibed in scientific number

Author:

  • darthjee

Defined Under Namespace

Classes: DevianceDistance, Formatter, Normalizer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, deviance = nil) ⇒ ScientificNumber

Returns a new instance of ScientificNumber.

Parameters:

  • value (Nuber)

    number to be exibed

  • deviance (Number) (defaults to: nil)

    deviance of number



32
33
34
35
# File 'lib/dr_light/scientific_number.rb', line 32

def initialize(value, deviance = nil)
  @value    = value.to_f
  @deviance = deviance.to_f
end

Instance Attribute Details

#devianceNumeric (readonly)

The deviance from the average (imprecision)

Returns:

  • (Numeric)


23
24
25
# File 'lib/dr_light/scientific_number.rb', line 23

def deviance
  @deviance
end

#valueNumeric (readonly)

The number value (average)

Returns:

  • (Numeric)


16
17
18
# File 'lib/dr_light/scientific_number.rb', line 16

def value
  @value
end

Instance Method Details

#deviance_distance(other) ⇒ Float

Calculates the distance to another number in deviances

the deviance will be a composition of both numbers deviances

Examples:

With Number

number = DrLight::ScientificNumber.new(100, 3)

number.deviance_distance(115)   # returns 5

With scientifica number

number = DrLight::ScientificNumber.new(100, 3)
other  = DrLight::ScientificNumber.new(115, 4)

number.deviance_distance(other)   # returns 3

Returns:

  • (Float)

    always positive number

See Also:



72
73
74
# File 'lib/dr_light/scientific_number.rb', line 72

def deviance_distance(other)
  DevianceDistance.new(self, other).to_f
end

#to_sString

string representation of number

Examples:

number = DrLight::ScientificNumber.new(0.42, 0.01)
number.to_s # returns '4.20(10)e-1'

Returns:

  • (String)


44
45
46
47
48
49
50
51
# File 'lib/dr_light/scientific_number.rb', line 44

def to_s
  format(
    formatter.format_string,
    value: normalizer.value,
    exponential: normalizer.exponential,
    deviance: normalizer.deviance
  )
end