Seemingly un-researchable topics on the internet

OT: anything goes!

Moderator: Edi

User avatar
Dooey Jo
Sith Devotee
Posts: 3127
Joined: 2002-08-09 01:09pm
Location: The land beyond the forest; Sweden.
Contact:

Re: Seemingly un-researchable topics on the internet

Post by Dooey Jo »

Steel wrote:I cannot concieve of how, given an int to string function, it could take more than a hundred lines to make a float to string function.

I mean all you have to do is chop the float up into chunks, convert chunk to int and then to int to string.

eg pseudocode for a float thats f=123.456

int a=f;
String s1=intToString(a);
float b=f-a;
String s2=intToString(1000*b);
String result = s1+'.'+s2;
Return result;

All you have to do is to process the float into chunks and then cast those chunks to ints which cannot possibly take more than 100 lines.
They are using many special cases, and lots of awkward integer conversions and operations (to avoid any floating point operations, I'm sure). I made several versions (none longer that 50 lines); one with no floating point math, that used a table of integers to calculate the decimal value of the number, sort of like a fixed-point number, and some more integer math to convert the result to the right power of ten (the floating point exponent is in base-2) without losing accuracy. Another also used the table to get the decimal part, but used one floating point operation for the conversion stuff, and another one that used floating point math and a lookup function for integer base-10 exponentiations. The last one turned out to be the fastest (up to six times faster than printf, yay), and the most simple...

It works like this:
1. Switch off the "sign bit" (but remember it when printing later)
2. Get the integer log10 of the floating point number
3. Use result to scale the number so it fits nicely in an int of appropriate size
4. Print integer, insert a dot where appropriate, or use scientific notation if it's way too large (use the log10 result for this)
5. Profit
Image
"Nippon ichi, bitches! Boing-boing."
Mai smote the demonic fires of heck...

Faker Ninjas invented ninjitsu
User avatar
Steel
Jedi Master
Posts: 1132
Joined: 2005-12-09 03:49pm
Location: Cambridge

Re: Seemingly un-researchable topics on the internet

Post by Steel »

Starglider wrote:
Steel wrote:eg pseudocode for a float thats f=123.456

int a=f;
a will be zero for any float smaller than zero or greater than the max size of an int (e.g. (2^31)-1), so this will not work. You can do a FP division and remainder instead to get each possible decimal digit, but this will be horribly, horribly slow (since you have to cover the full exponential range and need at least one FP divide per digit) and will require additional code to check for strings of zeros if you want to output scientific notation rather than a literal. Efficient code operates directly on the FP representation (sign/exponent/mantissa) instead.
I'm well aware of those facts, hence I said you just need to chop it up right, obviously doing the sequence of digits for f via floor(f*b^n)%b is going to be fairly slow, but theres working and working fast. I'm a mathematician rather than a compsci, so a solution that arrives at the answer before (or around) the end of the universe is a good solution.

Also, we do rather want a to be zero if f is less than zero dont we? :P
Apparently nobody can see you without a signature.
User avatar
Ariphaos
Jedi Council Member
Posts: 1739
Joined: 2005-10-21 02:48am
Location: Twin Cities, MN, USA
Contact:

Re: Seemingly un-researchable topics on the internet

Post by Ariphaos »

Steel wrote: I cannot concieve of how, given an int to string function, it could take more than a hundred lines to make a float to string function.

I mean all you have to do is chop the float up into chunks, convert chunk to int and then to int to string.

eg pseudocode for a float thats f=123.456

int a=f;
String s1=intToString(a);
float b=f-a;
String s2=intToString(1000*b);
String result = s1+'.'+s2;
Return result;

All you have to do is to process the float into chunks and then cast those chunks to ints which cannot possibly take more than 100 lines.
I would like to submit this as a textbook example of why we need to keep teaching assembly languages. Above and beyond the fact that your function fails for certain special cases (representing not a number, etc. results), the goal is to make a direct conversion. You begin with 32, 64, 80, 128, etc. bits, in the form of a sign, an exponent, and a mantissa.

The sign bit is of course easy. Now try placing the decimal point.
Give fire to a man, and he will be warm for a day.
Set him on fire, and he will be warm for life.
User avatar
GuppyShark
Sith Devotee
Posts: 2830
Joined: 2005-03-13 06:52am
Location: South Australia

Re: Seemingly un-researchable topics on the internet

Post by GuppyShark »

hongi wrote:I tried to research Australian Aboriginal religions and it's really hard. You find stuff about the 'Dreamtime' and the Rainbow Serpent which you learn in shallow detail at school, but eventually you realise that most people don't know anything beyond that. I think it may have to do with the fact that the religious or spiritual practices of indigenous Australians simply weren't documented by Europeans. Maybe if there had been more British ethnographers (or if they hadn't converted the Aboriginals in the first place :roll:).
Probably because they don't want outsiders having access to the knowledge.
Post Reply