Specify the maximum of digits when parsing a Fraction
----------------------------------------------------- Key: MATH-181 URL: https://issues.apache.org/jira/browse/MATH-181 Project: Commons Math Issue Type: Improvement Affects Versions: 1.1 Reporter: Niall Pemberton Priority: Minor Attachments: MATH-181-FractionDigitsLimit.patch Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. So for example: format "?/?" displays decimal values formatted in the range 1/2 to n/9 format "??/??" displays decimal values formatted in the range 1/2 to n/99 format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Niall Pemberton updated MATH-181: --------------------------------- Attachment: MATH-181-FractionDigitsLimit.patch > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12561796#action_12561796 ] Luc Maisonobe commented on MATH-181: ------------------------------------ I find the javadoc of the private four arguments constructor misleading. It does not really enforce the fact the result is within epsilon of the true result in all cases. The constructor seems designed for only two different call sequence: non-zero epsilon and INTEGER.MAX_VALUE maxDenominator on the one hand, ignored epsilon (set to 0 in the public constructor) and non-maximal maxDenominator on the other hand. The current javadoc seems to say both constraints on epsilon and maxDenominator are enforced at the same time, I don't think it's true. > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12562019#action_12562019 ] Niall Pemberton commented on MATH-181: -------------------------------------- Yes, I mostly just copied the existing constructor and didn't put any effort - sorry :( Its an either or situation and not optimal (which is why I made it private) - but it means that with a couple of slight modifications the same code can be (re)used either with an epsilon or specified max. denominator digits. I can either submit another patch with improved JavaDoc or spearate out the two cases, duplicating the double-->fraction code. Which would you prefer? > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12562626#action_12562626 ] Luc Maisonobe commented on MATH-181: ------------------------------------ I understand your rationale here. I also don't like copying code that is almost identical, it leads to a maintenance nightmare. Updating only the javadoc to explain the two exclusive operating modes for this constructor and hence to strongly advise this constructor remains private should be sufficient. > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Niall Pemberton updated MATH-181: --------------------------------- Attachment: MATH-181-FractionDigitsLimit-v2.patch version 2 patch attached with (hopefully) improved JavaDocs > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit-v2.patch, MATH-181-FractionDigitsLimit.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12562953#action_12562953 ] Phil Steitz commented on MATH-181: ---------------------------------- Second patch looks good to me. One thing I would add is check in Fraction(double value, int denominatorDigits) to make sure denominatorDigits is not too big for an int - i.e., guard against integer overflow in (int)Math.pow(10, denominatorDigits) > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit-v2.patch, MATH-181-FractionDigitsLimit.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12563124#action_12563124 ] Luc Maisonobe commented on MATH-181: ------------------------------------ The patch has been commited as of r615856, including the addition of a check for denominatorDigits value as suggested by Phil. I will probably look more in depth on this issue, because I think some improvements can be made. Continued fractions are a very efficient way to approximate doubles. Their accuracy is linked to the *square* of the denominator of the first ignored term. So I think converting the number of digits to a max denominator simply as 10^d is too much. So I don't close the issue yet. > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit-v2.patch, MATH-181-FractionDigitsLimit.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Niall Pemberton updated MATH-181: --------------------------------- Attachment: MATH-181-FractionMaxDenominator.patch Thanks Luc - when I submitted this I was only thinking of my own simple use-case but perhaps its better (and allows finer grained control) to just be able to specify the maximum denominator value in the constructor - it avoids the overflow issue altogether. Attaching a patch, although it meets my needs as it is. > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit-v2.patch, MATH-181-FractionDigitsLimit.patch, MATH-181-FractionMaxDenominator.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12564531#action_12564531 ] luc edited comment on MATH-181 at 1/31/08 2:39 PM: ------------------------------------------------------------- I have checked the ideas I had about denominator. They do not address your issue, so the patch as applied is good. However, I noticed the last statement in the private constructor was a call to reduce. This seems strange to me because a known property of the recursive algorithm to build the convergents cn=pn/qn is that they are already reduced. This is a result of applying the Bezout theorem to the easily proved recursive property p(n)q(n-1) - p(n-1)q(n) = (-1)^(n+1). This property holds even if a1 is completely wrong due to numerical errors. When I remove this statement, the existing test succeed as expected. I did not find any related information in the log message nor in JIRA. May I remove this statement or did I miss something ? was (Author: luc): I have checked the ideas I had about denominator. They are do not address your issue, so the patch as applied is good. However, I noticed the last statement in the private constructor was a call to reduce. This seems strange to me because a known property of the recursive algorithm to build the convergents cn=pn/qn is that they are already reduced. This is a result of applying the Bezout theorem to the easily proved recursive property p(n)q(n-1) - p(n-1)q(n) = (-1)^(n+1). This property holds even if a1 is completely wrong due to numerical errors. When I remove this statement, the existing test succeed as expected. I did not find any related information in the log message nor in JIRA. May I remove this statement or did I miss something ? > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit-v2.patch, MATH-181-FractionDigitsLimit.patch, MATH-181-FractionMaxDenominator.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12564531#action_12564531 ] Luc Maisonobe commented on MATH-181: ------------------------------------ I have checked the ideas I had about denominator. They are do not address your issue, so the patch as applied is good. However, I noticed the last statement in the private constructor was a call to reduce. This seems strange to me because a known property of the recursive algorithm to build the convergents cn=pn/qn is that they are already reduced. This is a result of applying the Bezout theorem to the easily proved recursive property p(n)q(n-1) - p(n-1)q(n) = (-1)^(n+1). This property holds even if a1 is completely wrong due to numerical errors. When I remove this statement, the existing test succeed as expected. I did not find any related information in the log message nor in JIRA. May I remove this statement or did I miss something ? > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit-v2.patch, MATH-181-FractionDigitsLimit.patch, MATH-181-FractionMaxDenominator.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12564536#action_12564536 ] Niall Pemberton commented on MATH-181: -------------------------------------- I have no idea - it was in the original constructor and my math is rubbish compare to you guys. > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit-v2.patch, MATH-181-FractionDigitsLimit.patch, MATH-181-FractionMaxDenominator.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12564637#action_12564637 ] Phil Steitz commented on MATH-181: ---------------------------------- Yes, I think the call to reduce can be removed. > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit-v2.patch, MATH-181-FractionDigitsLimit.patch, MATH-181-FractionMaxDenominator.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
In reply to this post by JIRA jira@apache.org
[ https://issues.apache.org/jira/browse/MATH-181?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Luc Maisonobe resolved MATH-181. -------------------------------- Resolution: Fixed fixed as of r617411 As proposed by Niall, the maxDenominatorDigit has been replaced by maxDenominator. This means instead of 2 max digits, one should now provide 99 max denominator. The call to reduce() has also been removed. > Specify the maximum of digits when parsing a Fraction > ----------------------------------------------------- > > Key: MATH-181 > URL: https://issues.apache.org/jira/browse/MATH-181 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 1.1 > Reporter: Niall Pemberton > Priority: Minor > Attachments: MATH-181-FractionDigitsLimit-v2.patch, MATH-181-FractionDigitsLimit.patch, MATH-181-FractionMaxDenominator.patch > > > Firstly, thanks for the Fraction code - I've adapated it for something I'm working on and I didn't have a clue how to convert a decimal to a fraction :) > Excel spreadsheets have the facility to specify a fraction format where you specify the maximum number of denominator digits to display. > So for example: > format "?/?" displays decimal values formatted in the range 1/2 to n/9 > format "??/??" displays decimal values formatted in the range 1/2 to n/99 > format "???/???" displays decimal values formatted in the range 1/2 to n/999 etc > In excel then the value 0.6152 displays as 3/5, 8/13 and 510/829 respectively for the above 3 formats. > I'm attaching a patch for the Fraction class which adds a new constructor where the maximum number of digits can be specified, rather than the epsilon value. -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online. |
Free forum by Nabble | Edit this page |