IOS MATH.H Math functions

Source: Internet
Author: User
Tags base 10 logarithm mathematical constants mathematical functions pow random seed

In the actual work, some programs inevitably need to use mathematical functions to calculate, such as the geographical coordinates of the map program to map coordinate transformation. Objective-c, as an extension of ANSI C, uses the mathematical macro constants and mathematical functions defined in the C standard library header file <math.h> to achieve basic mathematical calculations, so there is no need to bother looking for corresponding functions and classes in Cocoa Foundation. Here are some common macro and mathematical functions, more detailed information or need to consult the <math.h> header file.


Mathematical constants:
#define M_E 2.71828182845904523536028747135266250//E
#define M_LOG2E 1.44269504088896340735992468100189214//Log 2e
#define M_LOG10E 0.434294481903251827651128918916605082//log 10e
#define M_LN2 0.693147180559945309417232121458176568//log E2
#define M_LN10 2.30258509299404568401799145468436421//log E10
#define M_PI 3.14159265358979323846264338327950288//PI
#define M_PI_2 1.57079632679489661923132169163975144//PI/2
#define M_PI_4 0.785398163397448309615660845819875721//PI/4
#define M_1_PI 0.318309886183790671537767526745028724//1/PI
#define M_2_PI 0.636619772367581343075535053490057448//2/PI
#define M_2_SQRTPI 1.12837916709551257389615890312154517//2/sqrt (PI)
#define M_SQRT2 1.41421356237309504880168872420969808//sqrt (2)
#define M_SQRT1_2 0.707106781186547524400844362104849039//1/sqrt (2)

Common functions:
Exponential arithmetic
NSLog (@ "%.f", pow (3,2)); Result 9
NSLog (@ "%.f", pow (3,3)); Result 27

Open Square operation
NSLog (@ "%.f", sqrt (16)); Result 4
NSLog (@ "%.f", sqrt (81)); Result 9

Rounding on
NSLog (@ "Res:%.f", Ceil (3.000000000001)); Result 4
NSLog (@ "Res:%.f", Ceil (3.00)); Result 3

Rounding down
NSLog (@ "Res:%.f", Floor (3.000000000001)); Result 3
NSLog (@ "Res:%.f", Floor (3.9999999)); Result 3

Rounded
NSLog (@ "Res:%.F", round (3.5)); Result 4
NSLog (@ "Res:%.F", round (3.46)); Result 3
NSLog (@ "Res:%.F", round (-3.5)); Nb:this One returns-4

Minimum value
NSLog (@ "Res:%.f", Fmin (5,10)); Result 5

Maximum Value
NSLog (@ "Res:%.f", Fmax (5,10)); Result 10



Absolute
NSLog (@ "Res:%.f", Fabs (10)); Result 10
NSLog (@ "Res:%.f", Fabs (-10)); Result 10

The trigonometric functions that are not listed here are also part of the C standard math function and can be consulted in <math.h>.

-----------------------------------------------------

Need to introduce header file #import <math.h>

1. Trigonometric Functions
Double sin (double); sine
Double cos (double); cosine
Double tan (double); tangent
2, Inverse trigonometric function
Double asin (double); Results between [-PI/2, PI/2]
Double ACOs (double); Results between [0, PI]
Double Atan (double); Inverse tangent (main value), result between [-PI/2, PI/2]
Double atan2 (double, double); Tangent (rounded value), results in [-pi, PI]
3. Hyperbolic trigonometric Functions
Double Sinh (double);
Double cosh (double);
Double Tanh (double);
4. Exponent and logarithm
Double exp (double), the power of the natural number E
Double sqrt (double); open square
Double log (double); Logarithm with e as the base
Double log10 (double); base 10 logarithm
Double pow (double x, double y); calculates the X-based Y-power
float POWF (float x, float y); function is consistent with POW, except that both input and output are floating point numbers
5, take the whole
Double ceil (double); Take the whole
Double floor (double); Remove the entire
6. Absolute value
Double fabs (double);
Double cabs (struct complex znum), the absolute value of the plural
7. Standardized floating-point numbers
Double Frexp (double f, int *p); Normalized floating-point numbers, f = x * 2^p, known F for X, p (x between [0.5, 1])
Double Ldexp (double x, int p); In contrast to Frexp, X, p is known to ask F
8, take the whole and take the remainder
Double Modf (double, double*); Returns the integer part of the argument through the pointer, returning the fractional part
Double Fmod (double, double); Returns the remainder of dividing two arguments
9. Other
Double Hypot (double x, double y), known right triangle two right angle side length, to seek hypotenuse length
Double Ldexp (double x, int exponent), calculation x* (exponent power of 2)
Double poly (double x, int degree, double coeffs []), calculation polynomial
NT Matherr (struct exception *e); Math Error Calculation handler

10, take absolute value

int abs (int i); Handling the absolute value of an int type

Double Fabs (double i); Handle the absolute value of a double type

Float FABSF (float i); /Handle the absolute value of float type

11, take the random number

OBJECTIVE-C does not provide the relevant function to generate random numbers, but C is for Rand (), Srand (), Random (), Srandom (), Arc4random (), Randomize () several functions. To refer to a header file #include<stdlib.h>
, the methods used by the random () and randomize () functions are similar to those used by the rand () and Srand () functions, respectively. Arc4random () without seed

1) Srand ((unsigned) time (0)); Do not add this sentence every time the random number is constant int i = rand ()% 5;

2) Srandom (time (0)); int i = random ()% 5;

3) int i = Arc4random ()% 5;

Note: rand () and random () are not actually a true pseudo-random number generator, it is necessary to initialize the random seed before use, or the random number generated each time. Arc4random () is a true pseudo-random algorithm that does not need to generate random seeds because it is generated automatically when the first call is called. And the range is twice times that of Rand (). In the iphone, Rand_max is 0x7fffffff (2147483647), and Arc4random () returns the maximum value is 0x100000000 (4294967296).

Accuracy comparison: arc4random () > Random () > rand ().

Common methods: Arc4random

1) get a random integer range in:[0,100] including 0, not including int x = arc4random ()%;

2) Get a random number range in:[500,1000], including 500, excluding the $ int y = (arc4random ()% 501) + x;

3) gets a random integer range in [From,to], including from , not including to -(int) GetRandomNumber: (int) from to: (int) to {return (int) and from + (Arc4random ()% (To–from + 1))); +1,result is [from to]; else is (from, to)!!!!!!! }

RELATED links:

common mathematical functions for iOS development

Objective-c for C math functions <math.h>

Common math functions for iOS

IOS MATH.H Math functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.