Class: Numeric

Inherits:
Object show all
Defined in:
lib/gviz/system_extension.rb

Instance Method Summary collapse

Instance Method Details

#norm(range, tgr = 0.0..1.0) ⇒ Object

Return a normalized value of the receiver between 0.0 to 1.0.

5.norm(0..10).should eql 0.5
7.norm(5..10).should eql 0.4

When the target range specified, the result is adjusted to the range.

5.norm(0..10, 0..20).should eql 10.0


11
12
13
14
# File 'lib/gviz/system_extension.rb', line 11

def norm(range, tgr=0.0..1.0)
  unit = (self - range.min) / (range.max - range.min).to_f
  unit * (tgr.max - tgr.min) + tgr.min
end