Abs: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
mNo edit summary
mNo edit summary
Line 1: Line 1:
{{PageAuthor|Lloigor}}
{{PageAuthor|Lloigor}}


A very simple macro to get an integer's absolute value, without the math plugin.
A very simple macro to get an integer's absolute value.


<highlight-nsis>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
<highlight-nsis>!define Abs "!insertmacro _Abs"
;; Get Absolute value of an Integer
!macro _Abs _RetVal_ _Value_
;; P1 :io: Integer
   IntOp ${_RetVal_} ${_Value_} & 0x7FFFFFFFFFFFFFFF
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
!define Abs "!insertmacro _Abs"
!macro _Abs _Value_
  IntCmp ${_Value_} -1 0 0 +2      ;; Is it negative?
   IntOp ${_Value_} ${_Value_} * -1  ;; Yes. Make it positive
!macroend</highlight-nsis>
!macroend</highlight-nsis>
Usage:<highlight-nsis>IntOp $IntVar 0 - 21  ; $IntVar = -21
Usage:<highlight-nsis>StrCpy $0 -40
${ABS} $IntVar        ; $IntVar = 21</highlight-nsis>
${ABS} $0 $0  ; $0 = 40</highlight-nsis>


[[Category:Math Functions]]
[[Category:Math Functions]]

Revision as of 06:25, 14 December 2011

Author: Lloigor (talk, contrib)


A very simple macro to get an integer's absolute value.

!define Abs "!insertmacro _Abs"
!macro _Abs _RetVal_ _Value_
   IntOp ${_RetVal_} ${_Value_} & 0x7FFFFFFFFFFFFFFF
!macroend

Usage:

StrCpy $0 -40
${ABS} $0 $0  ; $0 = 40