Class: Rlid::Percentage

Inherits:
Object
  • Object
show all
Defined in:
lib/rlid/probabilities/language_probabilities.rb

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Percentage

Returns a new instance of Percentage.



8
9
10
# File 'lib/rlid/probabilities/language_probabilities.rb', line 8

def initialize value
  @value = value
end

Instance Method Details

#to_fObject



32
33
34
# File 'lib/rlid/probabilities/language_probabilities.rb', line 32

def to_f
  @value
end

#to_sObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rlid/probabilities/language_probabilities.rb', line 12

def to_s
  if @value <= 0.98
    "%.2g" % (@value * 100)
  elsif @value == 1.0
    "100"
  else
    complement = 1.0 - @value
    # complement =
    log = -Math.log10(complement).ceil
    digits = log - 1
    res = "%.#{digits}f" % (@value * 100)
    last = -1
    while res[last] == ?9
      digits += 1
      res = "%.#{digits}f" % (@value * 100)
    end
    res
  end
end