Showing posts with label cmath library. Show all posts
Showing posts with label cmath library. Show all posts

Saturday, 17 March 2012

The tanh Function in C & C++


More functions from the  <math.h> or <cmath> header files. Today it’s about the tanh function.

The tanh function returns the hyperbolic tangent for any real number and is defined as shown below.


 


From C11 standard.


The newest standard includes the atanh function, but it is not implemented in Visual C++ 2010; however, you can use the log function to determine the arc hyperbolic tangent.

 

Test Code.

I tested the functions in Visual C++ 2010 as an console application.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{
     // Header.
     printf("The tanh Function in C & C++\n\n");
     printf("Given x, any real number, the function returns y \n");
     printf("equal to the hyperbolic tangent of x.\n\n");

     // Counter for loop.
     int i;
     // Argument.
     double x;
     // Result.
     double y, y2;

     printf("    x   =>  tanh(x) = (exp(2*x) - 1.0) / (exp(2*x) + 1.0) \n\n");
     for (i = -5; i <= 5; ++i)
    {
           x = i * 1.0;              
           y = tanh(x);
           y2 = (exp(2*x) - 1.0) / (exp(2*x) + 1.0);
           printf ("%6.1f  => %8.4f = %8.4f\n", x, y, y2);
     }
     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;
} // end main


Output.



Friday, 16 March 2012

The cosh Function in C & C++


More functions from the  <math.h> or <cmath> header files. Today it’s about the cosh function.

The cosh function returns the hyperbolic cosine for any real number and is defined as shown below.



From C11 standard.


 
The newest standard includes the acosh function, but it is not implemented in Visual C++ 2010; however, you can use the log function to determine the arc hyperbolic cosine.



 Test Code.

I tested the functions in Visual C++ 2010 as an console application.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{
     // Header.
     printf("The cosh Function in C & C++\n\n");
     printf("Given x, any real number, the function returns y \n");
     printf("equal to the hyperbolic cosine of x.\n\n");

     // Counter for loop.
     int i;
     // Argument.
     double x;
     // Result.
     double y, y2;

     printf("    x   =>  cosh(x) =  0.5 * (exp(x) + 1.0/exp(x)) \n\n");
     for (i = -5; i <= 5; ++i)
     {
           x = i * 1.0;              
           y = cosh(x);
           y2 = 0.5 * (exp(x) + 1.0/exp(x));
           printf ("%6.1f  => %8.4f = %8.4f\n", x, y, y2);
     }
     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;
} // end main
 

Output.





Tuesday, 13 March 2012

The sinh Function in C & C++



More functions from the  <math.h> or <cmath> header files. Today it’s about the sinh function.

The sinh function returns the hyperbolic sine for any real number and is defined as shown below.




From C11 standard.


The newest standard includes the asinh function, but it is not implemented in Visual C++ 2010; however, you can use the log function to determine the arc hyperbolic sine.


Test Code.

I tested the functions in Visual C++ 2010 as an console application.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{
     // Header.
     printf("The sinh Function in C & C++\n\n");
     printf("Given x, any real number, the function returns y \n");
     printf("equal to the hyperbolic sine of x.\n\n");

     // Counter for loop.
     int i;
     // Argument.
     double x;
     // Result.
     double y, y2;

     printf("    x   =>  sinh(x) =  0.5 * (exp(x) - 1.0/exp(x)) \n\n");
     for (i = -5; i <= 5; ++i)
     {
           x = i * 1.0;              
           y = sinh(x);
           y2 = 0.5 * (exp(x) - 1.0/exp(x));
           printf ("%6.1f  => %8.4f = %8.4f\n", x, y, y2);
     }
     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;
} // end main

Output.





The sqrt Function in C & C++

More functions from the  <math.h> or <cmath> header files. Today it’s about the sqrt function.

The sqrt function is straightforward. Given values for x, the function returns y, the square root.


From C11 standard.


Test Code.

I tested the functions in Visual C++ 2010 as an console application.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{
     // Header.
     printf("The sqrt Function in C & C++\n\n");
     printf("Given x, the function returns y equal to the square root of x.\n\n");

     // Counter for loop.
     int i;
     // Argument.
     double n;
     // Result.
     double y;

     printf("  y     x    y    =   x\n\n");
     for (i = 1; i < 10; ++i)
     {
           n = 12.3456 / i;
           y = sqrt(n);
           printf ("%5.4f  x %5.4f = %6.4f\n", y, y, n);
     }
     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;
} // end main


Output.


Monday, 12 March 2012

The pow Function in C & C++


More functions from the  <math.h> or <cmath> header files. Today it’s about the pow function.
 
The pow function is straightforward. Given values for x and n, the function returns x to the nth power.


From C11 standard.

 

Test Code.

I tested the functions in Visual C++ 2010 as an console application.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{
     // Header.
     printf("The pow Function in C & C++\n\n");
     printf("Given x and n, the function returns x to the nth power.\n\n");

     // Counter for loop.
     int i;
     // Argument.
     double x, n;
     // Result.
     double y;

     printf(" y    =    x   ^ n\n\n");
     for (i = 1; i < 10; ++i)
     {
           x = 12.3456 / i;
           n = i;
           y = pow(x, n);
           printf ("%5.2f = %6.3f ^ %1.0f\n", y, x, n);
     }

     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;
} // end main


Output.


Sunday, 11 March 2012

The frexp Function in C & C++


More functions from the  <math.h> or <cmath> header files. Today it’s about the frexp function.

Given a floating point value a, the function returns S and n such that a = S x 2n.

The value of S is limited to a range of greater than or equal to 0.5 and less than 1.

The value of n is an integer and is returned by way of a pointer.

From C11 standard.





Test Code.

I tested the functions in Visual C++ 2010 as an console application.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{

     // Header.
     printf("The frexp Function in C & C++\n\n");
     printf("Given a, the function returns S and n\n");
     printf("such that a  = S x 2 to the nth power.\n\n");

     // Counter for loop.
     int i;
     // Return value, significant.
     double S;
     // Arugment.
     double a;
     // 2 to the Nth power.
     int n = 0;

     printf("a     =   S   x 2 ^ n\n\n");
     for (i = 10; i < 27; ++i)
     {
           a = i / 10.0;
           S = frexp(a, &n);
           printf ("%4.3f = %4.3f x 2 ^ %d\n", a, S, n);
     }

     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;
} // end main
 
Output.



Friday, 9 March 2012

The ldexp Function in C & C++


More functions from the  <math.h> or <cmath> header files. Today it’s about the ldexp function.

Given two values for a and n, the function returns y such that y = a x 2n.



From C11 standard.


Test Code.

I tested the functions in Visual C++ 2010 as an console application.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{
     // Header.
     printf("The ldexp Function in C & C++\n\n");
     printf("Given a and n, the function returns\n");
     printf("a x 2 to the nth power.\n\n");

     int i;

     printf(" a x 2 ^  n\n\n");
     for (i = 0; i < 16; ++i)
           printf("10 x 2 ^ %2d = %8.2f\n", i, ldexp(10.0, i));

     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;
} // end main


Output.



The fmod Function in C & C++


More functions from the  <math.h> or <cmath> header files. Today it’s about the fmod function.

The modulus operator, %, returns the remainder of a over b. For example, 5 % 2 = 1 since 4, a multiple of 2, plus 1, the remainder, equals 5. The modulus operator however can only be used with whole numbers. The fmod function applies the same principle to floating-point numbers.

 
From C11 standard.


 
Test Code.

I tested the functions in Visual C++ 2010 as an console application.

// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{
     // Header.
     printf("fmod Function\n\n");
     printf("x = fmod(a,b) \n\n\n");

     printf("Integer Modulus\n");
     printf("5 %% 2 = %d", 5 % 2);

     printf("\n\nFloating-point Modulus\n");
     printf("modf(5.3, 2) = %6.3f\t", fmod(5.3, 2.0));
     printf("\n\n");    

     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;
} // end main


Output.


The modf Function in C & C++


More functions from the  <math.h> or <cmath> header files. Today it’s about the modf function.

This function returns two values. To do that, one part is returned by way of assignment (e.g., R1 = fn). The second part is return by storing the value in the second argument. (e.g., R1 = f(a,R2). It accomplishes this by using pointers.

The modf function takes a floating point number, e.g., a.b, and breaks the two apart such that R1 = b and R2 = a. R2 is the fractional part. R1 is the integral part.


From C11 standard.


// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{
     // Header.
     printf("modf Function\n\n");
     printf("R1 = modf(a,R2) \n\n");

     double R1, R2;
     double a = 123.456;

     R1 = modf(a, &R2);
     printf("a = 123.456 R1 = %6.3f\tR2 = %3.f\n\n", R1, R2);

     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;
} // end main
 

Output.




Tuesday, 6 March 2012

The Base 10 Log Function in C & C++


I am making may way through all the different functions in the various standard header files that form part of C and C++. I’m still working on the contents of the <math.h> or <cmath> header files. Today is about the base 10 logarithmic function.

Say, ten, raised to the power of x, is N then the base 10 log of N is x. Example, 10 squared is 100. The base 10 log of 100 is 2 since 10 to the 2 is 100.


The exp() and log() functions are related since the return value of one can be passed to the other in a circle. There is no sister function for log10(), you have to use the power function with base 10.

Here is a snippet from the C standard (C11 N1570).


What does a domain error mean? First, it only occurs in relation to the functions in the math library. Second, it means you passed an argument to the function which is outside of the range of arguments allowed. The log10 function only allows numbers greater than zero.

From subclause 7.12.1 of the C11 standard:

For all functions, a domain error occurs if an input argument is outside the domain over which the mathematical function is defined. The description of each function lists any required domain errors; an implementation may define additional domain errors, provided that such errors are consistent with the mathematical definition of the function.

The return value for a domain error is dependent on the compiler. In Visual C++ 2010, the return value is -1.#IND000000000000. I suspect the IND stands for indeterminate. Try a google search on that return value.

A pole error indicates the return value approaches zero or positive or negative infinity under the limit functions in calculus. The implement of a pole error is compiler dependent and in Visual C++ 2010, the return value is -1.#INF000000000000 or negative infinity.

Passing a non-positive argument to the function does not result in a compile or run-time error. It’s up to the programmer to ensure proper argument values are passed to the function.


Test Code.

I tested the functions in Visual C++ 2010 as an console application.

Interestingly, I discovered something about the printf function. It prints out zeros when there is a mismatch between a format specifier and the argument. I used “%d” with a float variable and “%f” with other floats. In all cases zero was displayed even when there variables weren’t zero. More on that in another blog, but once I matched them, it worked properly.


// The standard library includes the system function.
#include <cstdlib>

// C++ standard I/O library.
#include <cstdio>

// C++ math library.
#include <cmath>

int main()
{
     // Header.
     printf("Logarithmic Base 10 Function\n\n");
     printf("\tn\t   log10(n)\t10^log10((n)) \n\n");

     // Counter.
     int i;
     // Argument.
     double n;

     for (i = 0; i < 8; ++i)
     {
           n = pow(10.0, i);
           printf("%9.0f\t%10.4f\t%10.0f\n", n, log10(n), pow(10.0,log10(n)));
     }
     printf("\n\n");

     // Negative argument.
     printf("Negative argument: %f ", log10(-1.0));
     printf("\n\n");

     // Zero value argument.
     printf("Zero value argument: %f ", log10(0.0));
     printf("\n\n");

     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;
} // end main


Output.