Quantcast
Viewing all articles
Browse latest Browse all 15612

Absolute Value of a Float

I want to convert the below c code to VBA. I have attempted but the output is not what it should be. Any suggestions?
Code:

01: float myAbs(float x)
02: {
03:  // copy and re-interpret as 32 bit integer
04:  int casted = *(int*) &x;
05:  // clear highest bit
06:  casted &= 0x7FFFFFFF;
07:
08:  // re-interpret as float
09:  return *(float*)&casted;
10: }

Code:

Sub AbsF()

    Dim x As Double, y As Double, casted As Long

    x = -3.14

    casted = Int(x) And &H7FFFFFFF

    y = x And casted

    Debug.Print x, y, casted

End Sub


Viewing all articles
Browse latest Browse all 15612

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>