Skip to content

Conversation

ds26gte
Copy link
Contributor

@ds26gte ds26gte commented Aug 25, 2025

No description provided.

@ds26gte
Copy link
Contributor Author

ds26gte commented Aug 27, 2025

The added test that fromFixnum() works for small numbers uses the JSON library as @jpolitz suggested.

@@ -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+)$");
Copy link
Member

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?

Copy link
Contributor Author

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.

factor1 = exponentValue;
}
}
match = stringRep.match(/^(.*)\.(.*)$/);
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
}
match = stringRep.match(/^(.*)\.(.*)$/);
var factor2;
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

} else {
return Rational.makeInstance(x, 1, errbacks);
factor2 = Rational.makeInstance(Number(stringRep), 1, errbacks);
Copy link
Member

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.

Copy link
Contributor Author

@ds26gte ds26gte Aug 30, 2025

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

Copy link
Contributor Author

@ds26gte ds26gte Aug 30, 2025

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.

@jpolitz
Copy link
Member

jpolitz commented Sep 2, 2025

I just spent a little more time reading js-numbers. Can we explain how this function differs in purpose from fromString? Like is it the case that the entire body of fromFixnum could be return fromString(String(x)), and we'd get exactly the same results?

This PR is looking more and more like the flonumRegexp match case:

if (aMatch) {

In fact, flonumRegexp seems awfully close to the new pattern for genScientificPattern (is there a difference other than the grouping?)

https://github.com/brownplt/pyret-lang/blob/175b9342e23d6db9098d3c00550d913d9bf95d31/src/js/base/js-numbers.js#L2029C7-L2029C19

Should we be re-using that logic?

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

Successfully merging this pull request may close these issues.

2 participants