Page 1 of 1

Anyone know some algorithm to convert float to a string?

Posted: Thu Sep 19, 2013 4:33 pm
by vinicius9107
Does anyone know any function algorithm to convert a float to string?
Or at least some document that explains how to convert a float to string?

Re: Anyone know some algorithm to convert float to a string?

Posted: Thu Sep 19, 2013 6:53 pm
by FallenAvatar

Re: Anyone know some algorithm to convert float to a string?

Posted: Thu Sep 19, 2013 7:13 pm
by Love4Boobies
This is a good, simple, introduction to floating-point representation that focuses on prevalent standards.

Re: Anyone know some algorithm to convert float to a string?

Posted: Thu Sep 19, 2013 8:06 pm
by BMW
This should really be in the General Programming section.

Re: Anyone know some algorithm to convert float to a string?

Posted: Thu Sep 19, 2013 10:32 pm
by HugeCode
This question was helpful for me when I was working with floats.

Re: Anyone know some algorithm to convert float to a string?

Posted: Fri Sep 20, 2013 12:18 am
by rdos
Conversion from float (long double) to string in x86 assembler: http://rdos.net/vc/viewvc.cgi/rdos/trun ... iew=markup

Re: Anyone know some algorithm to convert float to a string?

Posted: Fri Sep 20, 2013 7:42 am
by bwat
vinicius9107 wrote:Does anyone know any function algorithm to convert a float to string?
Or at least some document that explains how to convert a float to string?
The problem is printing a representation of the string that is accurate to half a ulp (unit in the last place) so that when you read that string representation in again you get the same bit pattern in memory that you used to generate the string. It's not easy. What I do with my programming languages, those that use arbitrary precision floating-point numbers, is described in this:

"How to Print Floating-Point Numbers Accurately", Guy L. Steele Jr, and, Jon L White ACM SIGPLAN.

I just checked my code and I implemented the Dragon4 algorithm in around 675 lines of heavily documented code. It could easily be done in half of that.

There is a newer algorithm from some French bloke that is called "Grisu" but if I recall correctly it wasn't much use to me for arbitrary precision floats. The older one was better suited.

Now, how are you reading floating-point numbers? Have you read:

"How to Read Floating-Point Numbers Accurately" by W. D. Clinger?

Send me a mail and I'll send you copies of these two. I believe they're free to download on the net.

Re: Anyone know some algorithm to convert float to a string?

Posted: Fri Sep 20, 2013 8:13 am
by bwat
rdos wrote:Conversion from float (long double) to string in x86 assembler: http://rdos.net/vc/viewvc.cgi/rdos/trun ... iew=markup
I'm writing in Swedish so rdos knows his English is better than my Swedish - it might make the following observations easier to take.

Jag kollade snabbt genom din kod och såg föjlande:

Code: Select all

unsuported_txt	DB 'UNSUPORTED'
Det heter 'UNSUPPORTED' på engelska, och,

Code: Select all

overflow_txt	DB 'TO BIG NUMBER'
skall vara 'TOO BIG A NUMBER' för att vara korrekt.

Mvh.

Re: Anyone know some algorithm to convert float to a string?

Posted: Sat Sep 21, 2013 9:31 am
by rdos
bwat wrote:
rdos wrote:Conversion from float (long double) to string in x86 assembler: http://rdos.net/vc/viewvc.cgi/rdos/trun ... iew=markup
I'm writing in Swedish so rdos knows his English is better than my Swedish - it might make the following observations easier to take.

Jag kollade snabbt genom din kod och såg föjlande:

Code: Select all

unsuported_txt	DB 'UNSUPORTED'
Det heter 'UNSUPPORTED' på engelska, och,

Code: Select all

overflow_txt	DB 'TO BIG NUMBER'
skall vara 'TOO BIG A NUMBER' för att vara korrekt.

Mvh.
Yeah, that is a piece of software written before 2000 when I first checked-in RDOS to CVS in August 2000. Looking at my old zip-files, it is essentially the same code as in version 2.3 of 1992 (which is the oldest snapshot I have). However, in that version all the descriptions are in swedish.

So I just refer to my relatively poorer english of the first part of the 90s. :mrgreen:

I also don't like how it is coded, but I can excuse that with it being one of the first x86-assembly programs I wrote.

Re: Anyone know some algorithm to convert float to a string?

Posted: Wed Sep 25, 2013 7:35 pm
by Love4Boobies
bwat wrote:"How to Print Floating-Point Numbers Accurately", Guy L. Steele Jr, and, Jon L White ACM SIGPLAN.
A couple of developments since that publication:
  • Correctly Rounded Binary-Decimal and Decimal-Binary Conversions by David M. Gay
  • Printing floating-point numbers quickly and accurately by Robert G. Burger and Kent R. Dybvig