Common math functions for iOS

Source: Internet
Author: User
Tags base 10 logarithm random seed

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

1.Trigonometric function
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.Exponential and logarithmic
Double exp (double); Finding 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, just 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 number, F = x * 2^p, known F for X, p (x between [0.5, 1])
double Ldexp (double x, int p); opposite to Frexp, known as X, p for 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 #include<stdlib.h>
header file, 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) (from + (Arc4random ()% (To–from + 1))),//+1,result is [from to]; else is (from, to)!!!!!!!}

Common math functions for iOS

Related Article

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.