Jump to content

Integer to float conversion


Hergonan
 Share

Recommended Posts

func _inttoflo($d)
    return (1 - (2*BitShift($d,31))) * (2^(bitAND(bitshift($d,23),255)-127)) * (1+BitAND($d,8388607)/8388608)
EndFunc

So I use this code to convert an integer to a float.

However sometimes it doesn't work...

it works for 1189765120 (returns 30000)

doesn't work for -1189765120 (returns 0.000428080558776855 instead of -0.0001426935)

doesn't work for 634 (returns 5.87791596572463e-039 instead of 8.884232E-43)

Can you tell me what's wrong? or give me a better method of getting the float from an integer?

is there a flo("hex") command? (like dec("hex"))

Link to comment
Share on other sites

I saw some clever float conversions in Auto3Library sources.

PaulIA used DllStructCreate for conversion.

Iům not at home now so I can't find it for you so look into sources yourself.

I would 'cheat' and save my brain. This is what I would do, though it might need a bit a change, but the basic idea is there.

Func IntToFlt($iVal)
Local $result
$fs = DllStructCreate("float")
$is = DllStructCreate("int64",DllStructGetPtr($fs))

DllStructSetData($is,1,$iVal)

$result =  DllStructGetData($fs,1)
$fs = 0
$is = 0
return $result

endfunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

From Auto3Library:

Func _Lib_IntToFloat($iInt)
  Local $tFloat, $tInt

  $tInt   = DllStructCreate("int")
  $tFloat = DllStructCreate("float", DllStructGetPtr($tInt))
  DllStructSetData($tInt, 1, $iInt)
  Return DllStructGetData($tFloat, 1)
EndFunc

It's the same as code from martin's post

Edited by Zedna
Link to comment
Share on other sites

From Auto3Library:

Func _Lib_IntToFloat($iInt)
  Local $tFloat, $tInt

  $tInt   = DllStructCreate("int")
  $tFloat = DllStructCreate("float", DllStructGetPtr($tInt))
  DllStructSetData($tInt, 1, $iInt)
  Return DllStructGetData($tFloat, 1)
EndFunc

It's the same as code from martin's post

That's a pity, I thought I had a new way to do it. :) . Ah well, at least it wasn't a bad idea. :) Also, mine cleans up the structs.
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...