Mathematical functions: abs(), floor(), round(), ceil(), sqrt(), exp(), log(), sin(), cos(), tan(), pow(), trunc()

math.h is C's scientific calculator: pow and sqrt for powers and roots, the rounding quartet (floor down, ceil up, round nearest, trunc chop) that exams love to cross-examine, plus abs, exp, log and the trig trio in RADIANS.

11 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Four ways to lose the decimals

The class average lands at 72.6. What goes on the marksheet?

Depends who you ask: floor says 72, ceil says 73, round says 73, trunc says 72. Four functions, four philosophies, and on POSITIVE numbers two pairs agree, so they look interchangeable.

Then the number turns negative and the pairs split, which is exactly where examiners set their trap. math.h is today's toolbox; the rounding quartet is its exam heart.

At a glance

The math.h toolbox

FunctionJobExample
floor(x) / ceil(x)Always down / always upfloor(2.7)=2, ceil(2.1)=3
round(x) / trunc(x)Nearest / chop decimalsround(2.5)=3, trunc(2.9)=2
sqrt(x) / pow(x,y)Root / powersqrt(144)=12, pow(2,10)=1024
abs(x)Distance from zeroabs(-7)=7 (fabs for doubles)
exp(x) / log(x)e^x / NATURAL loglog is ln; log10 for base 10
sin, cos, tanTrigonometry in RADIANSsin(3.14159/2) ≈ 1

Theory

The quartet, precisely

  • floor(x): the largest integer ≤ x. Always slides DOWN: floor(2.7) = 2, and floor(-2.3) = -3 (down means more negative).
  • ceil(x): the smallest integer ≥ x. Always UP: ceil(2.1) = 3, ceil(-2.7) = -2.
  • round(x): the NEAREST integer, with .5 going away from zero: round(2.5) = 3, round(-2.5) = -3.
  • trunc(x): chop the decimals, moving toward zero: trunc(2.9) = 2, trunc(-2.7) = -2.

The split to memorise: on negatives, floor goes down (-3), trunc goes toward zero (-2). On positives they happen to agree, which is the disguise.

Practical

The quartet interrogated

#include <stdio.h>
#include <math.h>

int main() {
    double avg = -2.7;

    printf("floor: %.0f\n", floor(avg));   /* -3 */
    printf("ceil : %.0f\n", ceil(avg));    /* -2 */
    printf("round: %.0f\n", round(avg));   /* -3 */
    printf("trunc: %.0f\n", trunc(avg));   /* -2 */
    printf("power: %.0f\n", pow(2, 10));   /* 1024 */
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

floor(-2.3) and trunc(-2.3): what do they return?

  1. floor gives -3, trunc gives -2
  2. Both give -2
  3. Both give -3
  4. floor gives -2, trunc gives -3
Show the answer

floor gives -3, trunc gives -2

floor ALWAYS slides down the number line: below -2.3 lies -3. trunc simply deletes the .3, landing at -2 (toward zero). On positives the two agree (both 2 for 2.3), which is why the negative case is the exam's favourite ambush. One phrase: floor goes down, trunc goes home (to zero).

Think first

The power operator that isn't

A student computes 2 to the power 3 as: int x = 2 ^ 3; The program runs, no errors, and x is... what? And what SHOULD the student have written?

Show the answer

x is 1. In C, ^ is the bitwise XOR operator from Unit 1's table, never power: 2 XOR 3 compares bits (10 vs 11) and yields 01. No error, just a silently wrong answer, the worst kind. Correct: pow(2, 3), returning 8.0 as a double. "C has no power operator" is worth a permanent home in your notes.

Quiz

You call sin(90) expecting 1 (sine of 90 degrees) and get 0.894. Why?

  1. sin takes RADIANS: you asked for sine of 90 radians
  2. math.h's sin is approximate and often wrong
  3. You forgot to link the math library
  4. sin only works for values below 2π
Show the answer

sin takes RADIANS: you asked for sine of 90 radians

All C trig functions speak radians. Sine of 90 RADIANS is a perfectly correct 0.894. For degrees, convert first: sin(90 * 3.14159 / 180) ≈ 1. Forgetting the conversion produces plausible-looking wrong numbers, no crash, no warning, which makes this the sneakiest bug in scientific homework.

Watch out

Where marks leak

^ for power: XOR, never power; use pow. Degrees into trig: radians only, convert with ×π/180. log: it is the NATURAL log (ln); base-10 wants log10. abs vs fabs: abs is for ints (historically stdlib.h), fabs for doubles, abs(-2.7) quietly returns 2, not 2.7. And on GCC/Linux lab machines, math programs link with -lm: gcc prog.c -lm, or face "undefined reference to sqrt".

Theory

The library habit

Two lessons, two libraries: string.h for text, math.h for numbers, and the pattern is permanent: before writing a loop, ask "has someone shipped this?". The remaining question is how to build your OWN functions properly: return types, parameter lists, local variables, the formal anatomy, next lesson, where computeGrade() finally gets written the professional way.

Summary

Key takeaways

  • math.h: pow, sqrt, abs/fabs, exp, log (natural!), sin/cos/tan in RADIANS.
  • The quartet: floor down, ceil up, round nearest (.5 away from zero), trunc toward zero.
  • On negatives floor and trunc split: floor(-2.3) = -3, trunc(-2.3) = -2.
  • No power operator in C: ^ is XOR; 2^3 is 1, pow(2,3) is 8.
  • Degrees × π/180 before trig; gcc needs -lm for math programs.
  • Memory hook: floor goes down, trunc goes home.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Functions and Structure

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Mathematical functions: abs(), floor(), round(), ceil(), sqrt(), exp(), log(), sin(), cos(), tan(), pow(), trunc() · Computer Programming and Programming Methodology (CPPM) · Gri-Learn