Mirage Source
http://miragesource.net/forums/

Add()
http://miragesource.net/forums/viewtopic.php?f=210&t=647
Page 1 of 3

Author:  William [ Mon Oct 16, 2006 3:33 pm ]
Post subject:  Add()

Hehe :P You wanted more tutorials ;) hehe.

Add:
Code:
Public Function Add(ByVal a As Long, ByVal b As Long)
Add = a + b
End Function


Usage:
Code:
Dim i As Long
i = Add(3, 5)


"i" would result in 8 :) Aint I good ?

Author:  Spodi [ Mon Oct 16, 2006 3:46 pm ]
Post subject: 

Congradulations, you managed to needless slow down and obfuscate the most simple form of math. :lol: :wink:

Though learning how to use functions appropriate is a pretty good thing. Just keep in mind that if you have:

Code:
Public Function MyFunc()


You need to define what type of format it is working in as MyFunc will be handled as a variable. In your example, you are making "Add" a variant (default variable format of a variable without a specified format).

Code:
Public Function MyFunc() As Long


Would make Add return a long.

Sorry, just wanted to point that out in case you didn't know. :wink:

Author:  William [ Mon Oct 16, 2006 4:19 pm ]
Post subject: 

I never really added that to my mind.. that part has been so obvious so I kinda never learned it :P So thanks for reminding me.

Author:  Verrigan [ Mon Oct 16, 2006 4:21 pm ]
Post subject: 

I have to say it.

Nesting is your friend! :D

Author:  William [ Mon Oct 16, 2006 4:24 pm ]
Post subject: 

At least Add() looks professional :P hehe

Author:  Leighland [ Mon Oct 16, 2006 6:28 pm ]
Post subject: 

Code:
Public Function Add(ByVal a As Long, ByVal b As Long)
        Add = a + b
End Function



Awww there ya go Verrigan... lol

Author:  Misunderstood [ Mon Oct 16, 2006 8:43 pm ]
Post subject: 

Thats too big of an indent :P

Author:  William [ Mon Oct 16, 2006 8:47 pm ]
Post subject: 

and you forgot as long after the function :P

Author:  Leighland [ Tue Oct 17, 2006 8:11 pm ]
Post subject: 

Too long of an indent cuz I did it manually with the spacebar lmao.

Code:
Public Function Add(ByVal a As Long, ByVal b As Long) As Long
     Add = a + b
End Function

There...

Author:  Obsidian [ Tue Oct 17, 2006 8:29 pm ]
Post subject: 

i HATE when people don't nest their code...

isn't it faster just to do

i = 5+3

instead of

i = Add(5,3)

it's less to type... but congrats for submitting something new :wink:

Author:  William [ Wed Oct 18, 2006 12:41 pm ]
Post subject: 

Haha..Obsidian, the whole topic was a joke. And you were the only one who didnt get it :P

Author:  Obsidian [ Wed Oct 18, 2006 2:57 pm ]
Post subject: 

oh no i figured it was... :roll:

Author:  halla [ Wed Oct 18, 2006 4:04 pm ]
Post subject: 

We cant leave subtraction out of the mix.

Code:
Public Function Subtract(ByVal a As Long, ByVal b As Long) as Long
         Subtract = a - b
End Function


Code:
Public Function Multiply(ByVal a As Long, ByVal b As Long) as Long
         Multiply = a * b
End Function


Code:
Public Function Divide(ByVal a As Long, ByVal b As Long) as Long
         Divide = a / b
End Function


There now the basic math functions don't feel left out.

Author:  pingu [ Wed Oct 18, 2006 6:16 pm ]
Post subject: 

I dare somebody to do square root without the "Sqrt()" functon...

Author:  Misunderstood [ Wed Oct 18, 2006 6:55 pm ]
Post subject: 

Code:
Public Function SquareRoot(ByVal a As Double) as Double
         SquareRoot=a^(1/2)
End Function

Author:  Leighland [ Wed Oct 18, 2006 8:17 pm ]
Post subject: 

Rofl, how about percentage? :p

Author:  Spodi [ Wed Oct 18, 2006 9:07 pm ]
Post subject: 

http://en.wikipedia.org/wiki/Methods_of ... uare_roots

Author:  Verrigan [ Wed Oct 18, 2006 10:35 pm ]
Post subject: 

Misunderstood wrote:
Code:
Public Function SquareRoot(ByVal a As Double) as Double
         SquareRoot=a^(1/2)
End Function
Is that method faster than Sqr(num)? :P

Author:  Spodi [ Wed Oct 18, 2006 11:55 pm ]
Post subject: 

Code:
Public Sub TestOne(ByVal Index As Long)
Dim a As Single
   
    a = Sqr(LookupTable(Index))
   
End Sub


Code:
Public Sub TestTwo(ByVal Index As Long)
Dim a As Single
   
    a = LookupTable(Index) ^ 0.5
   
End Sub


Code:
%Faster   -98.4|   -98.4|   -98.4|   -98.4|   -98.4|   -98.4|   -98.5|   -98.4|   -98.4|   -98.4
Test1       162|     160|     169|     161|     160|     161|     159|     161|     160|     161
Test2     10272|   10262|   10263|   10283|   10281|   10274|   10278|   10267|   10258|   10265


Nope. Not by a long shot. :wink:

Author:  halla [ Thu Oct 19, 2006 1:18 am ]
Post subject: 

Leighland wrote:
Rofl, how about percentage? :p


What do you mean?

Author:  Verrigan [ Thu Oct 19, 2006 1:36 am ]
Post subject: 

Thanks, Spodi. Is that in the IDE, or compiled?

Author:  Misunderstood [ Thu Oct 19, 2006 2:08 am ]
Post subject: 

Right, raising to the .5 is generally much slower in most programing languages than the buit in sqrt function.

Author:  Spodi [ Thu Oct 19, 2006 2:34 am ]
Post subject: 

Compiled, 5000 loops per each result. :wink:

Author:  Verrigan [ Thu Oct 19, 2006 3:00 am ]
Post subject: 

Thanks. I was at work, and didn't have access to mah VB compiler. :P

Author:  James [ Thu Oct 19, 2006 4:37 am ]
Post subject: 

Code:
Public Function Perct(By Val X as Double, By Val Per as Double) as Double
     Perct = X * (Per/100)
End Function


Perc(X, Per)

So it would output the percentile. :)

Already built in functions include:

Abs - Absolute Value
Sin, Cos. Tan (Sine Cosine Tangent)
Exp - base log e raised to specified power
log - logarithm (sp?)
Atn - Inverse tangent

Sqr - Square Root

Here are some nifty functions I found for CSC, SEC, and COT

Code:
Public Function Sec(ByVal angle As Double) As Double
    ' Calculate the secant of angle, in radians.
    Return 1.0 / Math.Cos(angle)
End Function

Public Function Csc(ByVal angle As Double) As Double
    ' Calculate cosecant of an angle, in radians.
    Return 1.0 / Math.Sin(angle)
End Function

Public Function Cot(ByVal angle As Double) As Double
    ' Calculate cotangent of an angle, in radians.
    Return 1.0 / Math.Tan(angle)
End Function



PS: Forumals to make the Inverse of the Trig Functions:

Inverse sine (Asin(x))
Atan(x / Sqrt(-x * x + 1))

Inverse cosine (Acos(x))
Atan(-x / Sqrt(-x * x + 1)) + 2 * Atan(1)

Inverse secant (Asec(x))
2 * Atan(1) – Atan(Sign(x) / Sqrt(x * x – 1))

Inverse cosecant (Acsc(x))
Atan(Sign(x) / Sqrt(x * x – 1))

Inverse cotangent (Acot(x))
2 * Atan(1) - Atan(x)

For those who don't remember trig, these forumals are derviced ALL from using the MANY Trig Identities altered to use TANGENT! Gah.

Page 1 of 3 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/