-
Couldn't load subscription status.
- Fork 120
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
fromFixnum(): fix for when argument fixnum is less than about 1e-7, brownplt/code.pyret.org#556 #1810
Changes from 1 commit
10ba82d
92aee48
11cbc5c
a9fb542
777d118
b3311db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,17 +173,36 @@ define("pyret-base/js/js-numbers", function() { | |
| } else { | ||
| // used to return float, now rational | ||
| var stringRep = x.toString(); | ||
| var match = stringRep.match(/^(.*)\.(.*)$/); | ||
| var match = stringRep.match(genScientificPattern); | ||
| var factor1 = 1; | ||
| if (match) { | ||
| var divideP = false; | ||
| stringRep = match[1]; | ||
| var exponentPart = match[2]; | ||
| if (exponentPart.match('^-')) { | ||
| divideP = true; | ||
| exponentPart = exponentPart.substring(1); | ||
| } | ||
| var exponentValue = makeBignum("1" + zfill(Number(exponentPart))); | ||
| if (divideP) { | ||
| factor1 = divide(1, exponentValue); | ||
| } | ||
| else { | ||
| factor1 = exponentValue; | ||
| } | ||
| } | ||
| match = stringRep.match(/^(.*)\.(.*)$/); | ||
| var factor2; | ||
|
||
| if (match) { | ||
| var afterDecimal = parseInt(match[2]); | ||
| var factorToInt = Math.pow(10, match[2].length); | ||
| var extraFactor = _integerGcd(factorToInt, afterDecimal); | ||
| var multFactor = factorToInt / extraFactor; | ||
| return Rational.makeInstance(Math.round(x*multFactor), Math.round(factorToInt/extraFactor), errbacks); | ||
| factor2 = Rational.makeInstance(Math.round(x*multFactor), Math.round(factorToInt/extraFactor), errbacks); | ||
| } else { | ||
| return Rational.makeInstance(x, 1, errbacks); | ||
| factor2 = Rational.makeInstance(Number(stringRep), 1, errbacks); | ||
|
||
| } | ||
|
|
||
| return multiply(factor1, factor2); | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -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+)$"); | ||
|
||
|
|
||
| // fromString: string -> (pyretnum | false) | ||
| var fromString = function(x, errbacks) { | ||
| if (x.match(digitRegexp)) { | ||
|
|
||
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
factor1something likeexponentFactorinstead? That would have helped me a lot to understand why it's initialized to1.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
matchvariable 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