Sign in to follow this  
Followers 0
jaenster

Something like the SQRT function

42 posts in this topic

SQRT function in autoit

;The code is fully writen by jaenster :)
; Full time to made, 49 minutes.

$return = _wortel("47")
consolewrite($return&@crlf)
$return = _wortel("49")
consolewrite($return&@crlf)
$return = _wortel("25")
consolewrite($return&@crlf)
$return = _wortel("1337")
consolewrite($return&@crlf)
func _wortel($Number)
   $Try = 0
   $Highst = -1
   $lowst = -1
   $factor = 1
   while 1
      $Try += $factor
      if $try*$try = $number Then
         return $try
      ElseIf $try*$try > $number Then
         if $Highst = -1 or $try*$try < $Highst Then
            $Highst = $try * $try
            $try = $try -($factor*2)
            $factor = $factor/10
            if $factor <= 0.0001 then
               return round($try,4)
            endif
         endif
      elseif $try*$try < $number Then
         if $lowst = -1 or $try*$try > $lowst Then
            $lowst = $try * $try
         endif
      endif
      if $try > $number then
         return $try
      endif
   wend
endfunc

-jaenster

Share this post


Link to post
Share on other sites



Hello

uhm? Why do you make a UDF for a function that already exists?

Mfg / Best Regards Spider


www.AutoIt.de - Moderator of the German AutoIt Froum

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hello

uhm? Why do you make a UDF for a function that already exists?

Mfg / Best Regards Spider

May be he didn't realise or, maybe he likes to see how things could be done using fundamental simple statements. This can be useful if you have to work something out in another language which doesn't have the maths experssions that Autoit has. Finding the square root on a processor without a maths coprocessor requires a bit of thinking. You can test the idea in AutoIt and prove the result with Execute.

Here's a similar way to find the square root which is a bit shorter.

$return = _wortel("47")
ConsoleWrite($return & @CRLF)
$return = _wortel("49")
ConsoleWrite($return & @CRLF)
$return = _wortel("25")
ConsoleWrite($return & @CRLF)
$return = _wortel("1337")
ConsoleWrite($return & @CRLF)

Func _wortel($Number)
   $Try = 10;could be anything
   $factor = 10;could be anything must be bigger than accuracy required
   While $factor > 0.0001;accuracy required
      
      If $Try * $Try > $Number Then; if you want cube root have $Try*$try*$try here etc
          $factor /= 2
          $Try -= $factor
     ElseIf $try < $number then
         $Try += $factor
     Else
         exitloop;we have exact answer
      EndIf
  WEnd
  return round($try,4)
  
EndFunc ;==>_wortel

But in AutoIt, the result of An is Exp(Log(A)*n)

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Carrot? :whistle:

Exactly carrot or root.

$wortel = 25^(1\2) ;square root = power of a half

Share this post


Link to post
Share on other sites

Exactly carrot or root.

$wortel = 25^(1\2) ;square root = power of a half

Aaaw, now you've ruined it all! That's much too simple.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Could someone smart explain this to me:

If I run this script:

$sqrt = Number(3^0.5);3^(1/2)
MsgBox(0, "", $sqrt)

it returns ~1.73, but when I replace ^ with * (3*0.5) then it returns 1.5 (i tried and calculator returns 1.5 too). What's the difference in ^ and *?

EDIT: I found the answer for my question: (from help file) ^ Raises a number to the power. e.g. 2 ^ 4 (equals 16), * Multiplies two numbers. e.g. 20 * 10 (equals 200). My next question is, that can I make that calculation (3^0.5) in real life with pencil and paper (and how)?

Edited by poisonkiller

Share this post


Link to post
Share on other sites

Could someone smart explain this to me:

If I run this script:

$sqrt = Number(3^0.5);3^(1/2)
MsgBox(0, "", $sqrt)

it returns ~1.73, but when I replace ^ with * (3*0.5) then it returns 1.5 (i tried and calculator returns 1.5 too). What's the difference in ^ and *?

EDIT: I found the answer for my question: (from help file) ^ Raises a number to the power. e.g. 2 ^ 4 (equals 16), * Multiplies two numbers. e.g. 20 * 10 (equals 200). My next question is, that can I make that calculation (3^0.5) in real life with pencil and paper (and how)?

Do you mean how can you calculate a square root by hand or do you mean how do you calculate any number to any power by hand?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Both, if possible. :whistle:

Share this post


Link to post
Share on other sites

But how do I calculate power with 0.5? (3^0.5)

Share this post


Link to post
Share on other sites

Yes but i made my source for:

1) To learn autoit beter

2) For fun

3) Maby somebody likes it, And learn something from me.


-jaenster

Share this post


Link to post
Share on other sites

But how do I calculate power with 0.5? (3^0.5)

Yes 3^0.5 is the square root of 3. It doesn't matter what the values are; x to the power y is x^y which is the same as 1/(x^-y)

x^y is the same as Exp(log(x) * y)


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

No I mean how can I calculate 0.5^3 with pencil and paper... I know that 2^3 equals 9 and 1^3 equals 3, but what about 0.5^3?

Share this post


Link to post
Share on other sites

0.5*0.5*0.5

0.5(0.5*0.5)

0.5(0.25)

0.125


My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.

Share this post


Link to post
Share on other sites

No I mean how can I calculate 0.5^3 with pencil and paper... I know that 2^3 equals 9 and 1^3 equals 3, but what about 0.5^3?

You write reverse power

I know that 2^3 equals 9

23 =8 = 2*2*2

32 =9 = 3*3

13 =1 = 1*1*1

31 =3 = 3

then

I know that 2^3 equals 8


A lan chat (Multilanguage)LanMuleFile transferTank gameTank 2 an online game[center]L'esperienza è il nome che tutti danno ai propri errori.Experience is the name everyone gives to their mistakes.Oscar Wilde[/center]

Share this post


Link to post
Share on other sites

Woops, it seems I wrote numbers backwards... Ok, I try to explain once more:

2^3=8 because 2*2*2=8, 3^3=27 because 3*3*3=27, 9^0.5=3 because...? How can I calculate 9^0.5?

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

No I mean how can I calculate 0.5^3 with pencil and paper... I know that 2^3 equals 9 and 1^3 equals 3, but what about 0.5^3?

I don't think you do know that.

2^3 = 8, but 3^2 = 9

1^3 is 1, but 3^1 = 3

Here is a good explanation of how to calculate square roots by hand.

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

Don't forget the first line of a square root script is always.

If $number < 0 Then

ConsoleWrite("Can't square root a negative number")

else

ConsoleWrite(($number ^ .5) & @CRLF)

endif

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  
Followers 0