Rename the log function to logarithm to avoid collisions with the log

function for logging.

Add keyword argument support for the math functions.

Add square-root as as an alias to sqrt.
This commit is contained in:
Chris Eppstein 2012-03-11 20:50:15 -07:00
parent 9cbab2bd27
commit 45571de0f7
3 changed files with 25 additions and 5 deletions

View File

@ -26,7 +26,7 @@ The Documentation for the [latest preview release](http://beta.compass-style.org
### Misc Changes ### Misc Changes
* Fix the mime-type of embedded `woff` font files. Add support for Embedded OpenType fonts. * Fix the mime-type of embedded `woff` font files. Add support for Embedded OpenType fonts.
* New math functions are now available: `e()`, `log($number[, $base = e()])`, `sqrt($number)` * New math functions are now available: `e()`, `logarithm($number[, $base = e()])`, `square-root($number)`
and `pow($number, $exponent)` and `pow($number, $exponent)`

View File

@ -3,38 +3,52 @@ module Compass::SassExtensions::Functions::Math
def pi() def pi()
Sass::Script::Number.new(Math::PI) Sass::Script::Number.new(Math::PI)
end end
Sass::Script::Functions.declare :pi, []
def sin(number) def sin(number)
trig(:sin, number) trig(:sin, number)
end end
Sass::Script::Functions.declare :sin, [:number]
def cos(number) def cos(number)
trig(:cos, number) trig(:cos, number)
end end
Sass::Script::Functions.declare :cos, [:number]
def tan(number) def tan(number)
trig(:tan, number) trig(:tan, number)
end end
Sass::Script::Functions.declare :tan, [:number]
def e() def e()
Sass::Script::Number.new(Math::E) Sass::Script::Number.new(Math::E)
end end
Sass::Script::Functions.declare :pi, []
def log(number, base = Sass::Script::Number.new(Math::E)) def logarithm(number, base = e )
assert_type number, :Number assert_type number, :Number
assert_type base, :Number assert_type base, :Number
raise Sass::SyntaxError, "base to logarithm must be unitless." unless base.unitless?
Sass::Script::Number.new(Math.log(number.value, base.value), number.numerator_units, number.denominator_units) Sass::Script::Number.new(Math.log(number.value, base.value), number.numerator_units, number.denominator_units)
end end
Sass::Script::Functions.declare :logarithm, [:number]
Sass::Script::Functions.declare :logarithm, [:number, :base]
def sqrt(number) def sqrt(number)
numeric_transformation(number) { |n| Math.sqrt(n) } numeric_transformation(number) { |n| Math.sqrt(n) }
end end
Sass::Script::Functions.declare :sqrt, [:number]
alias square_root sqrt
Sass::Script::Functions.declare :square_root, [:number]
def pow(number, exponent) def pow(number, exponent)
assert_type number, :Number assert_type number, :Number
assert_type exponent, :Number assert_type exponent, :Number
raise Sass::SyntaxError, "exponent to pow must be unitless." unless exponent.unitless?
Sass::Script::Number.new(number.value**exponent.value, number.numerator_units, number.denominator_units) Sass::Script::Number.new(number.value**exponent.value, number.numerator_units, number.denominator_units)
end end
Sass::Script::Functions.declare :pow, [:number, :exponent]
private private
def trig(operation, number) def trig(operation, number)

View File

@ -82,11 +82,17 @@ class SassExtensionsTest < Test::Unit::TestCase
assert_equal "0.959", evaluate("sin(360)") assert_equal "0.959", evaluate("sin(360)")
assert evaluate("tan(pi()/2 - 0.0001)").to_f > 1000, evaluate("tan(pi()/2 - 0.0001)") assert evaluate("tan(pi()/2 - 0.0001)").to_f > 1000, evaluate("tan(pi()/2 - 0.0001)")
assert evaluate("tan(pi()/2 + 0.0001)").to_f < -1000, evaluate("tan(pi()/2 - 0.0001)") assert evaluate("tan(pi()/2 + 0.0001)").to_f < -1000, evaluate("tan(pi()/2 - 0.0001)")
assert_equal "0.693px", evaluate("log(2px)") assert_equal "0.693px", evaluate("logarithm(2px)")
assert_equal "0", evaluate("log(1)") assert_equal "0", evaluate("logarithm(1)")
assert_equal "1", evaluate("log(e())") assert_equal "1", evaluate("logarithm(e())")
assert_equal "1", evaluate("logarithm($number: e())")
assert_equal "1", evaluate("logarithm(10, $base: 10)")
assert_equal "5px", evaluate("sqrt(25px)") assert_equal "5px", evaluate("sqrt(25px)")
assert_equal "5px", evaluate("sqrt($number: 25px)")
assert_equal "5px", evaluate("square-root(25px)")
assert_equal "5px", evaluate("square-root($number: 25px)")
assert_equal "25px", evaluate("pow(5px, 2)") assert_equal "25px", evaluate("pow(5px, 2)")
assert_equal "25px", evaluate("pow($number: 5px, $exponent: 2)")
assert_equal "79.432px", evaluate("pow(5px, e())") assert_equal "79.432px", evaluate("pow(5px, e())")
end end