dr_light 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 288117834749641c8f72f5992cbaa22268342014b919bae7b8305af38c139dcb
4
- data.tar.gz: 923ee123b123b4222c0f4ccc35e7f370256519ab0cd6df6ae1a5589c031eab31
3
+ metadata.gz: 03cdec01261b9d5b682f4b85d71009abd4216912aaece529ada333ff4ab910e7
4
+ data.tar.gz: a2e6c4d8013f389e5872de461e637bc6209365a2561b6cace450fd7d4b1ca367
5
5
  SHA512:
6
- metadata.gz: e8222dea48e0d17380267b9e5ce7a9d316580212a88213f04fd1ef625998f8d25996d9c6ec88e2143a507d628bbbb26068f9517833f49ffc158de41de9203d87
7
- data.tar.gz: 51fec29e95479c2a3bfbe61787dcc438726b2563d56ea80d43a9a8d45d701f391995902a92abe53724985dfee0395455248ab37ac633db40e99c8067687cb5c5
6
+ metadata.gz: 92a816d6c71135d96fbb5f8435aaf27316f3b44fb946c44592129ffeb8892eeac3a16c715293b978cec43490bc8e9a03587fc6ee476da1a3d0e41771fa13f914
7
+ data.tar.gz: b4871424ed66e10973c2ef2e24c5197c024c5c2afbbf8d3c88ff1a2c57a3e011e466adb0334ee221f94e7618491d8f261a7918afbebea4876547c5b710167d22
data/README.md CHANGED
@@ -4,11 +4,12 @@ DrLight
4
4
  [![Test Coverage](https://codeclimate.com/github/darthjee/dr_light/badges/coverage.svg)](https://codeclimate.com/github/darthjee/dr_light/coverage)
5
5
  [![Issue Count](https://codeclimate.com/github/darthjee/dr_light/badges/issue_count.svg)](https://codeclimate.com/github/darthjee/dr_light)
6
6
  [![Gem Version](https://badge.fury.io/rb/dr_light.svg)](https://badge.fury.io/rb/dr_light)
7
+ [![Inline docs](http://inch-ci.org/github/darthjee/dr_light.svg?branch=master)](http://inch-ci.org/github/darthjee/dr_light)
7
8
 
8
9
 
9
10
  ![dr_light](https://raw.githubusercontent.com/darthjee/dr_light/master/dr_light.jpg)
10
11
 
11
12
  Yard Documentation
12
13
  -------------------
13
- https://www.rubydoc.info/gems/dr_light/0.0.1
14
+ https://www.rubydoc.info/gems/dr_light/0.0.2
14
15
 
@@ -1 +1,3 @@
1
- ignore: []
1
+ ignore:
2
+ - lib/dr_light.rb
3
+ - lib/dr_light/version.rb
@@ -15,13 +15,21 @@ rules:
15
15
  exclude: []
16
16
  ExampleTag:
17
17
  enabled: true
18
- exclude: []
18
+ exclude:
19
+ - DrLight::ScientificNumber#value
20
+ - DrLight::ScientificNumber#deviance
21
+ - DrLight::ScientificNumber#initialize
22
+ - DrLight::ScientificNumber::Formatter#initialize
23
+ - DrLight::ScientificNumber::Normalizer#initialize
19
24
  ReturnTag:
20
25
  enabled: true
21
26
  exclude: []
22
27
  Summary::Presence:
23
28
  enabled: true
24
- exclude: []
29
+ exclude:
30
+ - DrLight::ScientificNumber#initialize
31
+ - DrLight::ScientificNumber::Formatter#initialize
32
+ - DrLight::ScientificNumber::Normalizer#initialize
25
33
  Summary::Length:
26
34
  enabled: true
27
35
  exclude: []
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # @author darthjee
4
- # @ai public
4
+ # @api public
5
5
  #
6
6
  # Pack of science usefull classes
7
7
  module DrLight
8
8
  autoload :ScientificNumber, 'dr_light/scientific_number'
9
+ autoload :Utils, 'dr_light/utils'
9
10
  end
@@ -6,8 +6,23 @@ module DrLight
6
6
  #
7
7
  # Number to be exibed in scientific number
8
8
  class ScientificNumber
9
- autoload :Formatter, 'dr_light/scientific_number/formatter'
9
+ autoload :Formatter, 'dr_light/scientific_number/formatter'
10
+ autoload :Normalizer, 'dr_light/scientific_number/normalizer'
11
+
10
12
  attr_reader :value, :deviance
13
+ # @!method value
14
+ # @api public
15
+ #
16
+ # The number value (average)
17
+ #
18
+ # @return [Numeric]
19
+
20
+ # @!method deviance
21
+ # @api public
22
+ #
23
+ # The deviance from the average (imprecision)
24
+ #
25
+ # @return [Numeric]
11
26
 
12
27
  # @param value [Nuber] number to be exibed
13
28
  # @param deviance [Number] deviance of number
@@ -17,19 +32,41 @@ module DrLight
17
32
  end
18
33
 
19
34
  # string representation of number
35
+ #
36
+ # @example
37
+ # number = DrLight::ScientificNumber.new(0.42, 0.01)
38
+ # number.to_s # returns '4.20(10)e-1'
39
+ #
40
+ # @return [String]
20
41
  def to_s
21
42
  format(
22
43
  formatter.format_string,
23
- value: formatter.value,
24
- exponential: formatter.exponential,
25
- deviance: formatter.deviance
44
+ value: normalizer.value,
45
+ exponential: normalizer.exponential,
46
+ deviance: normalizer.deviance
26
47
  )
27
48
  end
28
49
 
29
50
  private
30
51
 
52
+ # @private
53
+ # @api private
54
+ #
55
+ # String formatter
56
+ #
57
+ # @return [Formatter]
31
58
  def formatter
32
- @formatter ||= Formatter.new(
59
+ @formatter ||= Formatter.new(normalizer)
60
+ end
61
+
62
+ # @private
63
+ # @api private
64
+ #
65
+ # Values normalizer
66
+ #
67
+ # @return [Normalizer]
68
+ def normalizer
69
+ @normalizer ||= Normalizer.new(
33
70
  value: value,
34
71
  deviance: deviance
35
72
  )
@@ -3,70 +3,58 @@
3
3
  module DrLight
4
4
  class ScientificNumber
5
5
  # @author darthjee
6
- # @api public
6
+ # @api private
7
7
  #
8
8
  # Class responsible for formatting the values of
9
9
  # {ScientificNumber}
10
10
  class Formatter
11
- attr_reader :exponential, :significant,
12
- :value, :deviance
13
-
14
- # @param value [Nuber] number to be exibed
15
- # @param deviance [Number] deviance of number
16
- def initialize(value:, deviance:)
17
- @value = value
18
- @deviance = deviance
19
- @significant = 1
20
- @exponential = 0
21
- format_value
11
+ # @param value [Numeric] number to be exibed
12
+ # @param deviance [Numeric] deviance of number
13
+ def initialize(normalizer)
14
+ @normalizer = normalizer
22
15
  end
23
16
 
24
- # Returns the string of the format expected for
25
- # {ScientificNumber#to_s}
17
+ # Returns the string of the format expected
18
+ #
19
+ # @see ScientificNumber#to_s
26
20
  #
27
21
  # @return [String]
28
22
  def format_string
29
- ["%<value>.#{significant}f"].tap do |values|
30
- values << '(%<deviance>d)' unless deviance.zero?
31
- values << 'e%<exponential>d' unless exponential.zero?
23
+ ["%<value>.#{normalizer.significant}f"].tap do |values|
24
+ values << '(%<deviance>d)' if deviance?
25
+ values << 'e%<exponential>d' if exponential?
32
26
  end.join
33
27
  end
34
28
 
35
29
  private
36
30
 
37
- def format_value
38
- normalize_value
39
- normalize_deviance
40
- end
41
-
42
- def normalize_value
43
- order = order_from(@value)
44
- @exponential += order
45
- @value /= (10.0**order)
46
- @deviance /= (10.0**order)
47
- end
48
-
49
- def normalize_deviance
50
- return if @deviance.zero?
51
-
52
- if order_difference.negative?
53
- @value *= (10.0**order_difference)
54
- @exponential -= order_difference
55
- else
56
- @significant += order_difference
57
- end
58
-
59
- @deviance *= 10 * (10.0**order_difference)
60
- @deviance += 0.5
61
- end
31
+ attr_reader :normalizer
32
+ # @method normalizer
33
+ # @private
34
+ # @api private
35
+ #
36
+ # Number normalizer
37
+ #
38
+ # @return [Normalizer]
62
39
 
63
- def order_from(number)
64
- format('%<number>e', number: number).gsub(/.*e/, '').to_i
40
+ # @private
41
+ # @api private
42
+ #
43
+ # Checks if deviance should be present on the output
44
+ #
45
+ # @return [TrueClass,FalseClass]
46
+ def deviance?
47
+ !normalizer.deviance.zero?
65
48
  end
66
49
 
67
- def order_difference
68
- @order_difference ||=
69
- order_from(@value) - order_from(@deviance)
50
+ # @private
51
+ # @api private
52
+ #
53
+ # Checks if exponential should be present on the output
54
+ #
55
+ # @return [TrueClass,FalseClass]
56
+ def exponential?
57
+ !normalizer.exponential.zero?
70
58
  end
71
59
  end
72
60
  end
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DrLight
4
+ class ScientificNumber
5
+ # @author darthjee
6
+ # @api private
7
+ #
8
+ # Class responsible for normalizing the scientific number
9
+ class Normalizer
10
+ attr_reader :exponential, :significant, :value, :deviance
11
+ # @method exponential
12
+ # @api private
13
+ #
14
+ # Exponential factor of number
15
+ #
16
+ # Any number can be represented by the exponencial
17
+ # format.
18
+ #
19
+ # ex: +12.4+ can be represented +1.24e1+
20
+ #
21
+ # @return [Numeric]
22
+
23
+ # @method significant
24
+ # @api private
25
+ #
26
+ # Significant decimals to be shown from the value
27
+ #
28
+ # The smaller the {#deviance}, more significant
29
+ # digits need to be shown
30
+ #
31
+ # @return [Numeric]
32
+
33
+ # @method value
34
+ # @api private
35
+ #
36
+ # The value after normalization
37
+ #
38
+ # Value will be returned always being a value
39
+ # between -10 and 10
40
+ #
41
+ # @return [Numeric]
42
+
43
+ # @method deviance
44
+ # @api private
45
+ #
46
+ # Deviance representation
47
+ #
48
+ # Deviance will always be a number
49
+ # between 10 and and 100
50
+ #
51
+ # @return [Integer]
52
+
53
+ # @param value [Numeric] number to be normalized
54
+ # @param deviance [Numeric] deviance of number
55
+ def initialize(value:, deviance:)
56
+ @value = value
57
+ @deviance = deviance
58
+ @significant = 1
59
+ normalize_value
60
+ normalize_deviance
61
+ end
62
+
63
+ private
64
+
65
+ # @private
66
+ #
67
+ # Normalize the value to use scientific notation
68
+ #
69
+ # 612 becomes 6.13
70
+ #
71
+ # 0.613 becomes 6.13
72
+ #
73
+ # The difference is recorded on exponential
74
+ #
75
+ # @return [Numeric] the exponential
76
+ def normalize_value
77
+ @value /= value_order_multiplier
78
+ @deviance /= value_order_multiplier
79
+ @exponential = value_order
80
+ end
81
+
82
+ # @private
83
+ #
84
+ # Normalize the values based on the order difference
85
+ #
86
+ # @return [Numeric]
87
+ def normalize_difference
88
+ if order_difference.negative?
89
+ @value *= order_difference_multiplier
90
+ @exponential -= order_difference
91
+ else
92
+ @significant += order_difference
93
+ end
94
+ end
95
+
96
+ # @private
97
+ #
98
+ # Normalize the deviance to have 12 digits
99
+ #
100
+ # @return [Numeric]
101
+ def normalize_deviance
102
+ return if @deviance.zero?
103
+
104
+ normalize_difference
105
+ @deviance *= 10 * order_difference_multiplier
106
+ @deviance += 0.5
107
+ end
108
+
109
+ # @private
110
+ #
111
+ # Order difference between value and deviance
112
+ #
113
+ # @return [Numeric]
114
+ def order_difference
115
+ @order_difference ||= Utils.order_difference(@value, @deviance)
116
+ end
117
+
118
+ # @private
119
+ #
120
+ # potency of 10^order_difference
121
+ #
122
+ # @return [Numeric]
123
+ def order_difference_multiplier
124
+ @order_difference_multiplier ||= 10.0**order_difference
125
+ end
126
+
127
+ # @private
128
+ #
129
+ # Order of the value
130
+ #
131
+ # @return [Numeric]
132
+ def value_order
133
+ @value_order ||= Utils.order(@value)
134
+ end
135
+
136
+ # @private
137
+ #
138
+ # potency of 10^value_order
139
+ #
140
+ # @return [Numeric]
141
+ def value_order_multiplier
142
+ @value_order_multiplier ||= 10.0**value_order
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DrLight
4
+ # @author darthjee
5
+ # @api public
6
+ #
7
+ # Module with utility functions
8
+ module Utils
9
+ # Return the order of the number
10
+ #
11
+ # Number 63 can be rewriten as 6.3*10^1 where
12
+ # 1 is the order
13
+ #
14
+ # @param number [Numeric] number to be analyzed
15
+ #
16
+ # @return [Integer]
17
+ #
18
+ # @example Negative big number
19
+ # DrLight::Utils.order(-100) # returns 2
20
+ # @example Positive small number
21
+ # DrLight::Utils.order(0.01) # returns -2
22
+ # @example Zero
23
+ # DrLight::Utils.order(0) # returns 0
24
+ def self.order(number)
25
+ format('%<number>e', number: number).gsub(/.*e/, '').to_i
26
+ end
27
+
28
+ # Returns the order difference between 2 numbers
29
+ #
30
+ # @param first_number [Numeric] number to be analiyzed
31
+ # @param second_number [Numeric] number to be analiyzed
32
+ #
33
+ # @return [Integer]
34
+ #
35
+ # @example
36
+ # DrLight::Utils.order_difference(-10, 0.1) # returns 2
37
+ def self.order_difference(first_number, second_number)
38
+ order(first_number) - order(second_number)
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DrLight
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe DrLight::ScientificNumber do
6
+ describe '#to_s' do
7
+ describe 'yard' do
8
+ subject(:number) { described_class.new(value, deviance) }
9
+
10
+ let(:value) { 0.42 }
11
+ let(:deviance) { 0.01 }
12
+
13
+ it do
14
+ expect(number.to_s).to eq('4.20(10)e-1')
15
+ end
16
+ end
17
+ end
18
+ end