Mathematical Functions

Slides:



Advertisements
Similar presentations
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Advertisements

Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
STANDARD FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CMSC 1041 Functions II Functions that return a value.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
ICS 3U Math Class. ActionScript 3 – Math Class Rounding ceil – closest integer >= number (rounds up) floor – closest integer
Recursion It is a process of calling the same function itself again and again until some condition is satisfied. Syntax: func1() { ……….. func1(); }
Chapter 5 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Variables, Operators, and Expressions
Simple C Programs.
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
Type casting Algorithm & flowchart
presented BY : DURGESH KKHANDEKAR 1st semester
TMF1414 Introduction to Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Functions, Part 2 of 2 Topics Functions That Return a Value
Iterative Constructs Review
CMPT 201 Functions.
C Short Overview Lembit Jürimägi.
CSC113: Computer Programming (Theory = 03, Lab = 01)
FUNCTIONS EXAMPLES.
Deitel- C:How to Program (5ed)
Representation of data types
Chapter 5 - Functions Outline 5.1 Introduction
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Formatted and Unformatted Input/Output Functions
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Functions Chapter 3 of the text Motivation:
Using Free Functions Chapter 3 Computing Fundamentals with C++
Chapter 6 - Functions Outline 5.1 Introduction
CSCE 206 Lab Structured Programming in C
Functions October 23, 2017.
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Dr Tripty Singh Tutorial for Fuctions
Introduction to Programming
Module 14 Miscellaneous Topics
CSCE 206 Lab Structured Programming in C
Revision.
Intro to Arrays Storing List of Data.
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Mathematical Functions CSC 215 Mathematical Functions

math.h Include mathematical functions All functions in this library take double as an argument All functions in this library return double as the result.

double sqrt(double x) Parameter: x − This is the floating point value. Return Value: Returns the square root of x. Example: #include <stdio.h> #include <math.h> int main () { printf("Square root of %lf is %lf\n", 16.0, sqrt(16.0) ); return(0); } Output: Square root of 16.000000 is 4.000000

double pow (double x, double y) Parameters x − This is the floating point base value. y − This is the floating point power value. Return Value: This function returns the result of raising x to the power y. Example: #include <stdio.h> #include <math.h> int main () { printf("Value 2.0 ^ 3 = %lf\n", pow(2.0, 3)); return(0); } Output: Value 2.0 ^ 3 = 8.000000

double fabs(double x) Output: Parameters x − This is the floating point value. Return Value This function returns the absolute value of x. Example: #include <stdio.h> #include <math.h> int main () { int a, b; a = 1234; b = -344; double c = -135; printf("The absolute value of %d is %lf\n", a, fabs(a)); printf("The absolute value of %d is %lf\n", b, fabs(b)); printf("The absolute value of %lf is %lf\n", c, fabs(c)); return(0); } Output: The absolute value of 1234 is 1234.000000 The absolute value of -344 is 344.000000 The absolute value of -135.000000 is 135.000000

double ceil (double x) Output: value1 = 2.0 value2 = 2.0 value3 = 3.0 Parameters x − This is the floating point value. Return Value This function returns the smallest integral value not less than x. Example: #include <stdio.h> #include <math.h> int main () { float val1=1.6, val2=1.2, val3=2.8; int val4 = 2; printf ("value1 = %.1lf\n", ceil(val1)); printf ("value2 = %.1lf\n", ceil(val2)); printf ("value3 = %.1lf\n", ceil(val3)); printf ("value4 = %.1lf\n", ceil(val4)); return(0); } Output: value1 = 2.0 value2 = 2.0 value3 = 3.0 value4 = 2.0

double floor (double x) Parameters x − This is the floating point value. Return Value This function returns the largest integral value not greater than x. Example: #include <stdio.h> #include <math.h> int main () { float val1 = 1.6, val2 = -9.8, val3 = 2.8; int val4 = 2; printf("Value1 = %.1lf\n", floor(val1)); printf("Value2 = %.1lf\n", floor(val2)); printf("Value3 = %.1lf\n", floor(val3)); printf("Value4 = %.1lf\n", floor(val4)); return(0); } Output: Value1 = 1.0 Value2 = -10.0 Value3 = 2.0 Value4 = 2.0

double fmod(double x, double y) Parameters x − This is the floating point value with the division numerator i.e. x. y − This is the floating point value with the division denominator i.e. y .Return Value This function returns the remainder of dividing x/y. Example: #include <stdio.h> #include <math.h> int main () { float a = 9.2; float b = 3.2; int c = 2; printf("Remainder of %f / %d is %lf\n", a, c, fmod(a,c)); printf("Remainder of %f / %f is %lf\n", a, b, fmod(a,b)); return(0); } Output: Remainder of 9.200000 / 2 is 1.200000 Remainder of 9.200000 / 3.200000 is 2.800000

double log10(double x) Output: log10(10000.000000) = 4.000000 Parameters x − This is the floating point value. .Return Value This function returns the common logarithm of x, for values of x greater than zero Example: #include <stdio.h> #include <math.h> int main () {double x, ret; x = 10000; /* finding value of log1010000 */ ret = log10(x); printf("log10(%lf) = %lf\n", x, ret); return(0); } Output: log10(10000.000000) = 4.000000

Random Number Generation The function rand() computes a sequence of pseudo-random integers in the range zero to RAND_MAX, which is defined in <stdlib.h>. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767. int rand(void) Return Value: This function returns an integer value between 0 and RAND_MAX.

Example: #include <stdio.h> #include <stdlib.h> int main() { int i, n = 5; /* Print 5 random numbers from 0 to 49 */ for( i = 0 ; i < n ; i++ ) printf("%d\n", rand() % 50); } return(0); Output: 33 36 27 15 43