Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-24-2003, 01:46 PM   #1 (permalink)
anon
Guest
 
Posts: n/a
The Math object

We all know that Math is very important in the world of programming, so this tutorial will all be the properties and methods of the Math object. It can be used as a reference or to learn more about the Math Object, now some basic knowledge of JavaScript is required. And you must know what object, method, paramater, and property are and how to use them in JavaScript. Here we will discuss only the commonly, and slightly commonly used ones, the VERY uncommonly used ones will be listed at the bottom. And these are all the properties and methods of the Math object that are cross-browser compatible.

First lets look at the methods of the Math object.

abs()
Abs stands for Absolute Value, meaning any number you put in comes out positive.
document.write(Math.abs(2))
document.write(Math.abs(-2))
Will return the same thing.

ceil()
ceil() rounds up. for example
document.write(Math.ceil(10.01))
document.write(Math.ceil(10.99))
Will both return 11 Because it is rounded up.

cos()
cos() stands for cosine. Something like:
document.write(Math.cos(100))
would return the cosine of a hundred

floor()
Opposite of ceil(), always rounds down to the next lowest number. For example
document.write(Math.floor(10.01))
document.write(Math.floor(10.99))
would both return 10. Because they are rounded down.

max()
max() returns the maximum of two numbers, for example
document.write("The largest number is.... " + Math.max(100,200))
would return "The largest number is.... 200" because 100 and 200 were passes in as paramaters and 200 is greater, useless you think right? You're thinking "Hey I know 200 is greater than 100!" Well when you have to compare two variable it's very important.

min()
Opposite of max, returns the smaller number.

pow()
Used instead of ^. If you want to have a number to the something power than do:
Math.pow(2,3)
Would return 8, because 2 to the 3rd power is 8

round()
round() is a combination of floor() and ceil(). If the decimal is greater than or equal to .5 then it will round up, else it will round down.

random()
Math.random() returns a random number between 0 and 1.

sin()
sin() stands for sine. you pass in a number or variable as a paramter and it will return the sine of it, for example:
document.write(Math.sin(100));
would return the sine of 100

sqrt()
sqrt() stands for square root, pass in a number or variable or number as a paramater and it will return the number's square root, for example
document.write(Math.sqrt(100));
would return 10 because 10 is the square root of 100

tan()
tan() stands for tangent, to get the tangent of a number just pass in a number as a paramater. for example to get the tangent of 100 just do:
document.write(Math.tan(100))

Now lets look at the Properties of the Math object

E
Returns Euler's constant.

PI
Returns pi...

SQRT1_2
Returns the Square root of one half

SQRT2
Returns the Square root of the Square root of 2

Here are the "EXTREMELY" unused ones...

Properties:

LN2 - Returns the Natural logarithm of 2
LN10 - Returns the Natural logarithm of 10
LOG2E - Return the base 2 logarithm of E
LOG10E - Return the base 10 logarithm of E

Methods:

acos() - Returns the arccosine of a number
asin() - Returns the arcsine of a number
atan() - Returns the arctangent of a number
exp() - Returns E to the X power. Where X is the paramater
log() - Returns the natural logarithm of a number
  Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
math not quite accurate in C program uman Standard C, C++ 4 09-27-2004 05:19 PM
sorting objects arrays by object properties sde Java 28 08-05-2004 05:51 AM
creating an array of an object on the freestore in c++ ?? sde Standard C, C++ 7 08-04-2002 01:39 PM


All times are GMT -8. The time now is 07:24 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting