Complex
C library to work with complex numbers.
Install / Use
/learn @anic17/ComplexREADME
C complex library
[!NOTE] See the website for more information
Powerful C library to easily work with complex numbers, with a variety of complex implementations of some commonly used functions.
All angles are calculated in radians, however you can convert the different units with some built-in functions.
List of all functions
Angle conversion functions
long double degtorad(long double): Convert degrees to radians.long double radtodeg(long double): Convert radians to degrees.
Conversion functions
char* ctoa(complex_t): Convert complex expression to string, in format $a+bi$ or $a-bi$
Basic complex operations
complex_t cadd(complex_t, complex_t): Add 2 complex numbers.complex_t cdiff(complex_t, complex_t): Substract 2 complex numbers.complex_t cmultiply(complex_t, complex_t): Multiply 2 complex numbers.complex_t cdivide(complex_t, complex_t): Divide 2 complex numbers.complex_t conj(complex_t): Conjugate of a complex number.long double carg(complex_t): Complex argument.long double cabs(complex_t): Absolute value of a complex number.complex_t cvalue(long double, long double): Assign new values to a complex number, and return that value.long double imag(complex_t): Get the imaginary part of a complex number.long double real(complex_t): Get the real part of a complex number.
Powers and logarithms
complex_t cexp(complex_t num): Exponential function ($e^{x}$).complex_t cpower(complex_t, complex_t): Raise a complex number to a power.complex_t csqrt(complex_t): Square root of a complex number.complex_t clog(complex_t): Natural logarithm (base e).complex_t clogbase(complex_t, complex_t)Logarithm with any base.complex_t clog10(complex_t): Common logarithm (base 10).
Trigonometric functions
complex_t csin(complex_t): Sine.complex_t ccos(complex_t): Cosine.complex_t ctan(complex_t): Tangent.complex_t ccsc(complex_t): Cosecant.complex_t csec(complex_t): Secant.complex_t ccot(complex_t): Cotangent.
Hyperbolic functions
complex_t csinh(complex_t): Hyperbolic sine.complex_t ccosh(complex_t): Hyperbolic cosine.complex_t ctanh(complex_t): Hyperbolic tangent.complex_t ccsch(complex_t): Hyperbolic cosecant.complex_t csech(complex_t): Hyperbolic secant.complex_t ccoth(complex_t): Hyperbolic cotangent.
Example
// Define the 2 complex numbers, 2 + 3i and 3 + 4i respectively
complex_t num1 = {2, 3}, num2 = {3, 4};
// Natural logarithm
printf("ln(%s)=%s\n", ctoa(num1), ctoa(clog(num1))); // Prints 1.2824747+0.98279372i
// Multiplication
printf("(%s)*(%s)=%s", ctoa(num1), ctoa(num2), ctoa(cmultiply(num1, num2))); // Prints -6+17i
// Hyperbolic cosine
printf("cosh(%s)=%s\n", ctoa(num2), ctoa(ccosh(num2))); // Prints -6.580663-7.5815527i
Usage
To use the library, simply include it with the following line:
#include "complex.h"
Furthermore, ctoa() has 2 different output formats:
- Long, which shows the full complex number, including zero-valued parts.
- Short, which trims all the unnecessary data: if a part is zero, it won't be shown.
This behavior can be modified by changing the value of SHORT_CTOA (defined at line 15, complex.c to either 0 (long output) or 1 (short output).
Example outputs of both with cpower(cvalue(0, 1), cvalue(0, 1)) ( $i^{i}$ )
Long:
0.20787958+0i
Short:
0.20787958
For simplicity, cvalue() function is the same as j() function, in order to ease the coding process.
cpower(cvalue(0, 1), cvalue(0, 1)) --> cpower(j(0, 1), j(0, 1))
Known issues
Due to long double-precision floats limitations, precision loss may happen.
Copyright © 2025 anic17 Software
Related Skills
node-connect
348.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
348.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
348.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
