Trig and Logarithms
Compute sin, cos, log, exp.
Trig and Logarithms is a free C Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Trig and Logarithms
The math.h header includes trigonometry functions like sin, cos, and tan, plus logarithm functions like log and log10.
They all take and return double values.
Angles Are in Radians
Important: the trig functions expect angles in radians, not degrees.
A full circle is 2 x pi radians. Half a circle (180 degrees) is pi radians.
Sine and Cosine
sin(x) and cos(x) return values between -1 and 1.
At an angle of 0, sine is 0 and cosine is 1.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("sin(0) = %.1f\n", sin(0.0));
printf("cos(0) = %.1f\n", cos(0.0));
return 0;
}Converting Degrees to Radians
To use degrees, convert first. Multiply degrees by pi / 180.
Here we compute the sine of 90 degrees, which should be 1.
#include <stdio.h>
#include <math.h>
int main(void) {
double deg = 90.0;
double rad = deg * M_PI / 180.0;
printf("sin(90 deg) = %.2f\n", sin(rad));
return 0;
}The tan Function
tan(x) returns the tangent of an angle, which equals sin(x) / cos(x).
At 45 degrees the tangent is 1.
#include <stdio.h>
#include <math.h>
int main(void) {
double rad = 45.0 * M_PI / 180.0;
printf("tan(45 deg) = %.2f\n", tan(rad));
return 0;
}Inverse Trig Functions
To go the other way (from a ratio back to an angle), use the inverse functions:
asinfor inverse sineacosfor inverse cosineatanfor inverse tangent
They return angles in radians.
#include <stdio.h>
#include <math.h>
int main(void) {
double angle = atan(1.0);
printf("atan(1) = %.4f rad\n", angle);
return 0;
}The Natural Logarithm
log(x) is the natural logarithm: the logarithm with base e (about 2.718).
It answers "to what power must e be raised to get x?" Note that log(1) is 0.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("log(1) = %.1f\n", log(1.0));
printf("log(M_E) = %.1f\n", log(M_E));
return 0;
}Base-10 Logarithm
For the common base-10 logarithm, use log10(x).
Since 1000 is 10 to the power 3, log10(1000) equals 3.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("log10(1000) = %.1f\n", log10(1000.0));
return 0;
}The exp Function
exp(x) is the inverse of log: it raises e to the power x.
So exp(0) is 1, and log(exp(2)) returns 2.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("exp(0) = %.1f\n", exp(0.0));
printf("exp(1) = %.3f\n", exp(1.0));
return 0;
}Domain Warnings
Logarithms are only defined for positive numbers.
log(0)returns negative infinitylogof a negative number returns NaN
Likewise asin and acos only accept inputs between -1 and 1.
Combining Them
This program converts an angle to radians, takes its sine, then prints a base-10 logarithm, showing both families together.
#include <stdio.h>
#include <math.h>
int main(void) {
double rad = 30.0 * M_PI / 180.0;
printf("sin = %.2f\n", sin(rad));
printf("log10(100) = %.0f\n", log10(100.0));
return 0;
}Quick Check
Recall what unit the trig functions expect.
Recap
You learned that sin, cos, and tan use radians, with inverses asin, acos, and atan.
You also met log (natural), log10 (base 10), and exp. Logs need positive inputs. Next: rounding and absolute values.
Frequently asked questions
Is the “Trig and Logarithms” lesson free?
Yes — the full text of “Trig and Logarithms” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Trig and Logarithms”?
Compute sin, cos, log, exp. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Trig and Logarithms” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Including math.h
- Powers and Roots
- Trig and Logarithms
- Rounding and Absolute Values