sass-planifolia

Vanilla Sass helper functions
git clone https://git.ce9e.org/sass-planifolia.git

commit
84f52e2a46aca7fc28f549b237e8911d76e4b49b
parent
a62912c7f83f37e52d8c7cf35c00c262dcb04de6
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2017-12-27 07:45
math: rename `ln()` to `log()` and `log()` to `log10()`

Diffstat

M CHANGES.md 5 +++++
M sass/math.scss 22 +++++++++++-----------
M test/math.js 18 +++++++++---------

3 files changed, 25 insertions, 20 deletions


diff --git a/CHANGES.md b/CHANGES.md

@@ -1,3 +1,8 @@
   -1     1 0.6.0 (unreleased)
   -1     2 ------------------
   -1     3 
   -1     4 - math: rename `ln()` to `log()` and `log()` to `log10()`
   -1     5 
    1     6 0.5.0 (2017-12-05)
    2     7 ------------------
    3     8 

diff --git a/sass/math.scss b/sass/math.scss

@@ -44,7 +44,7 @@ $planifolia-math-steps-default: 32 !default;
   44    44   @return $result;
   45    45 }
   46    46 
   47    -1 @function _pf-ln-taylor-1($x, $steps) {
   -1    47 @function _pf-log-taylor-1($x, $steps) {
   48    48   $z: ($x - 1) / ($x + 1);
   49    49 
   50    50   $power: $z;
@@ -100,12 +100,12 @@ $planifolia-math-steps-default: 32 !default;
  100   100   }
  101   101 }
  102   102 
  103    -1 @function _pf-log-approx($x) {
   -1   103 @function _pf-log10-approx($x) {
  104   104   @if $x <= 0 {
  105   105     @error 'cannot calculate log of #{$x}';
  106   106   } @else if $x >= 1 {
  107   107     // choose the smaller option (-1) because it yields better
  108    -1     // results in ln().
   -1   108     // results in log().
  109   109     @return str-length(inspect(round($x))) - 1;
  110   110   } @else {
  111   111     @return -1 * str-length(inspect(round(1 / $x)));
@@ -115,20 +115,20 @@ $planifolia-math-steps-default: 32 !default;
  115   115 /// @param {number} $x
  116   116 /// @param {number} $steps [32] - steps of the taylor expansion
  117   117 /// @return {number}
  118    -1 @function ln($x, $steps: $planifolia-math-steps-default) {
  119    -1   $ln10: 2.302585092994046;
  120    -1   $approx: _pf-log-approx($x);
   -1   118 @function log($x, $steps: $planifolia-math-steps-default) {
   -1   119   $log10: 2.302585092994046;
   -1   120   $approx: _pf-log10-approx($x);
  121   121   // $y is in range [1, 10]
  122   122   $y: $x / _pf-pow-int(10, $approx);
  123    -1   @return $approx * $ln10 + _pf-ln-taylor-1($y, $steps);
   -1   123   @return $approx * $log10 + _pf-log-taylor-1($y, $steps);
  124   124 }
  125   125 
  126   126 /// @param {number} $x
  127   127 /// @param {number} $steps [32] - steps of the taylor expansion
  128   128 /// @return {number}
  129    -1 @function log($x, $steps: $planifolia-math-steps-default) {
  130    -1   $ln10: 2.302585092994046;
  131    -1   @return ln($x, $steps) / $ln10;
   -1   129 @function log10($x, $steps: $planifolia-math-steps-default) {
   -1   130   $log10: 2.302585092994046;
   -1   131   @return log($x, $steps) / $log10;
  132   132 }
  133   133 
  134   134 /// @param {number} $x
@@ -145,7 +145,7 @@ $planifolia-math-steps-default: 32 !default;
  145   145   } @else if $x == 0 and $exponent > 0 {
  146   146     @return 0;
  147   147   } @else {
  148    -1     $y: ln($x, $steps) * $exp2;
   -1   148     $y: log($x, $steps) * $exp2;
  149   149     $pow2: _pf-exp-taylor-0($y, $steps);
  150   150     @return $pow1 * $pow2;
  151   151   }

diff --git a/test/math.js b/test/math.js

@@ -54,18 +54,18 @@ describe('math', function() {
   54    54     });
   55    55   });
   56    56 
   57    -1   describe('ln', function() {
   58    -1     it('ln(1) == 0', function() {
   59    -1       sassaby.func('ln').calledWithArgs('1').equals('0');
   -1    57   describe('log', function() {
   -1    58     it('log(1) == 0', function() {
   -1    59       sassaby.func('log').calledWithArgs('1').equals('0');
   60    60     });
   61    -1     it('ln(0.1)', function() {
   62    -1       shared.similar(sassaby.func('ln').calledWithArgs('0.1'), Math.log(0.1));
   -1    61     it('log(0.1)', function() {
   -1    62       shared.similar(sassaby.func('log').calledWithArgs('0.1'), Math.log(0.1));
   63    63     });
   64    -1     it('ln(123456789)', function() {
   65    -1       shared.similar(sassaby.func('ln').calledWithArgs('123456789'), Math.log(123456789));
   -1    64     it('log(123456789)', function() {
   -1    65       shared.similar(sassaby.func('log').calledWithArgs('123456789'), Math.log(123456789));
   66    66     });
   67    -1     it('ln(12345678.9)', function() {
   68    -1       shared.similar(sassaby.func('ln').calledWithArgs('12345678.9'), Math.log(12345678.9));
   -1    67     it('log(12345678.9)', function() {
   -1    68       shared.similar(sassaby.func('log').calledWithArgs('12345678.9'), Math.log(12345678.9));
   69    69     });
   70    70   });
   71    71