Talk:Float2Hex

From Second Life Wiki
Jump to: navigation, search

Float2String w/o Rounding

Greetings. I'm currently working on Json-related workarounds to combat the "enhanced features" LSL string handling offers. I've worked out how to encode floats, vectors and rotations w/o the space after comma and how to trim the extra padded zeros (to condense the resulting Json text). I can also detect when "stringifying" has truncated a float value, which is undesirable when one requires a greater precision within the Json text.

So, my question is, which approach would be best to use to get from (say) PI to "3.1415926535897932384626433832795f"? An exponential form may be suitable for this, but I'm aware of your warning of using hex notation in vectors and rotations and am unsure of the usability of that. And, knowing that a precise mapping isn't possible, having an idea of the imprecision involved in the algorithm would be a plus.

Thank you for any consideration you might give to this.

-- LepreKhaun Resident 22:38, 20 September 2013 (PDT)

The comment in the code about hex notation for vectors is a justification for just using the integer values instead of using hex notation for integer values. In an early version, I trimmed off the "p##" portion to save on the characters only to discover that for vectors and rotations it required the "p##". Hex integers work for floats but not in vectors or rotations. So...

<lsl> float a = (float)"0xa"; //OK vector b = (vector)"<0x1,0,0>"; //BROKEN </lsl>

But then I realized that it didn't matter because for the vast majority of integer values the integer encoding was smaller than the hex encoding, be it integer or float. Yes it would be possible to catch those cases where hex float encoding would be superior to integer encoding but it would bloat the code and make it slower.
But this is irrelevant, JSON does not support hex-floats. You want Float2Sci, which is dreadfully slow. I should really try to find a better way of doing it (without a loop). I wonder if optical error correction would be faster? If you don't care about having to do some processing to read your data, I might suggest Base64-Float. -- Strife (talk|contribs) 23:33, 21 September 2013 (PDT)
I was afraid that Float2Sci would be what I needed, sigh. I'll try to modify it to take an integer of desired precision, where it won't necessarily return 47 decimal places if only 10 are required by the user and that may speed execution. But this utility could not be used in time critical apps in any sense since Json text manipulations in LSL are *very* slow to begin with!
I'll also go into the literature for float to string conversion algorithms and see if, perhaps, a better one could be found. I've found most C implementations are usually portable to LSL, it's just a matter of finding one that is documented well. If worse comes to worse, I'll delve into the sprintf() library. lol
Thank you for your help! --LepreKhaun Resident 17:50, 22 September 2013 (PDT)
Once upon a time I did dive into the various libraries to see how they worked, because I didn't want to have to write my own implementation, I ended up writing my own. I didn't find one that maintain the precision.-- Strife (talk|contribs) 21:17, 22 September 2013 (PDT)
I'd be interested to know if llJson2List is susceptible to an analog of the Mono denormalized range (float) bug. -- Strife (talk|contribs) 23:55, 21 September 2013 (PDT)

<lsl> llOwnerSay("Typecast: "+llList2String(["Bad", "Good"], 0.0 != (float)"1.5e-45")); llOwnerSay("llJson2List: "+llList2String(["Bad", "Good"], 0.0 != llList2Float(llJson2List("1.5e-45"), 0))); </lsl>

Both return "Good", so that shouldn't be a problem. (took the liberty of adding the missing parenthesis to your code). -- LepreKhaun Resident 17:50, 22 September 2013 (PDT)
Thanks for the fix. I just realized I used the wrong value. Random link [[1]]. -- Strife (talk|contribs) 21:17, 22 September 2013 (PDT)

Ok, well, let's see.

So, you sent me to this one because you said it will contain explanations.

Do you mean that by placing: <lsl>

How it works

</lsl> before a code-dump is a good explanation? I find that funny but nevermind me, you're the 25-year old, gangster of this wiki so let's see what happens:

Test 1, Funny

Input <lsl>

       llOwnerSay(Float2Hex(1.0));

</lsl>

Output

Object: 1
shouldn't that be
0x3f8
?

Test2, Funnier

Input <lsl>

       llOwnerSay(Float2Hex(2.0));

</lsl>

Output

Object: 2
shouldn't that be
0x4

Test3, It's Getting Lame

Input <lsl>

       llOwnerSay(Float2Hex(10.0));

</lsl>

Output

Object: 10
shouldn't that be
0x412
?

I think I have a more efficient version of Float2Hex for you, try this:

<lsl> string Float2Hex(float input) {

 return (string)((integer)input);

} </lsl>

So, enlighten me a bit. This one does not work, the other one does not work... What is this?

Let's sum it up a bit, shall we? I said I wanted to be left alone. In fact, I think that your stuff is great and you along with your furry chums that you brought to vote on all Kira's petty content removal request should take over the wiki and run it yourselves. However, I am here to indulge your wishes so let's talk a bit about shame (because that's what you dumped on my talk page) and spell out the highlights.

Just try to see if this reasoning appeals to you:

  • Don't you find it shameful that you bother about other peoples' content when, in fact, your own scripts do not work properly?
  • Don't you find it shameful that you falter the high flag of logic and mathematics, yet none of your own scripts are properly documented so that we can see what they do?
  • Don't you find it shameful that you rally a band of thugs to create Jira pages about my content and make them vote to fight your wars? (see link above) I know that you are a furry, but at least show some sign of integrity since you wave that around as well in many of your political rants.
  • You roll and alt Kokoro Fasching that reverses my content AFTER Linden has told me to mark it for deletion?
  • ... that is not enough for you, so your mob edits the page deletion request so that it points to Void's delusional comments on my wiki. Who the hell is she, I never met her, lol? I tried one of her scripts as well and that failed just as bad... Is she your girlfriend?
  • In your 25-year programming experience you teach others that they should not use jumps, although you don't even cite the 1986 article on that as well as sprouting a lot of pop-culture knowledge about them?
  • ... since that's not enough, you then invade my talk page that my script is broken because it uses jumps. Then, I waste my time on you and it turns out you were completely wrong. Then, you go back to sulking and come back with a half-right claim that turns out to be wrong as well...
  • [Optional, because it can be seen as ridiculous, aside from shameful] You mention on that Jira page that somebody should start "backing up" my scripts? =))
  • You mention you are programming for 25-years, yet you have no clue what determinism is, your scripts are broken, you hide the "logic" that you so praise behind a wall of incomprehensible magic numbers and bitshifts?

This is not even shameful Strife, this is just L-O-U-S-Y. Are you Japanese? I thought you guys value honor and stuff, but this is just beyond the thin line of decency, let alone honor...

You are so small and insignificant when picked apart... Yet you do not have the composure and the integrity to mind about your own business, sniffing into other people's affairs like a pig, when most of your stuff is broken, crashes or plain wrong.

Great job, great job indeed. Now I really know I don't want to contribute to this wiki...

Kira Komarov

Response

Unfortunately you are confused as to what the purpose of this function is. I'm going to assume this is due to the fact that the link for C99 Hex Floats is dead. You could have googled it. It's what I did to update it. Not a great article

Points of confusion:

  1. What hex floats are. Try http://www.exploringbinary.com/hexadecimal-floating-point-constants/
  2. The contract this function provides.

The Contract

Given a float that is a real number, it will convert that float to a string that when typecast back to a float will hold the same value as the original float. It should be faster than Float2Sci. If it's not a real number the behavior is undefined. <lsl>number == (float)Float2Hex(number)</lsl>


I have no idea why you think 1 should be encoded as 0x3f8 or that 2 should be 0x4. Maybe you are thinking this does some zero cutting binary representation? Maybe you are confusing "%a" with "%p"? How does your proposed encoding method satisfy the contract? Regardless you should try some values that are not (valid as) integers to see what this function does.

For values in the integer range that are integers it does not bother to encode them in hexadecimal notation. This is actually a response to a bug in how LSL parsed floats in rotations and vectors (hence the comment on 3rd line of the function). Basically, you could not use hexadecimal integers in rotations or vectors (vector)"<0x1, 0x2, 0x3>", it would return ZERO_VECTOR, very vexing. Part of the purpose of this function is to make the result be compact, not wanting to use needless letters I did not want to force a zero exponent on everyone just to get around this edge case (vector)"<0x1p0, 0x2p0, 0x3p0>". Again I don't know if they fixed this. Sure some integers could be more tightly encoded as hexfloats but it's not worth the added complexity.

Attacking someones character is a tricky thing. You have to get your facts straight or it's libel. Unfortunately, you have based your character assassination on the premise that you understand what the code is supposed to do and that it does not work. To the contrary you have only demonstrated your own ignorance, leaving you with libelous comments without the defense that they are truth. In a court of law this is cut and dry. You have defamed my character. Regardless of the legalities, attacking someones character is considered a breach of the SL Community Standards and the Terms of Service (section 8.2.v). To that end I have no qualms about reporting this to LL. Good day. -- Strife (talk|contribs) 11:10, 25 May 2012 (PDT)

Float2Binary?

I'm actually trying to solve a similar problem in another language and I remembered this function, but I'm wondering; how easy would it be to adapt this to produce binary IEEE output? i.e - 1-bit for the sign, 8-bits for the exponent and 23-bits for the mantissa?

I've been hacking away at the problem, and it seems like it should be easy since the mantissa from your function is almost in the correct format already, and the exponent is easy to encode (add 127 and clamp into the byte range), but I have a feeling I'm missing something that's causing me to go wildly wrong somewhere! --
-- Haravikk (talk|contribs) 05:10, 13 April 2013 (PDT)

You want my other function FUI. -- Strife (talk|contribs) 22:03, 17 April 2013 (PDT)

count: -1 vs ERR_GENERIC

The implied type of "count", eg what "count" represents is not error, so using ERR_GENERIC for it's default state is not valid. The reason it starts at -1 instead of zero has everything to do with optimization. Using ERR_GENERIC in this case confuses and obfuscates the meaning of the code. tl:dr; -1 is the value I'm using. -- Strife (talk|contribs) 14:01, 8 October 2013 (PDT)

Yeah, maybe that wasn't a good idea. -- Kireji Haiku 23:15, 8 October 2013 (PDT)


crash

        float a = iuf(1 << 21);
        string s=Float2Hex( a);

makes crash the script at line ( and with input of iuf every values inferior to 2 097 152 )

unsigned /= (float)("0x1p"+(string)(exponent -= ((exponent >> 31) | 1)));

exponent in input of this line is valued to -128. It creates a division by 0

Miranda Umino (talk) 17:40, 13 September 2015 (PDT)

I'll work on fixing this. -- Strife (talk|contribs) 13:32, 14 September 2015 (PDT)