Anyone know some algorithm to convert float to a string?
-
- Posts: 8
- Joined: Sun Sep 08, 2013 5:11 pm
Anyone know some algorithm to convert float to a string?
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?
Or at least some document that explains how to convert a float to string?
-
- Member
- Posts: 283
- Joined: Mon Jan 03, 2011 6:58 pm
- Love4Boobies
- 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?
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 ]
[ Project UDI ]
Re: Anyone know some algorithm to convert float to a string?
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."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Re: Anyone know some algorithm to convert float to a string?
This question was helpful for me when I was working with floats.
Re: Anyone know some algorithm to convert float to a string?
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?
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: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?
"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.
Re: Anyone know some algorithm to convert float to a string?
I'm writing in Swedish so rdos knows his English is better than my Swedish - it might make the following observations easier to take.rdos wrote:Conversion from float (long double) to string in x86 assembler: http://rdos.net/vc/viewvc.cgi/rdos/trun ... iew=markup
Jag kollade snabbt genom din kod och såg föjlande:
Code: Select all
unsuported_txt DB 'UNSUPORTED'
Code: Select all
overflow_txt DB 'TO BIG NUMBER'
Mvh.
Every universe of discourse has its logical structure --- S. K. Langer.
Re: Anyone know some algorithm to convert float to a string?
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.bwat wrote:I'm writing in Swedish so rdos knows his English is better than my Swedish - it might make the following observations easier to take.rdos wrote:Conversion from float (long double) to string in x86 assembler: http://rdos.net/vc/viewvc.cgi/rdos/trun ... iew=markup
Jag kollade snabbt genom din kod och såg föjlande:Det heter 'UNSUPPORTED' på engelska, och,Code: Select all
unsuported_txt DB 'UNSUPORTED'
skall vara 'TOO BIG A NUMBER' för att vara korrekt.Code: Select all
overflow_txt DB 'TO BIG NUMBER'
Mvh.
So I just refer to my relatively poorer english of the first part of the 90s.
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.
- Love4Boobies
- 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?
A couple of developments since that publication:bwat wrote:"How to Print Floating-Point Numbers Accurately", Guy L. Steele Jr, and, Jon L White ACM SIGPLAN.
- 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 ]
[ Project UDI ]