kevinalberts Posted May 27, 2023 Share Posted May 27, 2023 I searched for clamp function in cpp sites and autoit sites, I found something but i didn't understand. So in autoit i couldn't find a func for Clamp, but i tried to write it in autoit (in stack overflow someone gave an example in basic way (Click to go)). At least i wrote what i understood. But it returns 75 anyways, i'm sure i'm doing something wrong. But i also didn't understand clamp function. So can someone explain the func and write an example in autoit please? This is what i wrote: #include <Math.au3> Global $min = 0, $max = 100 MsgBox(64, "", Clamp(75, $min, $max)) Func Clamp($fNumber, $fLower, $fUpper) Return _Max($fLower, _Min($fNumber, $fUpper)) EndFunc Thanks in advance... Link to comment Share on other sites More sharing options...
Moderators Solution Melba23 Posted May 27, 2023 Moderators Solution Share Posted May 27, 2023 kevinalberts, Welcome to the AutoIt forums. Here is my take on a "clamp" function: - @extended shows whether the passed value has been "clamped": For $i = 1 To 20 ConsoleWrite(_Clamp($i, 5, 15) & " - " & @extended & @CRLF) Next Func _Clamp($iValue, $iMin, $iMax) Local $fClamp = False If $iValue < $iMin Then $iValue = $iMin $fClamp = True ElseIf $iValue > $iMax Then $iValue = $iMax $fClamp = True EndIf Return SetError(0, $fClamp, $iValue) EndFunc I have left @error alone so you can do some initial checking on the passed value to see if it meets your requirements. M23 kevinalberts 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
kevinalberts Posted May 27, 2023 Author Share Posted May 27, 2023 2 hours ago, Melba23 said: kevinalberts, Welcome to the AutoIt forums. Here is my take on a "clamp" function: - @extended shows whether the passed value has been "clamped": For $i = 1 To 20 ConsoleWrite(_Clamp($i, 5, 15) & " - " & @extended & @CRLF) Next Func _Clamp($iValue, $iMin, $iMax) Local $fClamp = False If $iValue < $iMin Then $iValue = $iMin $fClamp = True ElseIf $iValue > $iMax Then $iValue = $iMax $fClamp = True EndIf Return SetError(0, $fClamp, $iValue) EndFunc I have left @error alone so you can do some initial checking on the passed value to see if it meets your requirements. M23 Yeah i figured out already, thanks for help. I also found this on autoit forum and it does the same thing i guess. My code maybe wasn't good but i'm sure the source i found on this forum is at least better than mine. Also your source, thanks again. Here is the code i found (i forgot who made this so i won't mention him) Func Clamp($nValue,$nMinValue,$nMaxValue) If Number($nValue) > Number($nMaxValue) Then ; correcting for strings Return $nMaxValue ElseIf Number($nValue) < Number($nMinValue) Then ; correcting for strings Return $nMinValue Else Return $nValue EndIf EndFunc Again, also thanks for your example. Link to comment Share on other sites More sharing options...
RTFC Posted May 27, 2023 Share Posted May 27, 2023 (edited) #include <Math.au3> Func _Clamp($value,$min,$max) Return _Max($min,_Min($max,$value)) EndFunc Edited May 27, 2023 by RTFC kevinalberts 1 My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
Werty Posted May 27, 2023 Share Posted May 27, 2023 Func _Clamp($value,$min,$max) Return $value<$min?$min:$value>$max?$max:$value EndFunc kevinalberts 1 Some guy's script + some other guy's script = my script! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now