By: Henry in C Tutorials on 2007年10月03日 [フレーム]
The following listing contains a single program that demonstrates several of the Math functions. However if you need to refer to other functions then you may look at the tables below.
1: /* Demonstrates some of C's math functions */
2:
3: #include <stdio.h>
4: #include <math.h>
5:
6: main()
7: {
8:
9: double x;
10:
11: printf("Enter a number: ");
12: scanf( "%lf", &x);
13:
14: printf("\n\nOriginal value: %lf", x);
15:
16: printf("\nCeil: %lf", ceil(x));
17: printf("\nFloor: %lf", floor(x));
18: if( x >= 0 )
19: printf("\nSquare root: %lf", sqrt(x) );
20: else
21: printf("\nNegative number" );
22:
23: printf("\nCosine: %lf\n", cos(x));
24: return(0);
25: }
Enter a number: 100.95
Original value: 100.950000
Ceil: 101.000000
Floor: 100.000000
Square root: 10.047388
Cosine: 0.913482
ANALYSIS: This listing uses just a few of the math functions. A value accepted on line 12 is printed. Then it's passed to four of the C library math functions--ceil(), floor(), sqrt(), and cos(). Notice that sqrt() is called only if the number isn't negative. By definition, negative numbers don't have square roots. You can add any of the other math functions to a program such as this to test their functionality.
The trigonometric functions perform calculations that are used in some graphical and engineering applications.
The exponential and logarithmic functions are needed for certain types of mathematical calculations.
The hyperbolic functions perform hyperbolic trigonometric calculations.
The standard C library contains the following miscellaneous mathematical functions:
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in C )
Passing double value to a function in C
Sum of the elements of an array in C
Printing a simple histogram in C
Simple arithmetic calculations in C
Passing pointer to a function in C
Find square and square root for a given number in C
Using memset(), memcpy(), and memmove() in C
Latest Articles (in C)
Simple arithmetic calculations in C
Find square and square root for a given number in C
Printing a simple histogram in C
Sum of the elements of an array in C
Passing pointer to a function in C
Passing double value to a function in C
Infix to Prefix And Postfix in C
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate