Other than printing out the numbers, what are the practical applications?
Ryū: Fast Float-To-String Conversion
11–20 of 45 posts
Re: Ryū: Fast Float-To-String Conversion
#12This 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
#134: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/…
The author even goes through an explicit example.
Re: Ryū: Fast Float-To-String Conversion
#14Other 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.
Re: Ryū: Fast Float-To-String Conversion
#15Other than printing out the numbers, what are the practical applications?
Re: Ryū: Fast Float-To-String Conversion
#16Earlier 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?
Re: Ryū: Fast Float-To-String Conversion
#17Earlier 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.
Re: Ryū: Fast Float-To-String Conversion
#18Re: Ryū: Fast Float-To-String Conversion
#19Other than printing out the numbers, what are the practical applications?
Re: Ryū: Fast Float-To-String Conversion
#20Other 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.
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.