File: Numeric_extras.rb

package info (click to toggle)
libtioga-ruby 1.11-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 10,460 kB
  • ctags: 3,986
  • sloc: ansic: 38,451; ruby: 16,774; sh: 172; makefile: 111
file content (134 lines) | stat: -rw-r--r-- 1,897 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#  Numeric_extras.rb

# add some Numeric methods

class Numeric

    # Math functions

    def acos
        Math.acos(self)
    end
    
    def acosh
        Math.acosh(self)
    end
    
    def asin
        Math.asin(self)
    end
    
    def asinh
        Math.asinh(self)
    end
    
    def atan
        Math.atan(self)
    end
    
    def atanh
        Math.atanh(self)
    end
    
    def atan2(n)
        Math.atan2(self,n)
    end
    
    def cos
        Math.cos(self)
    end
    
    def cosh
        Math.cosh(self)
    end
    
    def exp
        Math.exp(self)
    end
    
    def log
        Math.log(self)
    end
    
    def log10
        Math.log10(self)
    end
    
    def sin
        Math.sin(self)
    end
    
    def sinh
        Math.sinh(self)
    end
    
    def sqrt
        Math.sqrt(self)
    end
    
    def tan
        Math.tan(self)
    end
    
    def tanh
        Math.tanh(self)
    end
    
    def neg
        -self
    end
    
    def exp10
        10**self
    end
    
    def inv
        1/self
    end
    
    def trim(cutoff=1e-6)
        (self.abs < cutoff)? 0.0 : self
    end
    
    def pow(y)
        self**y
    end
    
    def raised_to(y)
        self**y
    end
    
    def as_exponent_of(y)
        y**self
    end
    
    def safe_log(cutoff=1e-99)
        ((self > cutoff)? self : cutoff).log
    end
    
    def safe_log10(cutoff=1e-99)
        ((self > cutoff)? self : cutoff).log10
    end
    
    def safe_inv(cutoff=1e-99)
        (self.abs > cutoff)? 1/self : (self > 0)? 1/cutoff : -1/cutoff
    end
    
    def safe_sqrt
        (self > 0.0)? self.sqrt : 0.0
    end
    
    def safe_asin
        ((self > 1.0)? 1.0 : (self < -1.0)? -1.0 : self).asin
    end
    
    def safe_acos
        ((self > 1.0)? 1.0 : (self < -1.0)? -1.0 : self).acos
    end

    def mod(y)
        self.modulo(y)
    end

    
end