Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Dec 5, 2018. It is now read-only.

Update Math.js to fix Issue 256 #257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

lgto4
Copy link
Contributor

@lgto4 lgto4 commented Jul 19, 2016

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".

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
Copy link
Member

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;

Copy link
Member

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?

Copy link
Contributor Author

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) {
Copy link
Member

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;
}

Copy link
Contributor Author

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);

Copy link
Member

@Pomax Pomax Aug 2, 2016

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...)

Copy link
Contributor

@GoToLoop GoToLoop Aug 2, 2016

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

Copy link
Contributor Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants