Skip to content

Commit 025c770

Browse files
authored
Merge pull request #1810 from ds26gte/fromFixnum-fix
fromFixnum(): fix for when argument fixnum is less than about 1e-7, brownplt/code.pyret.org#556
2 parents 11bb00b + b3311db commit 025c770

File tree

2 files changed

+4
-26
lines changed

2 files changed

+4
-26
lines changed

src/js/base/js-numbers.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -160,31 +160,7 @@ define("pyret-base/js/js-numbers", function() {
160160

161161
// fromFixnum: fixnum -> pyretnum
162162
var fromFixnum = function(x, errbacks) {
163-
if (!isFinite(x)) {
164-
return Roughnum.makeInstance(x, errbacks);
165-
}
166-
var nf = Math.floor(x);
167-
if (nf === x) {
168-
if (isOverflow(nf)) {
169-
return makeBignum(expandExponent(x+''));
170-
} else {
171-
return nf;
172-
}
173-
} else {
174-
// used to return float, now rational
175-
var stringRep = x.toString();
176-
var match = stringRep.match(/^(.*)\.(.*)$/);
177-
if (match) {
178-
var afterDecimal = parseInt(match[2]);
179-
var factorToInt = Math.pow(10, match[2].length);
180-
var extraFactor = _integerGcd(factorToInt, afterDecimal);
181-
var multFactor = factorToInt / extraFactor;
182-
return Rational.makeInstance(Math.round(x*multFactor), Math.round(factorToInt/extraFactor), errbacks);
183-
} else {
184-
return Rational.makeInstance(x, 1, errbacks);
185-
}
186-
187-
}
163+
return fromString(String(x), errbacks);
188164
};
189165

190166
var expandExponent = function(s) {
@@ -2063,7 +2039,6 @@ define("pyret-base/js/js-numbers", function() {
20632039

20642040
var roughnumRatRegexp = new RegExp("^~([+-]?\\d+)/(\\d+)$");
20652041

2066-
20672042
var scientificPattern = new RegExp("^([+-]?\\d*\\.?\\d*)[Ee]([+]?\\d+)$");
20682043

20692044
// fromString: string -> (pyretnum | false)

tests/pyret/tests/test-json.arr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ check "conversion":
1919
p('[5, null, {"hello": "world"}]') is
2020
J.j-arr([list: J.j-num(5), J.j-null,
2121
J.j-obj([SD.string-dict: "hello", J.j-str("world")])])
22+
23+
p('1E-7').native() is 1e-7
24+
p('5E-19').native() is 5e-19
2225
end
2326

2427
check "native":

0 commit comments

Comments
 (0)