[
https://issues.apache.org/jira/browse/NUMBERS-128?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17093176#comment-17093176
]
Alan Wang commented on NUMBERS-128:
-----------------------------------
hi [~Schamschi], I think the same problem exists in Franciton.doubleValue(),
abs((2^60 - 1 + 2^6)/(2^60 - 1) - (1 + 2^-53)) < abs((2^60 - 1 + 2^6)/(2^60 -
1) - 1) is also true.
But maybe the problem doesn't lie in the code, but caused by the float number
itself.
When doing floating-point arithmetic, if the relative error of two numbers
exceeds FLT_EPSILON(1.192093e-07), the computer will ignore the smaller number.
{code:java}
// code placeholder
float res = 1.0f;
for (int i = 0; i < 10; i++) {
res += (float) Math.pow(2, -24);
}
System.out.println(res); // 1.0
res = 1.0f;
res += (float) Math.pow(2, -24) * 10;
System.out.println(res); // 1.0000006
{code}
If we want to get the exact floating-point arithmetic result, the best way is
to use BigDecimal, since BigFraction.bigDecimalValue() could solve this issue,
does this problem still need to be resolved?
> Precision of Fraction.floatValue() can be improved
> --------------------------------------------------
>
> Key: NUMBERS-128
> URL: https://issues.apache.org/jira/browse/NUMBERS-128
> Project: Commons Numbers
> Issue Type: Improvement
> Components: fraction
> Affects Versions: 1.0
> Reporter: Heinrich Bohne
> Priority: Minor
>
> The current implementation of {{Fraction.floatValue()}} first calls
> {{doubleValue()}}, which converts the fraction to a {{double}} with maximum
> possible precision, and then rounds the returned {{double}} value to a
> {{float}}. For the fraction 1 + 2^6^ / (2^30^ - 1) = (2^30^ - 1 + 2^6^) /
> (2^30^ - 1), this yields a result of exactly 1. However, the [actual value
> of the
> fraction|https://www.wolframalpha.com/input/?i=(2%5E30+-+1+%2B+2%5E6)%2F(2%5E30+-+1)+to+base+2]
> is closer to 1 + 2^-23^ (which is representable exactly as a {{float}})
> than it is to 1, as WolframAlpha
> [confirms|https://www.wolframalpha.com/input/?i=abs((2%5E30+-+1+%2B+2%5E6)%2F(2%5E30+-+1)+-+(1+%2B+2%5E-23))+%3C+abs((2%5E30+-+1+%2B+2%5E6)%2F(2%5E30+-+1)+-+1)].
--
This message was sent by Atlassian Jira
(v8.3.4#803005)