Floating point format conversion

classic Classic list List threaded Threaded
7 messages Options
Tom Freund-2 Tom Freund-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Floating point format conversion

Have a 4-byte hex value representing a 32-bit floating point number in IEEE floating point format that is coming into a eLua chunk.

Want to convert it back to a floating point value. Using tonumber does not yield the right value. Any suggestions ?

--
Tom Freund





_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
Tom Freund-2 Tom Freund-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Floating point format conversion

A little more explanation.

The hex value comes in as a string (i.e. "1a4e6a"). I simply prefixed it with "0x". I also tried using the base argument for tonumber (16) without using the "0x" prefix. Still, the resulting value is not correct.


On Tue, May 27, 2014 at 2:33 PM, Tom Freund <[hidden email]> wrote:
Have a 4-byte hex value representing a 32-bit floating point number in IEEE floating point format that is coming into a eLua chunk.

Want to convert it back to a floating point value. Using tonumber does not yield the right value. Any suggestions ?

--
Tom Freund







--
Tom Freund

dig.y.SoL

"Systems overseeing 
public and private
infrastructure"

LinkedIn - http://www.linkedin/in/digysol
Voice - 860-232-1614





_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
Martin Guy Martin Guy
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Floating point format conversion

On 27/05/2014, Tom Freund <[hidden email]> wrote:
> On Tue, May 27, 2014 at 2:33 PM, Tom Freund <[hidden email]> wrote:
>> Have a 4-byte hex value representing a 32-bit floating point number in
>> IEEE floating point format that is coming into a eLua chunk.
>>
>> Want to convert it back to a floating point value. Using tonumber does
>> not
>> yield the right value. Any suggestions ?

> The hex value comes in as a string (i.e. "1a4e6a"). I simply prefixed it
> with "0x". I also tried using the base argument for tonumber (16) without
> using the "0x" prefix. Still, the resulting value is not correct.

I suspect you may need a C function to do the type conversion, using a
union { float f; int i; }, storing your value in field i and reading
it back from field f. I suspect that would be less painful than
reconstructing the floating poinr value from the bitfields.

Unless there's a better way...

    M

   M
_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
Marcelo Politzer Marcelo Politzer
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Floating point format conversion

Maybe I missed the point but a string.format("%f", your_0x_number)
seems like it could work.

Best,
Marcelo

2014-05-27 16:44 GMT-03:00 Martin Guy <[hidden email]>:

> On 27/05/2014, Tom Freund <[hidden email]> wrote:
>> On Tue, May 27, 2014 at 2:33 PM, Tom Freund <[hidden email]> wrote:
>>> Have a 4-byte hex value representing a 32-bit floating point number in
>>> IEEE floating point format that is coming into a eLua chunk.
>>>
>>> Want to convert it back to a floating point value. Using tonumber does
>>> not
>>> yield the right value. Any suggestions ?
>
>> The hex value comes in as a string (i.e. "1a4e6a"). I simply prefixed it
>> with "0x". I also tried using the base argument for tonumber (16) without
>> using the "0x" prefix. Still, the resulting value is not correct.
>
> I suspect you may need a C function to do the type conversion, using a
> union { float f; int i; }, storing your value in field i and reading
> it back from field f. I suspect that would be less painful than
> reconstructing the floating poinr value from the bitfields.
>
> Unless there's a better way...
>
>     M
>
>    M
> _______________________________________________
> eLua-dev mailing list
> [hidden email]
> https://lists.berlios.de/mailman/listinfo/elua-dev
_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
jbsnyder jbsnyder
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Floating point format conversion

In reply to this post by Martin Guy


On Tue, May 27, 2014 at 2:44 PM, Martin Guy <[hidden email]> wrote:
On 27/05/2014, Tom Freund <[hidden email]> wrote:
> On Tue, May 27, 2014 at 2:33 PM, Tom Freund <[hidden email]> wrote:
>> Have a 4-byte hex value representing a 32-bit floating point number in
>> IEEE floating point format that is coming into a eLua chunk.
>>
>> Want to convert it back to a floating point value. Using tonumber does
>> not
>> yield the right value. Any suggestions ?

> The hex value comes in as a string (i.e. "1a4e6a"). I simply prefixed it
> with "0x". I also tried using the base argument for tonumber (16) without
> using the "0x" prefix. Still, the resulting value is not correct.

I suspect you may need a C function to do the type conversion, using a
union { float f; int i; }, storing your value in field i and reading
it back from field f. I suspect that would be less painful than
reconstructing the floating poinr value from the bitfields.

Unless there's a better way...

I was thinking one could also write a function to repack the hexadecimal (base 16) into a binary string maybe using something like this:

and then use the pack/unpack functions:

Then depending on byte ordering:
print(pack.unpack(("001a4e6a"):fromhex(), '>f'))

5 2.4158525654806e-39


print(pack.unpack(("001a4e6a"):fromhex(), '<f'))

5 6.2290375092292e+25

 

    M

   M
_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev



--
James Snyder
ph: (847) 448-0386

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
Tom Freund-2 Tom Freund-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Floating point format conversion

Found an alternative way of doing this.

Thank you for all your suggestions.


On Tue, May 27, 2014 at 4:14 PM, James Snyder <[hidden email]> wrote:


On Tue, May 27, 2014 at 2:44 PM, Martin Guy <[hidden email]> wrote:
On 27/05/2014, Tom Freund <[hidden email]> wrote:
> On Tue, May 27, 2014 at 2:33 PM, Tom Freund <[hidden email]> wrote:
>> Have a 4-byte hex value representing a 32-bit floating point number in
>> IEEE floating point format that is coming into a eLua chunk.
>>
>> Want to convert it back to a floating point value. Using tonumber does
>> not
>> yield the right value. Any suggestions ?

> The hex value comes in as a string (i.e. "1a4e6a"). I simply prefixed it
> with "0x". I also tried using the base argument for tonumber (16) without
> using the "0x" prefix. Still, the resulting value is not correct.

I suspect you may need a C function to do the type conversion, using a
union { float f; int i; }, storing your value in field i and reading
it back from field f. I suspect that would be less painful than
reconstructing the floating poinr value from the bitfields.

Unless there's a better way...

I was thinking one could also write a function to repack the hexadecimal (base 16) into a binary string maybe using something like this:

and then use the pack/unpack functions:

Then depending on byte ordering:
print(pack.unpack(("001a4e6a"):fromhex(), '>f'))

5 2.4158525654806e-39


print(pack.unpack(("001a4e6a"):fromhex(), '<f'))

5 6.2290375092292e+25

 

    M

   M
_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev



--
James Snyder
ph: <a href="tel:%28847%29%20448-0386" value="+18474480386" target="_blank">(847) 448-0386

_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev



--
Tom Freund

dig.y.SoL

"Systems overseeing 
public and private
infrastructure"

LinkedIn - http://www.linkedin/in/digysol
Voice - 860-232-1614





_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
Martin Guy Martin Guy
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Floating point format conversion

On 28/05/2014, Tom Freund <[hidden email]> wrote:
> Found an alternative way of doing this.

Of the suggestions to the specific problem you asked, James wins the
shortest-and-most-elegant prize... what was your solution in the end?
Avoiding having to do the conversion?

Cheers

  M
_______________________________________________
eLua-dev mailing list
[hidden email]
https://lists.berlios.de/mailman/listinfo/elua-dev
Loading...