Jump to content

Clamp function.


Go to solution Solved by Melba23,

Recommended Posts

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

  • Moderators
  • Solution

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

#include <Math.au3>

Func _Clamp($value,$min,$max)
    Return _Max($min,_Min($max,$value))
EndFunc

 

Edited by RTFC
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...