Skip to content

Conversation

@AnslemHack
Copy link
Contributor

@AnslemHack AnslemHack commented Dec 2, 2025

This PR fixes #3595 introduces two new arithmetic functions, num() and den(), which extract the numerator and denominator from Fraction objects respectively.

Copy link
Owner

@josdejong josdejong left a comment

Choose a reason for hiding this comment

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

Thanks for working out these functions num and den @AnslemHack . Your PR looks well taken care of!

I made a few inline comments, can you have a look at those?

On a side note: the detailed description of your PR looks AI-generated and doesn't really add useful information to me 😅. I think it's enough to explain that "this PR addresses #3595, implementing two new functions, num and den".

@AnslemHack
Copy link
Contributor Author

Thanks for working out these functions num and den @AnslemHack . Your PR looks well taken care of!

I made a few inline comments, can you have a look at those?

On a side note: the detailed description of your PR looks AI-generated and doesn't really add useful information to me 😅. I think it's enough to explain that "this PR addresses #3595, implementing two new functions, num and den".

my apologise for the verbose @josdejong , I must have over refined my output description, I have now made changes to that with a brief description of what it simply does, hope this is better

@josdejong
Copy link
Owner

Thanks!

@gwhitney
Copy link
Collaborator

gwhitney commented Dec 4, 2025

A few more items:

  • re() and im() are in src/function/complex. Shouldn't these be in a new subdirectory src/function/fraction? (Jos?)
  • Please add some tests showing that num and den do reasonable things on number and bigint input.
  • Presumably at the moment num and den will fail on BigNumber input, because there is no implicit conversion from BigNumber to Fraction. But it seems to me that for these two particular functions, what else could one mean but to convert the BigNumber to Fraction and then return the numerator or denominator? So, with @josdejong's blessing, please add an explicit signature for BigNumber for both functions that does the conversion and then returns the numerator or denominator. And then tests for this behavior, of course. Thanks!

@AnslemHack
Copy link
Contributor Author

A few more items:

  • re() and im() are in src/function/complex. Shouldn't these be in a new subdirectory src/function/fraction? (Jos?)
  • Please add some tests showing that num and den do reasonable things on number and bigint input.
  • Presumably at the moment num and den will fail on BigNumber input, because there is no implicit conversion from BigNumber to Fraction. But it seems to me that for these two particular functions, what else could one mean but to convert the BigNumber to Fraction and then return the numerator or denominator? So, with @josdejong's blessing, please add an explicit signature for BigNumber for both functions that does the conversion and then returns the numerator or denominator. And then tests for this behavior, of course. Thanks!

thanks for your feedback I have now addressed your concerns. please take another look.

Copy link
Collaborator

@gwhitney gwhitney left a comment

Choose a reason for hiding this comment

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

Thanks for great progress! Just a few things were overlooked or need to be brought into consistency with the rest of mathjs. Thanks!

return typed(name, {
Fraction: (x) => x.d,
BigNumber: (x) => fraction(x).d,
'Array | Matrix': typed.referToSelf((self) => (x) => deepMap(x, self))
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you look throughout math.js, you will see the convention is to drop the parentheses in unary arrow-functions. E.g., this last one should be self => x => deepMap(s, self), and so on for several other examples throughout this PR. Please correct, thanks.

Copy link
Collaborator

Choose a reason for hiding this comment

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

There are still several examples of (onlyOneArgument) => functionBody rather than onlyOneArgument => functionBody in this PR; please convert to the latter format which is far more common throughout mathjs source code, thank you!

const fractionMatrix = math.matrix([math.fraction(1, 2), math.fraction(3, 4)])
expectTypeOf(math.num(fractionMatrix)).toMatchTypeOf<Matrix>()
expectTypeOf(math.den(fractionMatrix)).toMatchTypeOf<Matrix>()
assert.deepStrictEqual(
Copy link
Collaborator

Choose a reason for hiding this comment

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

And you can put the more specific Matrix<bigint> type here, and add an example where you call num/den on an ordinary number or bigint. Thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Great, thanks! Just need to also put the <bigint> into .toMatchTypeOf<MathArray>() in a couple of places above as well.

@AnslemHack AnslemHack requested a review from gwhitney December 20, 2025 23:40
@AnslemHack
Copy link
Contributor Author

@gwhitney @josdejong I believe to have addressed all concerns on this pr now.. would appreciate if you can have a look, still open for any further feedbacks or adjustment that might be required

@gwhitney
Copy link
Collaborator

Yes, very close now. Just a couple of open items, and please in addition to taking care of those also update to be on top of the latest version of the develop branch, thanks. Should be ready to merge after that.

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.

Provide access to numerator and denominator of fraction via num() and den() helpers

3 participants