Standard Library Functions Outline

Similar documents
Intrinsic Functions Outline

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

C++ Programming Lecture 11 Functions Part I

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

ECET 264 C Programming Language with Applications

Computer Science & Engineering 150A Problem Solving Using Computers

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Introduction to Engineering gii

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

CT 229 Java Syntax Continued

Fundamentals of Programming & Procedural Programming

Introduction to Programming

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University


C Functions. 5.2 Program Modules in C

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Chapter 4: Basic C Operators

Beginning C Programming for Engineers

Expressions. Eric McCreath

Goals for This Lecture:

Variable and Data Type 2

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Introduction to Programming

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

ANSI C Programming Simple Programs

Functions. Systems Programming Concepts

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Basic stuff -- assignments, arithmetic and functions

AMS 27L LAB #1 Winter 2009

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Maths Functions User Manual

Arithmetic Expressions Lesson #1 Outline

C++ Overview. Chapter 1. Chapter 2

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

C Programs: Simple Statements and Expressions

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

The Number object. to set specific number types (like integer, short, In JavaScript all numbers are 64bit floating point

Structured Programming. Dr. Mohamed Khedr Lecture 4

MYSQL NUMERIC FUNCTIONS

3.1. Chapter 3: The cin Object. Expressions and Interactivity

2 Making Decisions. Store the value 3 in memory location y

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

Product Price Formula extension for Magento2. User Guide

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Programming in QBasic

Assoc. Prof. Dr. Tansu FİLİK

Methods CSC 121 Fall 2014 Howard Rosenthal

Matlab Programming Introduction 1 2

Julia Calculator ( Introduction)

C Program Structures

Welcome to EGR 106 Foundations of Engineering II

9 Using Equation Networks

Python Programming: An Introduction to Computer Science

Green Globs And Graphing Equations

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

Introduction to Functions in C. Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

for Loop Lesson 3 Outline

1001ICT Introduction To Programming Lecture Notes

CS1010E Lecture 3 Simple C Programs Part 2

Chapter 1 Introduction to MATLAB

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Engineering Problem Solving with C++, Etter/Ingber

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Data Types and Basic Calculation

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

4. Modules and Functions

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Chapter 6 - Notes User-Defined Functions I

Introduction to MATLAB

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

LAB 1 General MATLAB Information 1

Introduction to Python, Cplex and Gurobi

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

Chapter 2. Outline. Simple C++ Programs

Structured programming

Chapter 3 Mathematical Functions, Strings, and Objects

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Chapter 3. built in functions help feature elementary math functions data analysis functions random number functions computational limits

What is Matlab? The command line Variables Operators Functions

Basic Assignment and Arithmetic Operators

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

GRAPH 4.4. Megha K. Raman APRIL 22, 2015

for Loop Lesson 2 Outline

10 Using the PCFL Editor In this chapter

Preview from Notesale.co.uk Page 2 of 79

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

ELEMENTARY MATLAB PROGRAMMING

Introduction to MATLAB 7 for Engineers

Arithmetic Expressions Lesson #2 Outline

Transcription:

Standard Library Functions Outline 1. Standard Library Functions Outline 2. Functions in Mathematics #1 3. Functions in Mathematics #2 4. Functions in Mathematics #3 5. Function Argument 6. Absolute Value Function in C #1 7. Absolute Value Function in C #2 8. Absolute Value Function in C #3 9. A Quick Look at abs 10. Function Call in Programming 11. Math Function vs Programming Function 12. C Standard Library 13. C Standard Library Function Examples 14. Is the Standard Library Enough? 15. Math: Domain & Range #1 16. Math: Domain & Range #2 17. Math: Domain & Range #3 18. Programming: Argument Type 19. Argument Type Mismatch 20. Programming: Return Type 21. More on Function Arguments 22. Function Argument Example Part 1 23. Function Argument Example Part 2 24. Function Argument Example Part 3 25. Using the C Standard Math Library 26. Function Call in Assignment 27. Function Call in printf 28. Function Call as Argument 29. Function Call in Initialization 30. Function Use Example Part 1 31. Function Use Example Part 2 32. Function Use Example Part 3 33. Function Use Example Part 4 34. Evaluation of Functions in Expressions 35. Evaluation Example #1 36. Evaluation Example #2 CS1313 Spring 2018 1

Functions in Mathematics #1 A rule that relates two variables, typically x and y, is called a function if to each value of x the rule assigns one and only one value of y. http://www.themathpage.com/aprecalc/functions.htm So, for example, if we have a function f(x) = x + 1 then we know that f(-2.5) = -2.5 + 1 = -1.5 f(-2) = -2 + 1 = -1 f(-1) = -1 + 1 = 0 f(0) = 0 + 1 = +1 f(+1) = +1 + 1 = +2 f(+2) = +2 + 1 = +3 f(+2.5) = +2.5 + 1 = +3.5 CS1313 Spring 2018 2

Functions in Mathematics #2 For example, if we have a function f(x) = x + 1 then we know that f(-2.5) = -2.5 + 1 = -1.5 f(-2) = -2 + 1 = -1 f(-1) = -1 + 1 = 0 f(0) = 0 + 1 = +1 f(+1) = +1 + 1 = +2 f(+2) = +2 + 1 = +3 f(+2.5) = +2.5 + 1 = +3.5 CS1313 Spring 2018 3

Functions in Mathematics #3 Likewise, if we have a function a(y) = y then we know that a(-2.5) = -2.5 = +2.5 a(-2) = -2 = +2 a(-1) = -1 = +1 a(0) = 0 = 0 a(+1) = +1 = +1 a(+2) = +2 = +2 a(+2.5) = +2.5 = +2.5 CS1313 Spring 2018 4

Function Argument f(x) = x + 1 a(y) = y We refer to the thing inside the parentheses immediately after the name of the function as the argument (also known as the parameter) of the function. In the examples above: the argument of the function named f is x; the argument of the function named a is y. NOTE: A function can have zero or one or multiple arguments. CS1313 Spring 2018 5

Absolute Value Function in C #1 In my_number.c, we saw this:... else if (abs(users_number computers_number) <= close_distance) { printf("close, but no cigar.\n"); } /* if (abs(...) <= close_distance) */... So, what does abs do? The abs function calculates the absolute value of its argument. It s the C analogue of the mathematical function a(y) = y (the absolute value function) that we just looked at. CS1313 Spring 2018 6

Absolute Value Function in C #2 fabs(-2.5) returns 2.5 abs(-2) returns 2 abs(-1) returns 1 abs(0) returns 0 abs(1) returns 1 abs(2) returns 2 fabs(2.5) returns 2.5 CS1313 Spring 2018 7

Absolute Value Function in C #3 We say abs of -2 evaluates to 2 or abs of -2 returns 2. Note: abs calculates the absolute value of an int argument; fabs calculates the absolute value of a float argument. CS1313 Spring 2018 8

A Quick Look at abs % cat abs_test.c #include <stdio.h> #include <math.h> int main () { /* main */ const int program_success_code = 0; printf("fabs(-2.5) = %f\n", fabs(-2.5)); printf(" abs(-2) = %d\n", abs(-2)); printf(" abs(-1) = %d\n", abs(-1)); printf(" abs( 0) = %d\n", abs( 0)); printf(" abs( 1) = %d\n", abs( 1)); printf(" abs( 2) = %d\n", abs( 2)); printf("fabs( 2.5) = %f\n", fabs( 2.5)); return program_success_code; } /* main */ % gcc -o abs_test abs_test.c -lm % abs_test fabs(-2.5) = 2.500000 abs(-2) = 2 abs(-1) = 1 abs( 0) = 0 abs( 1) = 1 abs( 2) = 2 fabs( 2.5) = 2.500000 CS1313 Spring 2018 9

Function Call in Programming Jargon: In programming, the use of a function in an expression is referred to as an invocation, or more colloquially as a call. We say that the statement printf("%d\n", abs(-2)); invokes or calls the function abs; the statement passes an argument of -2 to the function; the function abs returns a value of 2. CS1313 Spring 2018 10

Math Function vs Programming Function An important distinction between a function in mathematics and a function in programming: A function in mathematics is simply a definition ( this name means that expression ), whereas a function in programming is an action ( that name means execute that sequence of statements ). More on this later. CS1313 Spring 2018 11

C Standard Library Every implementation of C comes with a standard library of predefined functions. Note that, in programming, a library is a collection of functions. The functions that are common to all versions of C are known as the C Standard Library. On the next slide are examples of just a few of the functions in the C standard library. CS1313 Spring 2018 12

C Standard Math Library Function Examples Function Name Math Name Value Example abs(x) absolute value x abs(-1) returns 1 sqrt(x) square root x 0.5 sqrt(2.0) returns 1.414 exp(x) exponential e x exp(1.0) returns 2.718 log(x) natural logarithm ln x log(2.718 ) returns 1.0 log10(x) common logarithm log x log10(100.0) returns 2.0 sin(x) sine sin x sin(3.14 ) returns 0.0 cos(x) cosine cos x cos(3.14 ) returns -1.0 tan(x) tangent tan x tan(3.14 ) returns 0.0 ceil(x) ceiling x ceil(2.5) returns 3.0 floor(x) floor x floor(2.5) returns 2.0 CS1313 Spring 2018 13

Is the Standard Library Enough? It turns out that the set of C Standard Library functions is grossly insufficient for most real world tasks. So, in C, and in most programming languages, there are ways for programmers to develop their own user-defined functions. We ll learn more about user-defined functions in a future lesson. CS1313 Spring 2018 14

In mathematics: Math: Domain & Range #1 The domain of a function is the set of numbers that can be used for the argument(s) of that function. The range is the set of numbers that can be the result of that function. CS1313 Spring 2018 15

Math: Domain & Range #2 For example, in the case of the function f(x) = x + 1 we can define the domain of the function f to be the set of real numbers (sometimes denoted R), which means that the x in f(x) can be any real number. Similarly, we define the range of the function f to be the set of real numbers, because for every real number y there is some real number x such that f(x) = y. CS1313 Spring 2018 16

Math: Domain & Range #3 On the other hand, for a function q(x) = 1 / (x 1) the domain cannot include 1, because q(1) = 1 / (1 1) = 1 / 0 which is infinity (in the limit). So the domain of q might be R {1} (the set of all real numbers except 1). In that case, the range of q would be R {0} (the set of all real numbers except 0), because there s no real number y such that 1/y is 0. (Note: If you ve taken calculus, you ve seen that, as y gets arbitrarily large, 1/y approaches 0 as a limit but gets arbitrarily large is not a real number, and neither is approaches 0 as a limit. ) CS1313 Spring 2018 17

Programming: Argument Type Programming has concepts that are analogous to the mathematical concepts of domain and range: argument type and return type. For a given function in C, the argument type which corresponds to the domain in mathematics is the data type that C expects for an argument to that function. For example: the argument type of abs is int; the argument type of fabs is float. CS1313 Spring 2018 18

Argument Type Mismatch An argument type mismatch is when you pass an argument of a particular data type to a function that expects a different data type for that argument. Some implementations of C WON T check for you whether the data type of the argument you pass is correct. So if you pass the wrong data type, you can get a bogus answer. This problem is more likely to come up when you pass a float where the function expects an int. In the reverse case, typically C simply promotes the int to a float. CS1313 Spring 2018 19

Programming: Return Type Just as the programming concept of argument type is analogous to the mathematical concept of domain, likewise the programming concept of return type is analogous to the mathematical concept of range. The return type of a C function which corresponds to the range in mathematics is the data type of the value that the function returns. The return value is guaranteed to have that data type, and the compiler gets upset or you get a bogus result if you use the return value inappropriately. CS1313 Spring 2018 20

More on Function Arguments In mathematics, a function argument can be: a number: f(5) = 5 + 1 = 6 a variable: f(z) = z + 1 an arithmetic expression: f(5 + 7) = (5 + 7) + 1 = 12 + 1 = 13 another function: f(a(w)) = w + 1 any combination of these; i.e., any general expression whose value is in the domain of the function: f(3a(5w + 7)) = 3 ( 5w + 7 ) + 1 Likewise, in C the argument of a function can be any non-empty expression that evaluates to an appropriate data type, including an expression containing a function call. CS1313 Spring 2018 21

Function Argument Example Part 1 #include <stdio.h> #include <math.h> int main () { /* main */ const float pi = 3.1415926; const int program_success_code = 0; float angle_in_radians; printf("cos(%10.7f) = %10.7f\n", 1.5707963, cos(1.5707963)); printf("cos(%10.7f) = %10.7f\n", pi, cos(pi)); printf("enter an angle in radians:\n"); scanf("%f", &angle_in_radians); printf("cos(%10.7f) = %10.7f\n", angle_in_radians, cos(angle_in_radians)); printf("fabs(cos(%10.7f)) = %10.7f\n", angle_in_radians, fabs(cos(angle_in_radians))); CS1313 Spring 2018 22

Function Argument Example Part 2 printf("cos(fabs(%10.7f)) = %10.7f\n", angle_in_radians, cos(fabs(angle_in_radians))); printf("fabs(cos(2.0 * %10.7f)) = %10.7f\n", angle_in_radians, fabs(cos(2.0 * angle_in_radians))); printf("fabs(2.0 * cos(%10.7f)) = %10.7f\n", angle_in_radians, fabs(2.0 * cos(angle_in_radians))); printf("fabs(2.0 * "); printf("cos(1.0 / 5.0 * %10.7f)) = %10.7f\n", angle_in_radians, fabs(2.0 * cos(1.0 / 5.0 * angle_in_radians))); return program_success_code; } /* main */ CS1313 Spring 2018 23

Function Argument Example Part 3 % gcc -o function_arguments function_arguments.c -lm % function_arguments cos( 1.5707963) = 0.0000000 cos( 3.1415925) = -1.0000000 Enter an angle in radians: -3.1415925 cos(-3.1415925) = -1.0000000 fabs(cos(-3.1415925)) = 1.0000000 cos(fabs(-3.1415925)) = -1.0000000 fabs(cos(2.0 * -3.1415925)) = 1.0000000 fabs(2.0 * cos(-3.1415925)) = 2.0000000 fabs(2.0 * cos(1.0 / 5.0 * -3.1415925)) = 1.6180340 CS1313 Spring 2018 24

Using the C Standard Math Library If you re going to use functions like cos that are from the part of the C standard library that has to do with math, then you need to do two things: 1. In your source code, immediately below the #include <stdio.h> you MUST also have #include <math.h> 2. When you compile, you must append -lm to the end of your compile command: gcc -o function_arguments function_arguments.c lm (Note that this is hyphen ell em, NOT hyphen one em.) NOTE: -lm means link to the standard math library. CS1313 Spring 2018 25

Function Call in Assignment Function calls are used in expressions in exactly the same ways that variables and constants are used. For example, a function call can be used on the right side of an assignment or initialization: float theta = 3.1415926 / 4.0; float cos_theta; cos_theta = cos(theta); length_of_c_for_any_triangle = sqrt(a * a + b * b 2 * a * b * cos(theta)); CS1313 Spring 2018 26

Function Call in printf A function call can also be used in an expression in a printf statement: printf("%f\n", 2.0); printf("%f\n", pow(cos(theta), 2.0)); In CS1313, this usage is ABSOLUTELY FORBIDDEN, because all calculations should get done in the calculation subsection, NOT in the output subsection. But the C language does permit this usage. CS1313 Spring 2018 27

Function Call as Argument Since any expression can be used as some function s argument, a function call can also be used as an argument to another function: const float pi = 3.1415926; float complicated_expression;... complicated_expression = 1.0 + cos(asin(sqrt(2.0)/2.0) + pi)); CS1313 Spring 2018 28

Function Call in Initialization Most function calls can be used in initialization, as long as its arguments are literal constants: float cos_theta = cos(3.1415926); This is true both in variable initialization and in named constant initialization: const float cos_pi = cos(3.1415926); CS1313 Spring 2018 29

#include <stdio.h> #include <math.h> Function Use Example Part 1 int main () { /* main */ const float pi = 3.1415926; const float cos_pi = cos(3.1415926); const float sin_pi = sin(pi); const int program_success_code = 0; float phi = 3.1415926 / 4.0; float cos_phi = cos(phi); float theta, sin_theta; CS1313 Spring 2018 30

Function Use Example Part 2 theta = 3.0 * pi / 4; sin_theta = sin(theta); printf("2.0 = %f\n", 2.0); printf("pi = %f\n", pi); printf("theta = %f\n", theta); printf("cos(pi) = %f\n", cos(pi)); printf("cos_pi = %f\n", cos_pi); printf("sin(pi) = %f\n", sin(pi)); printf("sin_pi = %f\n", sin_pi); printf("sin(theta) = %f\n", sin(theta)); printf("sin_theta = %f\n", sin_theta); printf("sin(theta)^(1.0/3.0) = %f\n", pow(sin(theta), (1.0/3.0))); CS1313 Spring 2018 31

Function Use Example Part 3 printf("1 + sin(acos(1.0)) = %f\n", 1 + sin(acos(1.0))); printf("sin(acos(1.0)) = %f\n", sin(acos(1.0))); printf("sqrt(2.0) = %f\n", sqrt(2.0)); printf("sqrt(2.0) / 2 = %f\n", sqrt(2.0) / 2); printf("acos(sqrt(2.0)/2.0) = %f\n", acos(sqrt(2.0)/2.0)); printf("sin(acos(sqrt(2.0)/2.0)) = %f\n", sin(acos(sqrt(2.0)/2.0))); return program_success_code; } /* main */ CS1313 Spring 2018 32

Function Use Example Part 4 % gcc -o function_use function_use.c -lm % function_use 2.0 = 2.000000 pi = 3.141593 theta = 2.356194 cos(pi) = -1.000000 cos_pi = -1.000000 sin(pi) = 0.000000 sin_pi = 0.000000 sin(theta) = 0.707107 sin_theta = 0.707107 sin(theta)^(1.0/3.0) = 0.890899 1 + sin(acos(1.0)) = 1.000000 sin(acos(1.0)) = 0.000000 sqrt(2.0) = 1.414214 sqrt(2.0) / 2 = 0.707107 acos(sqrt(2.0)/2.0) = 0.785398 sin(acos(sqrt(2.0)/2.0)) = 0.707107 CS1313 Spring 2018 33

Evaluation of Functions in Expressions When a function call appears in an expression for example, on the right hand side of an assignment statement the function is evaluated just before its value is needed, in accordance with the rules of precedence order. CS1313 Spring 2018 34

Evaluation Example #1 For example, suppose that x and y are float variables, and that y has already been assigned the value -10.0. Consider this assignment statement: x = 1 + 2.0 * 8.0 + fabs(y) / 4.0; CS1313 Spring 2018 35

Evaluation Example #2 x = 1 + 2.0 * 8.0 + fabs(y) / 4.0; x = 1 + 16.0 + fabs(y) / 4.0; x = 1 + 16.0 + fabs(-10.0) / 4.0; x = 1 + 16.0 + 10.0 / 4.0; x = 1 + 16.0 + 2.5; x = 1.0 + 16.0 + 2.5; x = 17.0 + 2.5; x = 19.5; CS1313 Spring 2018 36