admin管理员组文章数量:1296465
// t: current time, b: begInnIng value, c: change In value, d: duration
def: 'easeOutQuad',
swing: function (x, t, b, c, d) {
//alert(jQuery.easing.default);
return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
},
easeInQuad: function (x, t, b, c, d) {
return c*(t/=d)*t + b;
},
easeOutQuad: function (x, t, b, c, d) {
return -c *(t/=d)*(t-2) + b;
},
I am trying to convert Robert Penner's Easing Functions into python and getting stuck! Any help or any one else done this before?
.easing.js
// t: current time, b: begInnIng value, c: change In value, d: duration
def: 'easeOutQuad',
swing: function (x, t, b, c, d) {
//alert(jQuery.easing.default);
return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
},
easeInQuad: function (x, t, b, c, d) {
return c*(t/=d)*t + b;
},
easeOutQuad: function (x, t, b, c, d) {
return -c *(t/=d)*(t-2) + b;
},
I am trying to convert Robert Penner's Easing Functions into python and getting stuck! Any help or any one else done this before?
https://github./danro/jquery-easing/blob/master/jquery.easing.js
Share edited Mar 20, 2013 at 22:16 Matt K 13.9k3 gold badges35 silver badges52 bronze badges asked Mar 20, 2013 at 22:14 justachapjustachap 3431 gold badge6 silver badges12 bronze badges 10-
2
a /= b
is equal toa = a / b;
. – Rob W Commented Mar 20, 2013 at 22:16 - 1 It's the shorthand divide-and-assign operator – Bergi Commented Mar 20, 2013 at 22:17
-
1
It's worth noting that, while Python also has a
/=
that does a divide-and-assign like JS's, assignment a statement rather than an expression in Python, so-c * (t/=d)*(t-2) + b will be a
SyntaxError`. – abarnert Commented Mar 20, 2013 at 22:19 -
@VisioN: But you don't have to do it twice, since
t
is reused. "simple division" would requirec*t/d*t/d+b
– Bergi Commented Mar 20, 2013 at 22:21 -
@Bergi: we don't know if it's reused. It depends on whether
t/=d
ort-2
is evaluated first. – André Caron Commented Mar 20, 2013 at 22:21
5 Answers
Reset to default 7In both JavaScript and Python, /=
is an "augmented assignment" operator, with pretty much the same meaning.
In JS:
var i = 10;
i /= 2;
… is equivalent to:
var i = 10;
i = i / 2;
And, in Python:
i = 10
i /= 2
… is likewise equivalent (not quite exactly the same, but close enough for numbers) to:
i = 10
i = i / 2
However, there is one very big difference.
In JavaScript, assignment is an expression—it has a value, and that value is the value being assigned to the variable. So:
var i = 10;
var j = i /= 2;
… is roughly equivalent to:
var i = 10;
i /= 2;
var j = i;
In Python, assignment is a statement. It has no value, and cannot be used in an expression. So:
i = 10
j = i /= 2
… raises a SyntaxError
.
Porting code that uses assignment (augmented or otherwise) in the middle of an expression generally requires breaking that expression up into multiple lines and/or finding a way to rewrite the expression to not require any assignments. (But often, that's not a bad thing, because the original expressions weren't very readable anyway…)
For example, assuming JS evaluates operands from left to right (which I'm not sure is guaranteed?):
def easeInQuad(x, t, b, c, d):
t /= d
return c*t*t+b
More generally, you do this:
old_t = t
t /= d
And then you replace any instances of t
before that t/=d
with old_t
, and leave all instances from t/=d
and later alone. Fortunately, in this case, there are no previous instances, so we don't need that old_t
stuff.
And if you think about it, you can easily get the same effect without ever changing t
, in one line, much more readably, in any of the following ways:
return c * (t/d) * (t/d) + b
return c * (t/d)**2 + b
return c * t*t / d*d + b
Someone who thinks in C will immediately plain that these are all "too slow". After all, the first does an extra division, the second does an exponentiation instead of a multiplication, and the third does two multiplications instead of one. Horrors!
Of course you can always use a temporary variable:
t_over_d = t/d
return c * t_over_d * t_over_d + b
… but again, to a C programmer, that implies that you're using up a valuable register. Sure, every piler written after, say, 1985 will detect that t
is dead as soon as t_over_d
appears and reuse the same register, but why not force it to reuse the register if we can, especially if it saves a few keystrokes too?
In JS or Python, the cost of a multiplication is such a tiny fraction of the cost of calling a function, and interpreting the bytecode, and so on that you'd never even notice it. Meanwhile, the cost of rebinding a local variable (especially in a V8-style or PyPy-style JIT interpreter) might be much, much higher than the cost of passing around an unnamed temporary result.
So, this is a paradigm case of misguided "optimization" making code much harder to understand, while probably slowing it down instead of speeding it up, and in an area that cannot possibly be a bottleneck worth optimizing anyway.
Since gnibbler brought up the question of whether JavaScript actually does guarantee this evaluation order…
First, JavaScript is defined as, effectively, "what Firefox does" (and "what Spidermonkey does", but that ought to be the same thing—and, if it isn't, then JavaScript does 2 things, so that's twice as good, right?). But ECMAScript is defined by standards, and it's those standards that every JS implementation (despite the name) pays lip service to, and we can pretend that ECMAScript 5.1 is the standard that all implementations conform to (which is true so long as "all implementations" means "Opera"). You can find it here.
So, in ES 5.1: 11.5 Multiplicative Operators guarantees that the result of (t/=d)
will be evaluated before t
, and 11.13.2 Compound Assignment guarantees that evaluating t/=d
will set the value of t
before it finishes. (You have to read up on what "evaluate" means and what GetValue
an SetValue
mean, but I'm pretty sure this really is guaranteed.)
/=
is an augmented assignment operator. t /= d
is the same as t = t / d
. +=
, -=
, and *=
also exist, among others...
Well its a short-hand operator just like the +=
but instead of adding it divides. Here is the long form
t = t / d
c*(t/=d)*t + b;
Is equivalent to
t /= d # or t = t / d
c * t * t + b
Because Python can't to assigment inside expressions
Remember that if t
and d
are both int
/long
, this will be a truncated division in Python2
Likewise easeOutQuad
would be
def easeOutQuad (x, t, b, c, d):
t /= d
return -c * t * (t - 2) + b
Divide and assign.
>>> p = 6
>>> p /= 2
>>> p
3
本文标签: javascriptWhat does td mean Python and getting errorsStack Overflow
版权声明:本文标题:javascript - What does t = d mean? Python and getting errors - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741629104a2389254.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论