Math Object Equivalents in C
The core JavaScript language has a static Math object, which features a number of methods
and properties that act as constants. For example, if you want to
determine the next lesser integer for a floating-point value, use the
floor() method of the static Math object, as follows:
var floorValue = Math.floor(myValue);
The C language doesn’t have a dedicated object of that type. Instead, it implements a series of functions, which you can call directly in your statements. For example:
double floorValue = floor(myValue);
Table 7-3 lists the JavaScript
Math object functions and the
corresponding C functions. The standard C language offers far more
variations of these functions (in the math.h and stdlib.h libraries automatically incorporated
into every iOS app), offering differing levels of precision. For Table 7-3, I have chosen the versions that
return values of the double data type. Also pay special attention to the
required data type of values passed as arguments to the C
functions.
Table 7-3. Math functions comparisons
JavaScript | Description | C/Objective-C |
|---|---|---|
| Absolute value |
|
| Arc cosine |
|
| Arc sine |
|
| Arc tangent |
|
| Angle of polar coordinates |
|
| Next greater integer |
|
| Cosine |
|
| Euler’s constant to xth power |
|
|
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access