copyright ©2005 department of computer & information science using number & math objects

32
Copyright Copyright ©2005 ©2005 Department of Computer & Information Science Department of Computer & Information Science Using Number & Math Using Number & Math Objects Objects

Upload: lora-hodge

Post on 12-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Using Number & Math Using Number & Math ObjectsObjects

Page 2: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

GoalsGoals

By the end of this lecture you should …By the end of this lecture you should …• Be able to identify and program with Be able to identify and program with

intrinsic methods of JavaScript's Number intrinsic methods of JavaScript's Number class.class.

• Be able to identify and program with Be able to identify and program with intrinsic properties of JavaScript's Math intrinsic properties of JavaScript's Math class.class.

• Be able to identify and program with Be able to identify and program with intrinsic functions of JavaScript's Math intrinsic functions of JavaScript's Math class.class.

Page 3: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Number TypesNumber Types

• JavaScript doesn't distinguish JavaScript doesn't distinguish among the different number among the different number types. We create new Number types. We create new Number objects using the Number objects using the Number constructor methodconstructor method::var intNum1 = new Number(5150);var intNum1 = new Number(5150);

var fltNum2 = new Number(867.5309);var fltNum2 = new Number(867.5309);

Page 4: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Number.toFixed()Number.toFixed() Method Method

• Number.toFixed()Number.toFixed() returns a number returns a number with a specified number places after with a specified number places after the decimal point. the decimal point.

• Rounding applies, using these rules:Rounding applies, using these rules:– If a digit is >= 5, then we round upIf a digit is >= 5, then we round up– If a digit is < 5, then we round downIf a digit is < 5, then we round down

• If the number has fewer decimal If the number has fewer decimal places than the requested decimal places than the requested decimal places, JavaScript will add zeroes.places, JavaScript will add zeroes.

Page 5: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called usingNumbers_01.html.

Page 6: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Number.toString()Number.toString() Method Method

• The The Number.toString()Number.toString() converts a numeric value to a converts a numeric value to a string type.string type.

• Use Use Number.toString()Number.toString() to to output numeric values.output numeric values.

Page 7: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

parseInt() parseInt() FunctionFunction

• parseInt()parseInt() takes a value and takes a value and converts that value to a number. converts that value to a number. Specifically, it converts the value Specifically, it converts the value to an integer (whole number).to an integer (whole number).

• We can then assign the We can then assign the converted value to a number converted value to a number variable.variable.

Page 8: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

parseFloat() parseFloat() FunctionFunction

• parseFloat()parseFloat() takes a value and takes a value and converts that value to a number. converts that value to a number. Specifically, it converts the value Specifically, it converts the value to a floating point number to a floating point number (number with a fractional value).(number with a fractional value).

• We can then assign the converted We can then assign the converted value to a number variable.value to a number variable.

Page 9: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

isNaN()isNaN() Function Function

• The The isNaN()isNaN() ("not a number") ("not a number") intrinsic function allows us to intrinsic function allows us to determine if a value is a numeric determine if a value is a numeric type or JavaScript can convert that type or JavaScript can convert that value to a Number.value to a Number.

• Returns true if JavaScript cannot Returns true if JavaScript cannot convert to a Number and false if it convert to a Number and false if it can convert the value to a Number.can convert the value to a Number.

Page 10: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called isNaN_01.html.

Page 11: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

The The Math Math ObjectObject

• JavaScript provides programmers JavaScript provides programmers with a convenient way to use with a convenient way to use common mathematical tasks and common mathematical tasks and properties. properties.

• JavaScript groups those tasks and JavaScript groups those tasks and properties into the properties into the MathMath object. object. Unlike Unlike StringString, , NumberNumber and other and other classes, we don't need to create and classes, we don't need to create and instance of the instance of the MathMath object in order object in order to use its attributes and functions.to use its attributes and functions.

Page 12: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Math.PIMath.PI Property Property

• The The Math.PIMath.PI property returns property returns the constant value of the constant value of ΠΠ (Pi). (Pi).

• You can use You can use Math.PI Math.PI for for calculating the circumference or calculating the circumference or area of a circle.area of a circle.

Page 13: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called usingMath_01.html.

Page 14: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Math.abs()Math.abs() Function Function

• The The Math.abs()Math.abs() function returns function returns the absolute value of a given the absolute value of a given value (how far the number is value (how far the number is from zero).from zero).

Page 15: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called usingMath_02.html.

Page 16: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Math.ceil()Math.ceil() Function Function

• The The Math.ceil()Math.ceil() function function returns the smallest integer returns the smallest integer greater than or equal to a given greater than or equal to a given value. value.

• The method always rounds up, The method always rounds up, towards 0.towards 0.

Page 17: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called usingMath_03.html.

Page 18: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Math.floor()Math.floor() Function Function

• The The Math.floor()Math.floor() function function returns the nearest integer less returns the nearest integer less than or equal to a given value.than or equal to a given value.

• The method always rounds The method always rounds down, away from 0.down, away from 0.

Page 19: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called usingMath_04.html.

Page 20: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Math.round()Math.round() Function Function

• Rounds a given value to the Rounds a given value to the nearest integer, using the nearest integer, using the following rules:following rules:– If a digit is >= 5, then we round upIf a digit is >= 5, then we round up– If a digit is < 5, then we round If a digit is < 5, then we round

downdown

Page 21: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called usingMath_05.html.

Page 22: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Math.max()Math.max() Function Function

• The The Math.max()Math.max() function returns function returns the largest number, given a set the largest number, given a set of multiple numbers. of multiple numbers.

• Separate the numeric arguments Separate the numeric arguments with commas.with commas.

Page 23: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called usingMath_06.html.

Page 24: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Math.min()Math.min() Function Function

• The The Math.min()Math.min() function returns function returns the smallest number, given a set the smallest number, given a set of multiple numbers. of multiple numbers.

• Separate the numeric arguments Separate the numeric arguments with commas.with commas.

Page 25: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called usingMath_07.html.

Page 26: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Math.pow()Math.pow() Function Function

• The The Math.pow()Math.pow() function returns function returns the result of an exponential the result of an exponential expression, given a base and an expression, given a base and an exponent.exponent.

• The base is the first argument The base is the first argument and the exponent is the second.and the exponent is the second.

Page 27: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called usingMath_08.html.

Page 28: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Math.sqrt()Math.sqrt() Function Function

• The The Math.sqrt()Math.sqrt() function function returns the square root of a returns the square root of a given value.given value.

Page 29: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called usingMath_09.html.

Page 30: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Math.random()Math.random() Function Function

• Math.random()Math.random() produces a produces a psuedo-random number, psuedo-random number, between 0 and 1.between 0 and 1.

• We usually need to scale the We usually need to scale the number (multiply by an integer number (multiply by an integer factor) and round it to obtain a factor) and round it to obtain a whole number.whole number.

Page 31: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

Take the next few minutes to examine the file called usingMath_10.html.

Page 32: Copyright ©2005  Department of Computer & Information Science Using Number & Math Objects

Copyright Copyright ©2005 ©2005 Department of Computer & Information ScienceDepartment of Computer & Information Science

SummarySummary

• JavaScript provides programmers JavaScript provides programmers with with NumberNumber methods to adjust methods to adjust the display of the display of NumberNumber data types. data types.

• JavaScript allows programmers JavaScript allows programmers to use to use MathMath object properties & object properties & functions without instantiating functions without instantiating the the MathMath class. class.