Float+int math, c++ gcc

Discuss other programming languages besides AutoHotkey
User avatar
Spawnova
Posts: 339
Joined: 08 Jul 2015, 00:12
Contact:

Float+int math, c++ gcc

17 Jun 2017, 20:53

I'm trying to do int*float math, e.g. 5 * 0.5 //2.5 return as int, truncates to 2

However when I compile my c++ code and run it through ahk it returns nothing at all.

C++

Code: Select all

int function(int value) {
	float value2 = 0.5f;
	return static_cast<int>(value * value2); //runs into a problem here and returns nothing
}
AHK

Code: Select all

inputbox,value,Enter Value,Enter integer Value  ;simple input test
func := MCode("2,x86:g+wI20QkDNl8JAYPt0QkBtgNAAAAALQMZolEJATZbCQE2xwk2WwkBosEJIPECMOQAAAAPw==") ;compiled c++
msgbox % "Expected result of " value "/2 = " floor(value/2) "`ncompiled c++ returns:`n>" dllcall(func,int,value,"cdecl") "<"

MCode(mcode) //mcode function
{
  static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
  if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
    return
  if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
    return
  p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")
  if (c="x64")
    DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)
  if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))
    return p
  DllCall("GlobalFree", "ptr", p)
}
I'm not sure if I'm doing something wrong, or if it's a compiler issue, was hoping someone could help me out with this.
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Re: Float+int math, c++ gcc

13 Jul 2017, 13:28

Off-hand, my best guess would be that the float (0.5f) is being reduced to 0 when you cast it to an int. Anything * 0 = 0, so it would make sense that calling it wouldn't return anything.

On a separate note, what is the MCode? In the comment you said it's the compiled C++; does that mean it's some way to embed compiled C++ into AHK (cuz that would be awesome!)? :)

Return to “Other Programming Languages”

Who is online

Users browsing this forum: No registered users and 2 guests