Jump to content
Sign in to follow this  
Cusem

Make a negative number positive

Recommended Posts

Cusem

This maybe is a stupid question, but I'm trying to turn a negative number (like -3, -0.15, -18) into a positive value (like 3, 0.15, 18).

For instance:

$var[5][2] = [[0.18, "Johnny"][-6, "Elsa"][2, "Robby"][0.57, "Esther"][-0.29, "Marcus"]]

_Arraysort($var, 0, 0, 0, 2)

I want extract the name that's closest to 0, however if I sort in this order, "Elsa" will be $var[0][0] and I want "Johnny" to become $var[0][0].

This is why I want to make the negative values positive ("elsa" and "marcus"), but I'm kinda stuck on how to do this the simplest way.

Share this post


Link to post
Share on other sites
Paulie

You are looking for Abs() (Absolute Value)

$Negative = Random(-100, -1,1)
$Positive = Abs($Negative)
Msgbox(0,"", "Negative: "&$Negative&@CRLF&"Positive: "&$Positive)
Edited by Paulie

Share this post


Link to post
Share on other sites
Richard Robertson

Paul, what do you think you're doing, answering my easy questions. :D

Share this post


Link to post
Share on other sites
MrCreatoR

The same without using the Abs()..

$Negative = Random(-100, -1,1)
$Positive = -$Negative
Msgbox(0,"", "Negative: "&$Negative&@CRLF&"Positive: "&$Positive)

:D


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites
weaponx

The same without using the Abs()..

$Negative = Random(-100, -1,1)
$Positive = -$Negative
Msgbox(0,"", "Negative: "&$Negative&@CRLF&"Positive: "&$Positive)

:D

This is NOT the same as abs().

Share this post


Link to post
Share on other sites
amokoura

if $number < 0 then
  $numberDoubleNegative = 2 * $number
  $numberPositified = $number - $numberDoubleNegative
  $number = $numberPositified 
endif

Whoa I just won AutoIt

Share this post


Link to post
Share on other sites
Squirrely1

Cusem - this works to do what you want:

#Include <Array.au3>

;Dim $var[5][2] = [["0.18", "Johnny"], ["-6", "Elsa"], ["2", "Robby"], ["0.57", "Esther"], ["-0.29", "Marcus"]]
Dim $var[5][2] = [[0.18, "Johnny"], [-6, "Elsa"], [2, "Robby"], [0.57, "Esther"], [-0.29, "Marcus"]]
For $i = 0 To UBound($var) - 1
;$var[$i][0] = String(Abs(Number($var[$i][0])))
    $var[$i][0] = Abs($var[$i][0])
Next

_Arraysort($var, 0, 0, 0, 2)
_ArrayDisplay($var)

I'm not sure of the wisdom of mixing strings with numbers in an array, but this code seems to work. I initially changed the numbers in you defined array into strings and used the rem'd - out line to "ABS" the data.

Here is from the help file under AutoIt > Language Reference > Variables:

An Array is a variable containing series of data elements of the same type and size....

It was said that an Array contains only one datatype of the same type. But technically speaking, a Variant in AutoIt can contain anything from a number to a boolean value. So an AutoIt-Array could also contain different types, even other Arrays: ...

This has not been strictly forbidden in AutoIt. However, it is NOT ADVISABLE to mix different datatypes in an Array. Especially the use of an Array inside another Array will severely affect the execution speed of your script.

Edited by Squirrely1

Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites
MrCreatoR

This is NOT the same as abs().

I didn't say that it is :D

I only say that you not have to use Abs() to get positive number.


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites
Cusem

Thanks guys!

This was exactly what I was looking for!

Share this post


Link to post
Share on other sites
Xenobiologist

HI,

can also do a * -1 :D

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
amokoura

Or:

sqrt($num ^ 2)

Share this post


Link to post
Share on other sites
Richard Robertson

That's probably the fastest outside of Abs. But then you will lose data if the number is large.

Share this post


Link to post
Share on other sites
Malkey

Another Abs() equivalent

$num * ((-1)^($num<0))

,where $num is positive or negative.

Only took 11days to think of it.

Share this post


Link to post
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
Sign in to follow this  

×