-
Notifications
You must be signed in to change notification settings - Fork 185
Ryu-based to_string function #627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
That's impressive! Makes me wonder what is the algorithmic bottleneck in Fortran compiler implementations. Does the benchmark print a single value or an entire array of values? Is there an explanation for the relatively "large" standard deviation of measurements? When I think of
Can you elaborate more on which types of format specifiers don't work with Ryu? |
@ivan-pi Thanks for your questions.
The benchmark is like this do i = 1, num_samples
int64_num = random_int64()
d = transfer(int64_num)
call cpu_time(t1)
do j = 1, num_iter
buffer = d2shortest(d)
end do
call cpu_time(t2)
delta1(i) = (t2 - t1) * 1000000 / num_iter ! convert to us
end do It should be noted that the former benchmark results were not correct. I have updated the results posted above. In addition, I have set a larger value for And I notice that if we comment out the re-allocation operation
|
I came up with the idea of implementing an effective function that converts floating point numbers to decimal strings without using internal IO when discussing the
disp
function. And recently I have tried to work it out.The original Ryu can generate the shortest precision-preserving string of a floating point number and Ryu printf provides formatting of floating point numbers.
Based on the C and Scala version of Ryu codes, I have implemented the Fortran version of Ryu.
I think we can replace the current implementation in stdlib with Ryu-based codes. So I would like to briefly describe the API of ryu_fortran.
Currently,
ryu_fortran
provides four routines:f2shortest
,d2shortest
,d2fixed
andd2exp
.Interface
f2shortest
andd2shortest
produce shortest precision-preserving decimal strings of floating point numbers, that is, if we convert strings back to floating point values, we should get same binary representation comparing to original numbers. These two routines are suitable for cases where format is not specified. Note:f2shortest
andd2shortest
always print at least two digits. For example, C version of Ryu produces "1" for1._real32
whilef2shortest
produces "1.0".d2fixed
andd2exp
do formatting forreal64
floating point numbers. The main difference between them and Fortran edit descriptors is that they don't produce "*****".With these routines, I wrote a simple prototype of
to_string
for floating point numbers in app/main.f90. For stdlib, I think whenformat
argument is not presented,f2shortest
andd2shortest
can be called. But whenformat
is specified, there might be a disagreement over whether we follow Fortran convention or not. The good points of Ryu formatting are fast and that it never produces "*****". But it can not control the width of formatted values, which is sometimes required.I hope in this issue we can discuss the above points and reach certain agreements.
P.S. Benchmark results (edited on 2022.2.9)
The text was updated successfully, but these errors were encountered: