Module: Sass::Script::Functions

Defined in:
lib/sassy-math.rb

Overview

Sassy math Functions

Instance Method Summary collapse

Instance Method Details

#acos(rad) ⇒ Object



106
107
108
109
110
# File 'lib/sassy-math.rb', line 106

def acos(rad)
  rad = rad.value.to_f
  result = Math.acos(rad)
  Sass::Script::Number.new(result)
end

#acosh(rad) ⇒ Object



111
112
113
114
115
# File 'lib/sassy-math.rb', line 111

def acosh(rad)
  rad = rad.value.to_f
  result = Math.acosh(rad)
  Sass::Script::Number.new(result)
end

#asin(rad) ⇒ Object



121
122
123
124
125
# File 'lib/sassy-math.rb', line 121

def asin(rad)
  rad = rad.value.to_f
  result = Math.asin(rad)
  Sass::Script::Number.new(result)
end

#asinh(rad) ⇒ Object



126
127
128
129
130
# File 'lib/sassy-math.rb', line 126

def asinh(rad)
  rad = rad.value.to_f
  result = Math.asinh(rad)
  Sass::Script::Number.new(result)
end

#atan(rad) ⇒ Object



136
137
138
139
140
# File 'lib/sassy-math.rb', line 136

def atan(rad)
  rad = rad.value.to_f
  result = Math.atan(rad)
  Sass::Script::Number.new(result)
end

#atan2(y, x) ⇒ Object



141
142
143
144
145
146
# File 'lib/sassy-math.rb', line 141

def atan2(y, x)
  y = y.value.to_f
  x = x.value.to_f
  result = Math.atan2(y, x)
  Sass::Script::Number.new(result)
end

#atanh(rad) ⇒ Object



147
148
149
150
151
# File 'lib/sassy-math.rb', line 147

def atanh(rad)
  rad = rad.value.to_f
  result = Math.atanh(rad)
  Sass::Script::Number.new(result)
end

#cosh(rad) ⇒ Object



101
102
103
104
105
# File 'lib/sassy-math.rb', line 101

def cosh(rad)
  rad = rad.value.to_f
  result = Math.cosh(rad)
  Sass::Script::Number.new(result)
end

#deg_to_rad(degree) ⇒ Object

Trigonometric Functions



93
94
95
96
# File 'lib/sassy-math.rb', line 93

def deg_to_rad(degree)
  result = degree.value.to_f * Math::PI / 180
  Sass::Script::Number.new(result)
end

#eObject



65
66
67
68
# File 'lib/sassy-math.rb', line 65

def e
  e = Math::E
  Sass::Script::Number.new(e)
end

#exponent(base, powerNum, powerDen) ⇒ Object

Exponents



7
8
9
10
11
12
13
# File 'lib/sassy-math.rb', line 7

def exponent(base, powerNum, powerDen)
  base = base.value.to_f
  powerNum = powerNum.value.to_f
  powerDen = powerDen.value.to_f
  result = base ** (powerNum / powerDen)
  Sass::Script::Number.new(result)
end

#factorial(number) ⇒ Object

Miscellaneous



41
42
43
44
45
46
47
48
49
50
# File 'lib/sassy-math.rb', line 41

def factorial(number)
  result = 1
  number = number.value
  if number > 0
    (1..number).each do |i|
      result = result * i
    end
  end
  Sass::Script::Number.new(result)
end

#golden_ratioObject



69
70
71
72
# File 'lib/sassy-math.rb', line 69

def golden_ratio()
  result = (1.0 / 2.0) + (Math.sqrt(5) / 2.0)
  Sass::Script::Number.new(result)
end

#hypot(a, b) ⇒ Object



54
55
56
57
58
59
# File 'lib/sassy-math.rb', line 54

def hypot(a, b)
  a = a.value.to_f
  b = b.value.to_f
  result = Math.hypot(a, b)
  Sass::Script::Number.new(result)
end

#is_float(number) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/sassy-math.rb', line 83

def is_float(number)
  number = number.value
  if number - number.floor != 0
    result = true
  else
    result = false
  end
  Sass::Script::Bool.new(result)
end

#is_int(number) ⇒ Object

Comparative Functions



74
75
76
77
78
79
80
81
82
# File 'lib/sassy-math.rb', line 74

def is_int(number)
  number = number.value.to_f
  if number - number.floor != 0
    result = false
  else
    result = true
  end
  Sass::Script::Bool.new(result)
end

#ln(num) ⇒ Object

Logarithms



32
33
34
35
# File 'lib/sassy-math.rb', line 32

def ln(num)
  result = Math.log(num.value)
  Sass::Script::Number.new(result)
end

#log10(num) ⇒ Object



36
37
38
39
# File 'lib/sassy-math.rb', line 36

def log10(num)
  result = Math.log10(num.value)
  Sass::Script::Number.new(result)
end

#nth_root(number, root) ⇒ Object



25
26
27
28
29
30
# File 'lib/sassy-math.rb', line 25

def nth_root(number, root)
  number = number.value.to_f
  root = root.value.to_f
  result = number ** (1.0 / root)
  Sass::Script::Number.new(result)
end

#piObject

Constants



61
62
63
64
# File 'lib/sassy-math.rb', line 61

def pi
  pi = Math::PI
  Sass::Script::Number.new(pi)
end

#power(base, exponent) ⇒ Object



14
15
16
17
18
19
# File 'lib/sassy-math.rb', line 14

def power(base, exponent)
  base = base.value.to_f
  exponent = exponent.value.to_f
  result = base ** exponent
  Sass::Script::Number.new(result)
end

#rad_to_deg(rad) ⇒ Object



97
98
99
100
# File 'lib/sassy-math.rb', line 97

def rad_to_deg(rad)
  result = rad.value.to_f * 180 / Math::PI
  Sass::Script::Number.new(result)
end

#random(max = Sass::Script::Number.new(100)) ⇒ Object

shamelessly taken from here: gist.github.com/1561650



51
52
53
# File 'lib/sassy-math.rb', line 51

def random(max = Sass::Script::Number.new(100)) ## shamelessly taken from here: https://gist.github.com/1561650
  Sass::Script::Number.new(rand(max.value), max.numerator_units, max.denominator_units)
end

#sinh(rad) ⇒ Object



116
117
118
119
120
# File 'lib/sassy-math.rb', line 116

def sinh(rad)
  rad = rad.value.to_f
  result = Math.sinh(rad)
  Sass::Script::Number.new(result)
end

#sqrt(number) ⇒ Object



20
21
22
23
24
# File 'lib/sassy-math.rb', line 20

def sqrt(number)
  number = number.value.to_f
  result = Math.sqrt(number)
  Sass::Script::Number.new(result)
end

#tanh(rad) ⇒ Object



131
132
133
134
135
# File 'lib/sassy-math.rb', line 131

def tanh(rad)
  rad = rad.value.to_f
  result = Math.tanh(rad)
  Sass::Script::Number.new(result)
end