Join us in Outworldz at www.outworldz.com:9000 or follow us:

Search dozens of selected web sites for OpenSim and LSL script

New! Script Meta-Search will search thousands of scripts here and at other sites for LSL or Opensim scripts.
Loading

Want to add a script or a project? Upload it and a half million people will see it and your name here this year.

Home   Show All
Category: Contributor: Creator
Math Floats_to_Hex  

Floats_to_Hex

Use to encode floats in hex notation, mi...

Category: Math
By : StrifeOnizuka
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

the Zip file

Download all files for Floats_to_Hex
Contents are in zip format, with .LSL (text) source code and LSLEdit (text + Solution) formats.
Get file # 1. Floats_to_Hex_1.lsl
Get file # 2. Floats_to_Hex_2.lsl
Get file # 3. Floats_to_Hex_3.lsl
Get file # 4. Floats_to_Hex_4.lsl
Get file # 5. Floats_to_Hex_5.lsl
1 string hexc="0123456789ABCDEF";//faster
2
3 string Float2Hex(float input)
4 {// Copyright Strife Onizuka, 2006, LGPL, http://www.gnu.org/copyleft/lesser.html
5 if((integer)input != input)//LL screwed up hex integers support in rotation & vector string typecasting
6 {//this also keeps zero from hanging the zero stripper.
7 float unsigned = llFabs(input);//logs don't work on negatives.
8 integer exponent = llFloor(llLog(unsigned) / 0.69314718055994530941723212145818);//floor(log2(b))
9 if(exponent > 127) exponent = 127;//catch fatal rounding error in exponent.
10 integer mantissa = (integer)((unsigned / (float)("0x1p"+(string)exponent)) * 0x1000000);//shift up into integer range
11 while(!(mantissa & 0x1))
12 {//strip extra zeros off before converting or they break "p"
13 mantissa = mantissa >> 1;
14 exponent = -~exponent;//++c;
15 }
16 string str = "p" + (string)(exponent - 24);
17 do
18 str = llGetSubString(hexc,15&mantissa,15&mantissa) + str;
19 while(mantissa = mantissa >> 4);
20 if(input < 0)
21 return "-0x" + str;
22 return "0x" + str;
23 }//integers pack well so anything that qualifies as an integer we dump as such, supports netative zero
24 return llDeleteSubString((string)input,-7,-1);//trim off the float portion, return an integer
25 }

Floats_to_Hex

Usage:

Category: Math
By : StrifeOnizuka
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

1 string a = Float2Hex(100.000000); // a == 100
2 string b = Float2Hex(14353.344727); // b == 0xE04561p-10
3 float c = (float)a;//c == 100.000000
4 float d = (float)b;//d == 14353.344727

Floats_to_Hex

Helper Functions:

Category: Math
By : StrifeOnizuka
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

1 string Rot2Hex(rotation a)
2 {
3 return "<"+Float2Hex(a.x)+","+Float2Hex(a.y)+","+Float2Hex(a.z)+","+Float2Hex(a.s)+">";
4 }
5
6 string Vec2Hex(vector a)
7 {
8 return "<"+Float2Hex(a.x)+","+Float2Hex(a.y)+","+Float2Hex(a.z)+">";
9 }
10
11 string DumpList2String(list input, string seperator)// for csv use ", " as the seperator.
12 {
13 integer b = (input != []);
14 string c;
15 string d;
16 integer e;
17 if(b)
18 {
19 @top;
20 if((e = llGetListEntryType(input,--b)) == TYPE_FLOAT)
21 d=Float2Hex(llList2Float(input,b));
23 d=Rot2Hex(llList2Rot(input,b));
24 else if(e == TYPE_VECTOR)
25 d=Vec2Hex(llList2Vector(input,b));
26 else
27 d=llList2String(input,b);
28 if(b)
29 {
30 c = d + (c=seperator) + c;
31 jump top;
32 }
33 }
34 return (c=d) + c;
35 }

Floats_to_Hex

How it works:

Category: Math
By : StrifeOnizuka
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

1 string hexc="0123456789ABCDEF";//faster
2
3 string Float2Hex(float a)
4 {// Copyright Strife Onizuka, 2006, LGPL, http://www.gnu.org/copyleft/lesser.html
5 //If it's zero, the zero stripper will hang, lets avoid that
6 if(a)
7 {//we need to find the exponent, but to do that we need to take the log of the number (or run a while loop)
8 float b = llFabs(a);//logs don't work on negatives.
9 string f = "p";//This is a trade off, slightly slower (only slightly) for a couple bytes savings (10)
10 //To get the exponent we need to round the integer portion down or the log2(b)
11 // LSL doesn't have log2, so we use one of the laws of logs to get it from the natural log
12 integer c = llFloor(llLog(b) / 0.69314718055994530941723212145818);//floor(log2(b))
13 //There is a rounding error in the exponent that causes it to round up, this usualy isn't a problem as the
14 // exponent only is only used to shift the number. It becomes a problem when dealing with float max as
15 // it will round up to 128; which will crash llPow.
16 if(c > 127) c = 127;//catch fatal rounding error in exponent.
17 //Q: why not do (b * llPow(2,-c))? A: because the range of floats are different at the top and bottom
18 // at the top it's 2^127 at the bottom it's 2^-149; and if you tried to do this otherwise,
19 // llPow would crash with an overflow. Then we multiply it by 2 ^ 24, floats only use 23 bits for the mantisa,
20 // we cannot add it into c because we could overflow llPow; we correct the value of c later.
21 // Instead of using llPow(2.0, c) we are using (float)("0x1p"+(string)c), it's 10% faster and 16 bytes lighter.
22 integer d = (integer)((b / (float)("0x1p"+(string)c)) * 0x1000000);//shift up into integer range
23 while(!(d & 0xf))
24 {//strip extra zeros off before converting or they break "p"
25 //a catch22 of sorts, you cannot know exactly how far to shift it till after you have shifted it
26 d = d >> 4;
27 c+=4; //for each zero stripped we must adjust the exponent
28 }
29 do//convert to hex
30 f = llGetSubString(hexc,15&d,15&d) + f;
31 while(d = d >> 4);
32 if(a < 0)//final formating & c adjustment (from making it an integer)
33 return "-0x" + f +(string)(c - 24);
34 return "0x" + f +(string)(c - 24);
35 }//no point in this getting special formating.
36 return "0";//zero would hang the zero stripper.
37 }

Floats_to_Hex

This version is perfect, it is slightly slower. It properly handles the exponent bug. It's included for curiosities sake (and a bunch of work went into it and i didn't want to throw it away).

Category: Math
By : StrifeOnizuka
Created: 2010-01-10 Edited: 2010-01-10
Worlds: Second Life

1 string Float2Hex(float a)
2 {// Copyright Strife Onizuka, 2006, LGPL, http://www.gnu.org/copyleft/lesser.html
3 if(a)
4 {
5 float b = llFabs(a);
6 integer c = llFloor(llLog(b) / 0.69314718055994530941723212145818);//floor(log2(b))
7 string f = "";
8 if(c > 127) c = 127;
9 else if(c < -126) c = -126;
10 else c -= ((float)("0x1p"+(string)c) > b);
11 integer d = ((integer)(b / (float)("0x.000002p"+(string)c)) & 0x7FFFFF) << 1;
12 integer m = 6;
13 while(!(d & 0xf))
14 {//strip extra zeros off before converting or they break "p"
15 d = d >> 4;
16 --m;
17 }
18 do
19 {
20 f = llGetSubString(hexc, 0xf & d, 0xf & d) + f;
21 d = d >> 4;
22 }while(--m);
23 if(a < 0)
24 return "-0x"+(string)(b > 1.1754943508222875079687365372222e-38)+"."+f +"p"+(string)(c);
25 return "0x"+(string)(b > 1.1754943508222875079687365372222e-38)+"."+f +"p"+(string)(c);
26 }
27 return "0";//zero would hang the zero stripper.
28 }

Back to the Best Free Tools in Second Life and OpenSim.