-
Notifications
You must be signed in to change notification settings - Fork 884
Update Math.js to fix Issue 256 #257
base: master
Are you sure you want to change the base?
Conversation
The fix to this issue is the same as the fix to Issue 244: if the result is out of range, try again. The function was restructured to invoke `internalRandomGenerator` at only one location after the function's parameters' values have been set. The test for the degenerate case where `a===b` and the not-so-infinite retry loop remain intact. The function's header comment was cleaned up and ill chosen words like "high", "low" and "top" were removed. Similarly, neutral variable names replaced those with "min" and "max".
// assertion: ir is never less than 0.5 | ||
} | ||
return aMin; | ||
var a, b; // range from a to b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we're explicitly using a
and b
, it's better to make these function parameters, p.random = function(a,b) {...
and then bootstrap them with one line for each variable, rather than using arguments.length
, like:
a = a || 0;
b = b || 1;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also looks like your text editor decided to use tabs instead of 2 spaces for indentation, can that be fixed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, even though I chose 2 spaces, apparently if I copy & paste from source that has tabs, those tabs are maintained, but only when I commit the changes. Apparently.
I added parameters a
and b
, but there is no escaping the use of arguments.length
. Note that b = b || 1;
would screw up any any range ending with zero.
Spaces, not tabs. Explicit declaration of parameters, though there is no escaping the referencing of arguments.length.
return internalRandomGenerator(); | ||
p.random = function(a, b) { | ||
a = a || 0; | ||
if (arguments.length === 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with named arguments, we can simplify a little more:
if (arguments.length === 1) {
b = a;
a = 0;
} else {
a = a || 0;
b = b || 1;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You cannot do b = b || 1;
! That well mess up invocations like random(-5, 0);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that is a good point. But we can do b = (typeof b !== 'undefined') ? b : 1
(really wish there was a pragma that made truthiness easier in JS in that respect...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to rely on operator typeof
for parameter variables. We can test for 'em directly.
Also, it's much safer to catch null
as well by using !=
instead of !==
:
b = b != null? b : 1
or b = b != undefined? b : 1
or b = b != void 0? b : 1
Other alternatives:
b = b == null && 1 || b
or b = b == undefined && 1 || b
or b = b == void 0 && 1 || b
Or even the good old if ()
:
if (b == null) b = 1
or if (b == undefined) b = 1
or if (b == void 0) b = 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, the different ways to it correctly is only slightly less than all the ways to do it incorrectly. So I'm leaving my second iteration as is.
The fix to this issue is the same as the fix to Issue 244: if the result is out of range, try again. The function was restructured to invoke
internalRandomGenerator
at only one location after the function's parameters' values have been set. The test for the degenerate case wherea===b
and the not-so-infinite retry loop remain intact. The function's header comment was cleaned up and ill chosen words like "high", "low" and "top" were removed. Similarly, neutral variable names replaced those with "min" and "max".