Main Menu


Sponsored Links

  


  
  
Web Cartoon Maker: a Fun Way to Learn C++ Contents Previous Next

Math Functions

WCM C++ supports some of the classic C++ math functions. However it is recommended to use function from WCM C++'s own extended library.

In mathematics, you have probably seen functions like sin and log, and you hav e learned to evaluate expressions like sin(π/2) and log(1/x). First, you evaluate the expression in parentheses, which is called the argument of the function. For example, π /2 is approximately 1.571, and 1/x is 0.1 (if x happens to be 10).

Then you can eva luate the function itself, either by looking it up in a table or by performing various computations. The sin of 1.571 is 1, and the log of 0.1 is -1 (assuming that log indicates the logarithm base 10).

This process can be applied repeatedly to evaluate mor e complicated expressions like log(1/sin(π /2)). First we evaluate the argument of the innermost function, then evaluate the function, and so on.

WCM C++ provides a set of built-in functions that includes most of the mathematical operation. The math functions are invoked using a syntax that is similar to mathematical notation with exception that for your convenience all the angles must be specified it degrees (not radians as in mathematics and classic C++). Here are the examples of using mathematical functions available in WCM C++:

double dRes;


// assign dRes sine of 60 degrees which is 0.866025

dRes = Sin ( 60 );

// assign dRes cosine of 60 degrees which is 0.500000

dRes = Cos ( 60 );

// outputs tangent of 60 degrees which is 1.732051

dRes = Tan ( 60 );

// assign dRes arcsine of 0.866025 which is 59.999954 degrees

dRes = ASin ( 0.866025 );

// assign dRes arccosine of 0.500000 which is 60.000001 degrees

dRes = ACos ( 0.500000 );

// assign dRes arctangent of 1.732051 which is 60.000003 degrees

dRes = ATan ( 1.732051 );

// assign dRes 2 raised to the power 3 which is 8

dRes = Pow ( 2, 3 );

// assign dRes e or 2.7182 raised to the power 3 which is 20.085537

dRes = Exp ( 3 );

// assign dRes the natural logarithm of 2.7182 which is 0.999970

dRes = Log ( 2.7182 );

// assign dRes square root of 4 which is 2.000000

dRes = Sqrt ( 4 );

// assign dRes a random value between 0 and 1 (not including 1)

dRes = Rand ();

As you can see from this example, some functions may have multiple parameters and some might not have any. This will be explained later.

Notes and Cautions: These functions are a lso examples of “loose typing”. Even though the functions are defined in the library to use a double argument and return a double result, this is only loosely enforced. This will generally be fine, but care should be used with this approach. Also keep in mind that even if you are using function Rand() then our compiled cartoon will not be random once it is created. But different compilations of the same cartoon will be.


Contents Previous Next
  
News

New Tales Animator Video by Alan Sturgess

Alan Sturgess shared an excellent video he made using Tales Animator! You can still download Tales Animator here. Unfortunately it is only available for Wi

...

Simple Online Character Designer

There is a prototype of simple online character designer available HERE. It is only a prototype, it does not contain many pieces yet but it can already generat

...

Book is updated

Now our book "Web Cartoon Maker: A Fun Way to Learn C++" is fully in synch with WCM 1.5! It is available for download and online reading HERE.

...

Web Cartoon Maker 1.5 is here!

Web Cartoon Maker 1.5 is finally here! You can download it HERE! Here is what was updated in version 1.5: Web Cartoon Maker Desktop Edition is now fully standal

...

read more news...


Poll