Float+int math, c++ gcc

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :| :mrgreen: :geek: :ugeek: :arrow: :angel: :clap: :crazy: :eh: :lolno: :problem: :shh: :shifty: :sick: :silent: :think: :thumbup: :thumbdown: :salute: :wave: :wtf: :yawn: :facepalm: :bravo: :dance: :beard: :morebeard: :xmas: :HeHe: :trollface: :cookie: :rainbow: :monkeysee: :monkeysay: :happybday: :headwall: :offtopic: :superhappy: :terms: :beer:
View more smilies

BBCode is ON
[img] is OFF
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Float+int math, c++ gcc

Re: Float+int math, c++ gcc

Post by Miguel7 » 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!)? :)

Float+int math, c++ gcc

Post by Spawnova » 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.

Top