-
Notifications
You must be signed in to change notification settings - Fork 117
fromFixnum(): fix for when argument fixnum is less than about 1e-7, brownplt/code.pyret.org#556 #1810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: horizon
Are you sure you want to change the base?
Conversation
The added test that |
@@ -2036,6 +2055,8 @@ define("pyret-base/js/js-numbers", function() { | |||
|
|||
var scientificPattern = new RegExp("^([+-]?\\d*\\.?\\d*)[Ee]([+]?\\d+)$"); | |||
|
|||
var genScientificPattern = new RegExp("^([+-]?\\d*\\.?\\d*)[Ee]([+-]?\\d+)$"); |
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.
Is scientificPattern
used elsewhere? Is it wrong when used elsewhere?
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.
scientificPattern
, which doesn't allow a negative exponent, is used by expandExponent()
, which is used by makeBignum()
. I don't know why a negative exponent is explicitly disallowed. It's true that makeBignum()
, since it makes "big" integers, doesn't need to handle negative exponents.
For fromFixnum()
, we definitely need to handle negative exponents. The bug for which this fix was made was about misreading numbers like 5e-19
from an Excel sheet. So the new regexp pattern had to be introduced.
src/js/base/js-numbers.js
Outdated
factor1 = exponentValue; | ||
} | ||
} | ||
match = stringRep.match(/^(.*)\.(.*)$/); |
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.
Can we call factor1
something like exponentFactor
instead? That would have helped me a lot to understand why it's initialized to 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.
It would have also helped me to read the code if the match
variable wasn't re-used.
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.
Done
src/js/base/js-numbers.js
Outdated
} | ||
} | ||
match = stringRep.match(/^(.*)\.(.*)$/); | ||
var factor2; |
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.
Similarly could this be called baseFactor
?
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.
Done
src/js/base/js-numbers.js
Outdated
} else { | ||
return Rational.makeInstance(x, 1, errbacks); | ||
factor2 = Rational.makeInstance(Number(stringRep), 1, errbacks); |
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.
Can you help me understand this change? What's an example where using x
would have been wrong? I can't think of one, but maybe I'm not thinking through the ranges correctly.
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.
I had not updated x
to be the "shorter" (i.e. sans exponent) number and hence had to use Number(stringRep)
.
In the most recent push, I've updated x
and so can use Rational.makeInstance(x, 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, I found the older code dealing with the decimal point a bit opaque, so I've taken the liberty of using more expressive variable names and added comments to aid a future traveler. Hope that's ok.
I just spent a little more time reading js-numbers. Can we explain how this function differs in purpose from This PR is looking more and more like the pyret-lang/src/js/base/js-numbers.js Line 2057 in 175b934
In fact, Should we be re-using that logic? |
No description provided.