Comments (4)
Ah sorry. I misunderstood the failure.
You are right.
The current implementations of toFloat()
(and actually toDouble()
are very naive.
The new implementation will probably go in this direction (still need to test some corner cases):
public float toFloat() {
return toBigDecimal().floatValue();
}
from big-math.
I am not sure I understand what you mean with "the method fails".
BigRational.toRationalString()
is a representation of the exact rational number without loss of accuracy.
From the javadoc:
Returns the string representation of this rational number in the form "numerator/denominator".
BigRational.toString()
is a reasonable "human" representation of the rational number but may have loss of accuracy.
The implementation uses the convenience BigRational.toBigDecimal()
which uses the highest accuracy predefined math context in Java MathContext.DECIMAL128
.
It would possibly make sense to reduce this accuracy but I would not go lower than the accuracy of Double
, which is MathContext.DECIMAL64
.
Your proposed solution is similar to BigRational.toString()
but using the very low accuracy of Float
(which is equivalent to MathContext.DECIMAL32
).
Maybe it makes sense to provide a BigRational.toString(MathContext)
method which uses the argument to specify the desired accuracy of the string representation.
from big-math.
To put this another way:
BigRational x = BigRational.valueOf("8.804462619980757911125181749462772084351");
System.out.println(x.toFloat());
System.out.println(Float.valueOf(x.toBigDecimal().toString()));
Results in the output:
NaN
8.804462
Whereas:
BigRational x = BigRational.valueOf("8.80446261998075791112518174946277208435");
System.out.println(x.toFloat());
System.out.println(Float.valueOf(x.toBigDecimal().toString()));
Results in the output:
Infinity
8.804462
Whereas:
BigRational x = BigRational.valueOf("8.8044626199807579111251817494627720843");
System.out.println(x.toFloat());
System.out.println(Float.valueOf(x.toBigDecimal().toString()));
Results in the output:
8.804462
8.804462
The behaviour of x.toFloat() created an issue for me, I would have rather the behaviour had been more like Float.valueOf(x.toBigDecimal().toString()).
from big-math.
Unit tests are looking good.
This is the new implementation including javadoc:
/**
* Returns this rational number as a float value.
*
* <p>If the rational number cannot be represented as float then one of the following results will be returned:</p>
* <ul>
* <li>> <code>Float.MAX_VALUE</code> returns {@link Float#POSITIVE_INFINITY}</li>
* <li>< <code>-Float.MAX_VALUE</code> returns {@link Float#NEGATIVE_INFINITY}</li>
* <li>< <code>Float.MIN_VALUE</code> returns <code>+0.0f</code></li>
* <li>> <code>-Float.MIN_VALUE</code> returns <code>-0.0f</code></li>
* </ul>
*
* @return the float value
*/
public float toFloat() {
return toBigDecimal().floatValue();
}
and these are the unit tests:
@Test
public void testToFloat() {
assertEquals(0.0f, valueOf(0).toFloat(), 0.0);
assertEquals(123.0f, valueOf(123.0).toFloat(), 0.0);
assertEquals(123.4f, valueOf(123.4).toFloat(), 0.0);
assertEquals(-123.0f, valueOf(-123.0).toFloat(), 0.0);
assertEquals(-123.4f, valueOf(-123.4).toFloat(), 0.0);
assertEquals(0.33333333f, valueOf(1, 3).toFloat(), 0.0);
assertEquals(0.6666667f, valueOf(2, 3).toFloat(), 0.0);
assertEquals(8.804462f, valueOf("8.804462619980757911125181749462772084351").toFloat(), 0.0);
assertEquals(8.804462f, valueOf("8.80446261998075791112518174946277208435").toFloat(), 0.0);
assertEquals(5E-21f, valueOf(BigDecimal.ONE, new BigDecimal("2E20")).toFloat(), 0.0);
assertEquals(0.0f, valueOf(BigDecimal.ONE, new BigDecimal("2E100")).toFloat(), 0.0); // underflow to +0.0f
assertEquals(0.0f, valueOf(BigDecimal.ONE, new BigDecimal("2E9999")).toFloat(), 0.0); // underflow to +0.0f
assertEquals(0, Float.compare(+0.0f, valueOf(BigDecimal.ONE, new BigDecimal("2E9999")).toFloat())); // underflow to +0.0f
assertEquals(0, Float.compare(-0.0f, valueOf(BigDecimal.ONE, new BigDecimal("-2E9999")).toFloat())); // underflow to -0.0f
assertEquals(2E20f, valueOf(new BigDecimal("2E20")).toFloat(), 0.0);
assertEquals(Float.POSITIVE_INFINITY, valueOf(new BigDecimal("2E100")).toFloat(), 0.0); // overflow to +infinity
assertEquals(-2E20f, valueOf(new BigDecimal("-2E20")).toFloat(), 0.0);
assertEquals(Float.NEGATIVE_INFINITY, valueOf(new BigDecimal("-2E100")).toFloat(), 0.0); // overflow to -infinity
}
from big-math.
Related Issues (20)
- Prepare release 2.2.1
- Support temporary local MathContext in DefaultBigDecimalMath
- Prepare release 2.3.0
- root(x, n) with large n is very slow
- Prepare release 2.3.1 HOT 2
- sqrt hangs HOT 1
- sqrt rounding HOT 2
- `BigRational.toIntegerRationalString()` with small negative numbers
- Issue with larger scales? HOT 3
- Questions about the big-math library: Capabilities and Future. HOT 12
- Something the matter with the 2.3.1 release? HOT 1
- Prepare release 2.3.2
- Question about big-math .jar file. HOT 1
- Performance issue in BigDecimalMath.pow() HOT 1
- BigComplexMath.sqrt fails on non-positive real numbers HOT 2
- Use BigDecimal.compareTo() insted of BigDecimal.equals()
- Pow throws Overflow in special case
- doubleValue performance
- BigComplexMath#sqrt fails when x is a negative real number
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from big-math.