Objective C rounding, float Processing

Source: Internet
Author: User
Tags mathematical constants mathematical functions

Nslog (@ "square: %. F", POW (3, 2); // result 9

Nslog (@ "Top Rounding: %. F", Ceil (3.000000000001); // Result 4

Nslog (@ "Rounding: %. F", round (3.30); // result 3

Nslog (@ "drop in: % 0.f", floor (232.90); // result 232

Nslog (@ "minimum value: % 0.02f", fmin (3.22, 22.22); // result 3.22

Nslog (@ "maximum value: % 0.02f", fmax (3.22, 22.34); // result 22.34

Nslog (@ "absolute value: % 0.f", FABS (-10.00); // result 10

 

Here is a small tip:

1: If you want to round 3.321 to 3.32, that is, the last two digits of the decimal point, you can do this.

 

Nslog (@ "% 0.02f", round (3.321*100)/100); // result 3.21

Nslog (@ "% 0.02f", round (3.321); // result 3.00

The printed results are different.

 

As an extension of ansic, objective-C uses some mathematical constant macros and mathematical functions defined in the header file <math. h> in the C standard library for basic operations.

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_0000pi 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)

 

 

Transferred from http://blog.163.com/?email protected]/blog/static/7255470020123185505844/

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.