Comments (7)
from ryu.
The initial report is a known issue (#8).
I didn't double-check all the values you provided, but here's RyuFloat debugging output for the first value 0.33007812f (you can edit RyuFloat.java line 86 and then run it as a main class to get this output):
IN=111110101010010000000000000000
S=+ E=-25 M=11075584
Exact values
m =44302336
E =-1
d+=330078139901161193847656250
d =330078125000000000000000000
d-=330078110098838806152343750
e2=-27
44302336 * 5^27 / 10^18 = 44302336 * 5^9 / 2^18
Actual values
d+=330078139
d =330078125
d-=330078110
last removed=0
e10=-9
d+10=false
d-10=false
Actual values after loop
d+=33007813
d =33007812
d-=33007811
last removed=5
e10=-9
d+10=false
d-10=false
output=33007813
output_length=8
output_exponent=-1
0.33007813 0.33007812
This is a bit much, but these are the most important lines:
d+=330078139901161193847656250
d =330078125000000000000000000
d-=330078110098838806152343750
I'm using BigInteger to compute the exact decimal representation of the number itself, as well as the lower and upper bounds. For a complete explanation, I'll have to refer to the paper, but - briefly - the lower and upper bounds give the range within which the output must be in order to be round-trip safe, i.e., if you want the original number back, you must output a number that's between the bounds.
Here, the bounds are:
0.330078110098838806152343750 <= x <= 0.330078139901161193847656250
Both Ryu's output and the OpenJDK output numbers that are between the bounds. This is easy to see if you write them in the same column:
upper: 0.330078139901161193847656250
Ryu: 0.33007813
OpenJDK: 0.33007812
lower: 0.330078110098838806152343750
In fact, these are the only shortest decimal floating point numbers in this interval. But since we have a choice, which one should we take? We now look at the exact number, which is:
upper: 0.330078139901161193847656250
Ryu: 0.33007813
exact: 0.330078125000000000000000000
OpenJDK: 0.33007812
lower: 0.330078110098838806152343750
As you can see, the exact number has a 5 next, followed by zeroes. In this case, the Ryu's specification says that we need to round to even, so it should round down and output 0.33007812. It's not doing that right now.
from ryu.
As for printf, it doesn't use the same specification as Ryu - Ryu always outputs the shortest possible value that correctly converts back to the original number, and it should round-even in case of a tie.
from ryu.
I pushed a fix for 32-bit floats. Please follow #8 if you're interested in a complete resolution.
from ryu.
Shouldn't it be fixed for 64-bit doubles too?
Also, the following branch cannot be reached because the previous branch handles all cases when q <= 1
:
https://github.com/ulfjack/ryu/blob/master/ryu/d2s.c#L330
from ryu.
Yes, it definitely needs to be fixed for doubles as well.
I don't understand your second statement, I'm afraid. Maybe the code has changed?
from ryu.
Never-mind about my 2nd statement, I have misunderstood those C statements.
Comparing with C version I see that the following statement possible was missed in Java: dvIsTrailingZeros &= lastRemovedDigit == 0
- for both rounding loops just before updating of the last removed digit.
from ryu.
Related Issues (20)
- Instructions for porting to C# HOT 3
- A DecimalFloatingPoint struct in the public side of the API HOT 1
- Buffer overflow possible with d2fixed() and friends
- Julia implementation HOT 1
- Not getting the shortest decimal representation of 0.3 HOT 9
- Outdated Documentation HOT 1
- Questions on Ryu algorithm
- Remove the "static inline" declarator HOT 3
- Add a prefix to exported names HOT 6
- C implementation that accommodates 10-byte and 16-byte floating point data types HOT 7
- question: is there a standard for 'shortest'? HOT 1
- question about ryu support in gcc? ( pure-'C' ) HOT 1
- Honest output request HOT 2
- optimized ryu version for 'long doubles'? ( real 80-bit figures, not 'fake-long' ) - enhancement proposal -
- Wondering if anyone has a generic_decimal_to_binary implementation? HOT 2
- generic_128's long_double_to_fd128 is silently incorrect on most platforms. long double users beware
- travis ci never completes its checks HOT 3
- Clarification on lower bound of `compute_shortest`. HOT 4
- Ryu: Question about Step 0 in section 3.4 of the PLDI paper
- Specify STATIC in add_library
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 ryu.