Live data from Hacker News

Ryū: Fast Float-To-String Conversion

pldi18.sigplan.org

11–20 of 45 posts

Re: Ryū: Fast Float-To-String Conversion

#12
4:27 > "Every binary floating point number has an exact decimal equivalent, but not every decimal number has an exact binary equivalent. And the reason for that, you know, is that powers of two and powers of ten are only compatible in one direction, not in the other."

This is because 1/2 is representable as a fraction with 10 in the denominator: 5/10. All fractional digits after the binary point are just powers of 1/2, thus powers of 5/10. Division by ten in decimal doesn't produce any repeating digits.

Here is where the above is slightly misleading, though. The number of decimal digits to capture that exact decimal value of a binary floating point with an n-bit mantissa may be far in excess of the actual decimal precision that is contained in an n-bit mantissa.

In the case of the IEEE 64 bit double we need 17 decimal digits to capture a printed decimal representation which will reproduce the original double. That representation isn't the exact decimal number; much more than 17 digits may be required to get the exact decimal value. The exact decimal value is, I think, rarely of interest. In an ordinary application of floating point numbers, we don't print double values to, say, 30 digits of precision; anything after 17 is "junk".

In the other direction, only 15 decimal digits of precision are guaranteed to be preserved by the representation, so in a 17 digit print, digits 16 and 17 are also "dodgy"; they serve only to record the object exactly.

Re: Ryū: Fast Float-To-String Conversion

#13

4:27 > "Every binary floating point number has an exact decimal equivalent, but not every decimal number has an exact binary equivalent. And the reason for that, you know, is that powers of two and powers of ten are only compatible in one direction, not in the other." This is because 1/2 is representable as a fraction with 10 in the denominator: 5/10. All fractional digits after the binary point are just powers of 1/…

You're right, but I don't think it qualifies as misleading since the whole algorithm is exactly about finding the minimal expansion for a specific number.

The author even goes through an explicit example.

Re: Ryū: Fast Float-To-String Conversion

#14
post #5
post #4

Other than printing out the numbers, what are the practical applications?

Javascript was mentioned in the talk; because of its weak typing and plenty of APIs that take strings, floating point printing is pretty common.

Often, numbers in JS happen to be just integers. Would the implementation optimize for that case?

Re: Ryū: Fast Float-To-String Conversion

#15
post #4

Other than printing out the numbers, what are the practical applications?

Real-time displays often need to show floating point numbers as they change. As a simple example, the time remaining display of a music player ticks down as the music plays. Now in this case, it is easy to fake it by using integers and then placing the decimal point, but if you were controlling something complex in real time you might not be able to do this as easily.

Re: Ryū: Fast Float-To-String Conversion

#16
post #14
post #5

Earlier quoted context omitted.

Javascript was mentioned in the talk; because of its weak typing and plenty of APIs that take strings, floating point printing is pretty common.

Often, numbers in JS happen to be just integers. Would the implementation optimize for that case?

Javascript JITs specialize for integers already. I don't know if Ryū does specifically though.

Re: Ryū: Fast Float-To-String Conversion

#17
post #8
post #5

Earlier quoted context omitted.

Javascript was mentioned in the talk; because of its weak typing and plenty of APIs that take strings, floating point printing is pretty common.

That's really sad.

Why? Did you forget to read the implied "just like any other language that's being used in a graphical context, where live numbers need to be shown"?

Re: Ryū: Fast Float-To-String Conversion

#18
This is in a track called "PLDI Research Papers - Floats and Maps". Is that because they had one paper on maps and two on floats, and decided to put them together, or is there some deep connection between the two?

Re: Ryū: Fast Float-To-String Conversion

#20
post #4

Other than printing out the numbers, what are the practical applications?

Real-time displays often need to show floating point numbers as they change. As a simple example, the time remaining display of a music player ticks down as the music plays. Now in this case, it is easy to fake it by using integers and then placing the decimal point, but if you were controlling something complex in real time you might not be able to do this as easily.

> it is easy to fake it by using integers and then placing the decimal point

That's not "faking it", that using fixed point numbers [1]. They are a great alternative if the numbers stay in roughly the same magnitude, and are much saner when dealing with e.g. money.

1: https://en.wikipedia.org/wiki/Fixed-point_arithmetic

Post reply on HN