Anyone know some algorithm to convert float to a string?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
vinicius9107
Posts: 8
Joined: Sun Sep 08, 2013 5:11 pm

Anyone know some algorithm to convert float to a string?

Post 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?
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

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

Post by Love4Boobies »

This is a good, simple, introduction to floating-point representation that focuses on prevalent standards.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

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

Post by BMW »

This should really be in the General Programming section.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
HugeCode
Member
Member
Posts: 112
Joined: Mon Dec 17, 2012 9:12 am

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

Post by HugeCode »

This question was helpful for me when I was working with floats.
rdos
Member
Member
Posts: 3306
Joined: Wed Oct 01, 2008 1:55 pm

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

Post by rdos »

Conversion from float (long double) to string in x86 assembler: http://rdos.net/vc/viewvc.cgi/rdos/trun ... iew=markup
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

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

Post 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.
Every universe of discourse has its logical structure --- S. K. Langer.
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

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

Post 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.
Every universe of discourse has its logical structure --- S. K. Langer.
rdos
Member
Member
Posts: 3306
Joined: Wed Oct 01, 2008 1:55 pm

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

Post 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.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

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

Post 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
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply